Option List - Range of Current Dates, Weeks, Months, Years

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
sathukorala
AppGini Super Hero
AppGini Super Hero
Posts: 121
Joined: 2020-02-16 16:29

Option List - Range of Current Dates, Weeks, Months, Years

Post by sathukorala » 2020-11-30 05:31

This method will give you a dropdown list of date ranges as an option list

Step 1:
In AppGini software select option list for the table field and put any value like "1;;2"
appgini.png
appgini.png (20.66 KiB) Viewed 1689 times
Step 2:

After compiling and installing the app goto hooks/table_name.php and insert below code block for the init function

Code: Select all

function COVID_cases_init(&$options, $memberInfo, &$args) {
	
	$PeriodWeekly = 5;
	$PeriodDaily = 5;
	$PeriodHalfDaily = 5;
	$PeriodMonthly = 5;
	$PeriodBiMonthly = 5;
	$PeriodQuarterly = 5;
	$PeriodSixMonthly = 5;
	$PeriodAnnualy = 5;
	
	$mode = 2;
	
	$file = '.\hooks\Table_name.field_name.csv';
	file_put_contents('.\hooks\Table_name.field_name.csv', "");
	
	
	if ($mode == 1) {
	$TDate = date("Y-m-d");
	for($i=0; $i<=$PeriodHalfDaily;$i++){
	
	$morning = date("Y-m-d", strtotime($TDate. ' - '.$i.' days'))." - Morning";
	$evening = date("Y-m-d", strtotime($TDate. ' - '.$i.' days'))." - Evening";
	
	$current = $morning . ";;" . $evening . ";;";
	$current = file_get_contents($file);
	$current .= $morning . ";;" . $evening . ";;";
	file_put_contents($file, $current);
	}
	}
	
	if ($mode == 2) {
	$TDate = date("Y-m-d");
	for($i=0; $i<=$PeriodDaily;$i++){
	
	$current = date("Y-m-d", strtotime($TDate. ' - '.$i.' days')) . ";;";
	$current = file_get_contents($file);
	$current .= date("Y-m-d", strtotime($TDate. ' - '.$i.' days')) . ";;";
	file_put_contents($file, $current);
	}
	}
	
	if ($mode == 3) {
	$monday = strtotime("last monday");
	$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;
	$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
	
	for($i=0; $i<=$PeriodWeekly;$i++){
	$n = $i*7;
	$start_date = date("Y-m-d",strtotime(date("Y-m-d",$monday)." -$n days"));
	$end_date = date("Y-m-d",strtotime(date("Y-m-d",$sunday)." -$n days"));
	
	$current = $start_date . " to " . $end_date . ";;";
	$current = file_get_contents($file);
	$current .= $start_date . " to " . $end_date . ";;";
	file_put_contents($file, $current);
	}
	}
	
	if ($mode == 4) {
	$TDate = date("Y-m-d");
	for($i=0; $i<=$PeriodMonthly;$i++){
	
	$current = date("Y, M", strtotime($TDate. ' - '.$i.' months')) . ";;";
	$current = file_get_contents($file);
	$current .= date("Y, M", strtotime($TDate. ' - '.$i.' months')) . ";;";
	file_put_contents($file, $current);
	}
	}
	
	if ($mode == 5) {
	if((date("m")%2)>0){
	$TDate = date("Y-m-01");
	$TDate = date("Y-m-01", strtotime($TDate. ' + 1 months'));	
	}else{
	$TDate = date("Y-m-01");
	}
	for($i=1; $i<=$PeriodBiMonthly;$i++){
	
	$current = date("Y, M", strtotime($TDate. ' - '.(($i*2)-1).' months')). " to " . date("Y, M", strtotime($TDate. ' - '.(($i*2)-2).' months')) . ";;";
	$current = file_get_contents($file);
	$current .= date("Y, M", strtotime($TDate. ' - '.(($i*2)-1).' months')). " to " . date("Y, M", strtotime($TDate. ' - '.(($i*2)-2).' months')) . ";;";
	file_put_contents($file, $current);
	}
	}
	
	if ($mode == 6) {
	if((date("m")%3)>0){
	$TDate = date("Y-m-01");
	$csoun=3-(date("m")%3);
	$TDate = date("Y-m-01", strtotime($TDate. ' + '.$csoun.' months'));	
	}else{
	$TDate = date("Y-m-01");
	}
	for($i=1; $i<=$PeriodQuarterly;$i++){
	
	$current = date("Y, M", strtotime($TDate. ' - '.(($i*3)-1).' months')). " to " . date("Y, M", strtotime($TDate. ' - '.(($i*3)-3).' months')) . ";;";
	$current = file_get_contents($file);
	$current .= date("Y, M", strtotime($TDate. ' - '.(($i*3)-1).' months')). " to " . date("Y, M", strtotime($TDate. ' - '.(($i*3)-3).' months')) . ";;";
	file_put_contents($file, $current);
	}
	}
	
	if ($mode == 7) {
	if((date("m")%6)>0){
	$TDate = date("Y-m-01");
	$csoun=6-(date("m")%6);
	$TDate = date("Y-m-01", strtotime($TDate. ' + '.$csoun.' months'));	
	}else{
	$TDate = date("Y-m-01");
	}
	for($i=1; $i<=$PeriodSixMonthly;$i++){
	
	$current = date("Y, M", strtotime($TDate. ' - '.(($i*6)-1).' months')). " to " . date("Y, M", strtotime($TDate. ' - '.(($i*6)-6).' months')) . ";;";
	$current = file_get_contents($file);
	$current .= date("Y, M", strtotime($TDate. ' - '.(($i*6)-1).' months')). " to " . date("Y, M", strtotime($TDate. ' - '.(($i*6)-6).' months')) . ";;";
	file_put_contents($file, $current);
	}
	}
	
	if ($mode == 8) {
	$TDate = date("Y-m-01");
	for($i=1; $i<=$PeriodAnnualy;$i++){
	
	$current = date("Y", strtotime($TDate. ' - '.($i-1).' years')) . ";;";
	$current = file_get_contents($file);
	$current .= date("Y", strtotime($TDate. ' - '.($i-1).' years')) . ";;";
	file_put_contents($file, $current);
	}
	}
	
		return TRUE;
	}

Replace "Table_name.field_name" with your desired values
$mode --> select dates, weeks based on the value

1 = twice daily
2 = daily
3 = weekly
4 = monthly
5 = bimonthly
6 = quartery
7 = six monthly
8 = yearly

$PeriodWeekly like variables --> gives the number of options

bidaily.png
bidaily.png (15.3 KiB) Viewed 1689 times
daily.png
daily.png (10.66 KiB) Viewed 1689 times
weekly.png
weekly.png (12.07 KiB) Viewed 1689 times
monthly.png
monthly.png (10.66 KiB) Viewed 1689 times
Step 3:
To make the dates descending change the common.js.php below section
"return b.text > a.text;" instead of "return a.text > b.text;"

If you want you can remove the below code block

Code: Select all

/* function to sort select2 search results by relevence */
AppGini.sortSelect2ByRelevence = function(res, cont, qry) {
	return res.sort(function(a, b) {
		if(qry.term) {
			var aStart = a.text.match(new RegExp("^" + qry.term, "i")) !== null;
			var bStart = b.text.match(new RegExp("^" + qry.term, "i")) !== null;
			if(aStart && !bStart) return false;
			if(!aStart && bStart) return true;
		}
		return b.text > a.text;
	});
}

One drawback, if you have an exiting record with a date selected that is not available in the newly created list (as it is already older than 5 weeks), the data will still be displayed in the tableview, but not in the detailview, as the reference to the possible selections will not be available any more.

sathukorala
AppGini Super Hero
AppGini Super Hero
Posts: 121
Joined: 2020-02-16 16:29

Re: Option List - Range of Current Dates, Weeks, Months, Years

Post by sathukorala » 2020-12-08 09:57

The drawback is solved !!!
This will Write old date ranges in detail view and also Prevent duplicates option list values appending in the method


hooks/table_name-dv.js

Code: Select all


window.onload = start();

function start(){
		id_value = $j('#id').text();
		
		$j.ajax({
			url: 'hooks/ajax2.php',
			data: { id_column : id_value },
			success: function(data){
				    var data = data.trim();
					var values = $j.map($j('#time_period option'), function(ele) {
					return ele.value;
					});
					var check =  !!(values.indexOf(data)+1);
				    if (check == false) {
					$j('#time_period').append($j('<option>', { value: (data), text: (data) }));
					setTimeout(function() { $j("#time_period").select2('val', (data));}, 200);
					}
			}
		});
}
hooks/ajax2.php

Code: Select all

<?php
	$currDir = dirname(__FILE__) . '/..';
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");


	$id_value = makesafe($_REQUEST['id_column']);


	$sql = "SELECT time_period FROM covid_cases WHERE id = '{$id_value}'";

	$data = sqlValue($sql);

	echo $data;
?>
P.S: change db name, field names according to your scenario

Post Reply