File manager - Edit - /home/wwwroot/camplus.hk/master.camplus.hk/public_html/application/helpers/global_helper.php
Back
<?php function pr(&$a) { echo '<pre>'; print_r($a); echo '</pre>'; } function vd(&$a) { echo '<pre>'; var_dump($a); echo '</pre>'; } function generateItemUrl($id, $itemTitle) { $url = base_url()."item/listing_view?item_id=".$id; $itemId = $id; $idLen = strlen($itemId); // $CI =& get_instance(); // $CI->load->model('Item_model'); // $itemTitle = $CI->Item_model->getItemTitle($itemId); $generatedItemTitle = url_title($itemTitle); $patterns = array(); $patterns[0] = '/listing_view\?/'; $patterns[1] = '/item_id=/'; $replacements = array(); $replacements[1] = $itemId.'/'; $replacements[0] = $generatedItemTitle; $newUrl = preg_replace($patterns, $replacements, $url); $len = strlen($newUrl); $endPoint = $len-$idLen; $generatedUrl = substr($newUrl,0,$endPoint); return $generatedUrl; } function makeClickableLinks($text) { $text = html_entity_decode($text); $text = " ".$text; $text = preg_replace('/(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)/', '<a href="\\1" target=_blank>\\1</a>', $text); $text = preg_replace('/(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)/', '<a href="\\1" target=_blank>\\1</a>', $text); $text = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)/', '\\1<a href="http://\\2" target=_blank>\\2</a>', $text); $text = preg_replace('/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/', '<a href="mailto:\\1" target=_blank>\\1</a>', $text); return $text; } function is_ajax() { return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest"); } function is_valid_url($url) { return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url); } function load_img($filename, $params = array(), $tag = true) { if($tag == true) { $params_str = ""; foreach($params as $key=>$param) { $params_str .= $key.'="'.$param.'" '; } return '<img src="'.base_url()."resources/images/".$filename.'" '.$params_str.'>'; } else return base_url()."resources/images/".$filename; } ////function to load he external js files not pertaining to controllers and action //file name would be relative to js folder function load_js($js_file=NULL,$tag=true) { if($js_file!=NULL){ if(file_exists("./resources/js/".$js_file)) return '<script type="text/javascript" src="'.base_url()."resources/js/".$js_file.'"></script>'; } } function load_css($css_file=NULL) { if($css_file!=NULL){ if(file_exists("./resources/css/".$css_file)) return '<link type="text/css" rel="stylesheet" href="'.base_url()."resources/css/".$css_file.'" />'; } } function getIndex(&$arrayName,$index) { if(isset($arrayName[$index])) return $arrayName[$index]; else return FALSE; } function formatDate($dt) { $tstamp = strtotime($dt); return date("D jS M, Y ",$tstamp); } function formatTime($tstamp) { return date("g:i a",$tstamp); } function ago($time) { $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array("60","60","24","7","4.35","12","10"); $now = time(); $difference = $now - $time; $tense = "ago"; for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { $difference /= $lengths[$j]; } $difference = round($difference); if($difference != 1) { $periods[$j].= "s"; } return "$difference $periods[$j]"; } function formatDateTime($tstamp) { return date("D jS M, Y (g:i a)",$tstamp); } function get_listing_count($userid) { $CI =& get_instance(); $CI->load->model('Item_model'); $data = $CI->Item_model->get_listing_count($userid); return $data; } function lq() { // helper to show last query $CI =& get_instance(); echo "<pre class='smalltext'>".htmlentities($CI->db->last_query())."</pre>"; } function getCountryList($index=null) { $countries = array( 'AF'=>'Afghanistan', 'AL'=>'Albania', 'DZ'=>'Algeria', 'AS'=>'American Samoa', 'AD'=>'Andorra', 'AO'=>'Angola', 'AI'=>'Anguilla', 'AQ'=>'Antarctica', 'AG'=>'Antigua and Barbuda', 'AR'=>'Argentina', 'AM'=>'Armenia', 'AW'=>'Aruba', 'AU'=>'Australia', 'AT'=>'Austria', 'AZ'=>'Azerbaijan', 'BS'=>'Bahamas', 'BH'=>'Bahrain', 'BD'=>'Bangladesh', 'BB'=>'Barbados', 'BY'=>'Belarus', 'BE'=>'Belgium', 'BZ'=>'Belize', 'BJ'=>'Benin', 'BM'=>'Bermuda', 'BT'=>'Bhutan', 'BO'=>'Bolivia', 'BA'=>'Bosnia and Herzegovina', 'BW'=>'Botswana', 'BV'=>'Bouvet Island', 'BR'=>'Brazil', 'IO'=>'British Indian Ocean Territory', 'BN'=>'Brunei Darussalam', 'BG'=>'Bulgaria', 'BF'=>'Burkina Faso', 'BI'=>'Burundi', 'KH'=>'Cambodia', 'CM'=>'Cameroon', 'CA'=>'Canada', 'CV'=>'Cape Verde', 'KY'=>'Cayman Islands', 'CF'=>'Central African Republic', 'TD'=>'Chad', 'CL'=>'Chile', 'CN'=>'China', 'CX'=>'Christmas Island', 'CC'=>'Cocos (Keeling) Islands', 'CO'=>'Colombia', 'KM'=>'Comoros', 'CG'=>'Congo', 'CD'=>'Congo, the Democratic Republic of the', 'CK'=>'Cook Islands', 'CR'=>'Costa Rica', 'CI'=>'Cote D\'Ivoire', 'HR'=>'Croatia', 'CU'=>'Cuba', 'CY'=>'Cyprus', 'CZ'=>'Czech Republic', 'DK'=>'Denmark', 'DJ'=>'Djibouti', 'DM'=>'Dominica', 'DO'=>'Dominican Republic', 'EC'=>'Ecuador', 'EG'=>'Egypt', 'SV'=>'El Salvador', 'GQ'=>'Equatorial Guinea', 'ER'=>'Eritrea', 'EE'=>'Estonia', 'ET'=>'Ethiopia', 'FK'=>'Falkland Islands (Malvinas)', 'FO'=>'Faroe Islands', 'FJ'=>'Fiji', 'FI'=>'Finland', 'FR'=>'France', 'GF'=>'French Guiana', 'PF'=>'French Polynesia', 'TF'=>'French Southern Territories', 'GA'=>'Gabon', 'GM'=>'Gambia', 'GE'=>'Georgia', 'DE'=>'Germany', 'GH'=>'Ghana', 'GI'=>'Gibraltar', 'GR'=>'Greece', 'GL'=>'Greenland', 'GD'=>'Grenada', 'GP'=>'Guadeloupe', 'GU'=>'Guam', 'GT'=>'Guatemala', 'GN'=>'Guinea', 'GW'=>'Guinea-Bissau', 'GY'=>'Guyana', 'HT'=>'Haiti', 'HM'=>'Heard Island and Mcdonald Islands', 'VA'=>'Holy See (Vatican City State)', 'HN'=>'Honduras', 'HK'=>'Hong Kong', 'HU'=>'Hungary', 'IS'=>'Iceland', 'IN'=>'India', 'ID'=>'Indonesia', 'IR'=>'Iran, Islamic Republic of', 'IQ'=>'Iraq', 'IE'=>'Ireland', 'IL'=>'Israel', 'IT'=>'Italy', 'JM'=>'Jamaica', 'JP'=>'Japan', 'JO'=>'Jordan', 'KZ'=>'Kazakhstan', 'KE'=>'Kenya', 'KI'=>'Kiribati', 'KP'=>'Korea, Democratic People\'s Republic of', 'KR'=>'Korea, Republic of', 'KW'=>'Kuwait', 'KG'=>'Kyrgyzstan', 'LA'=>'Lao People\'s Democratic Republic', 'LV'=>'Latvia', 'LB'=>'Lebanon', 'LS'=>'Lesotho', 'LR'=>'Liberia', 'LY'=>'Libyan Arab Jamahiriya', 'LI'=>'Liechtenstein', 'LT'=>'Lithuania', 'LU'=>'Luxembourg', 'MO'=>'Macao', 'MK'=>'Macedonia, the Former Yugoslav Republic of', 'MG'=>'Madagascar', 'MW'=>'Malawi', 'MY'=>'Malaysia', 'MV'=>'Maldives', 'ML'=>'Mali', 'MT'=>'Malta', 'MH'=>'Marshall Islands', 'MQ'=>'Martinique', 'MR'=>'Mauritania', 'MU'=>'Mauritius', 'YT'=>'Mayotte', 'MX'=>'Mexico', 'FM'=>'Micronesia, Federated States of', 'MD'=>'Moldova, Republic of', 'MC'=>'Monaco', 'MN'=>'Mongolia', 'MS'=>'Montserrat', 'MA'=>'Morocco', 'MZ'=>'Mozambique', 'MM'=>'Myanmar', 'NA'=>'Namibia', 'NR'=>'Nauru', 'NP'=>'Nepal', 'AN'=>'Netherlands Antilles', 'NL'=>'Netherlands', 'NC'=>'New Caledonia', 'NZ'=>'New Zealand', 'NI'=>'Nicaragua', 'NE'=>'Niger', 'NG'=>'Nigeria', 'NU'=>'Niue', 'NF'=>'Norfolk Island', 'MP'=>'Northern Mariana Islands', 'NO'=>'Norway', 'OM'=>'Oman', 'PK'=>'Pakistan', 'PW'=>'Palau', 'PS'=>'Palestinian Territory, Occupied', 'PA'=>'Panama', 'PG'=>'Papua New Guinea', 'PY'=>'Paraguay', 'PE'=>'Peru', 'PH'=>'Philippines', 'PN'=>'Pitcairn', 'PL'=>'Poland', 'PT'=>'Portugal', 'PR'=>'Puerto Rico', 'QA'=>'Qatar', 'RE'=>'Reunion', 'RO'=>'Romania', 'RU'=>'Russian Federation', 'RW'=>'Rwanda', 'SH'=>'Saint Helena', 'LC'=>'Saint Lucia', 'PM'=>'Saint Pierre and Miquelon', 'KN'=>'Saint Kitts and Nevis', 'VC'=>'Saint Vincent and the Grenadines', 'WS'=>'Samoa', 'SM'=>'San Marino', 'ST'=>'Sao Tome and Principe', 'SA'=>'Saudi Arabia', 'SN'=>'Senegal', 'CS'=>'Serbia and Montenegro', 'SC'=>'Seychelles', 'SL'=>'Sierra Leone', 'SG'=>'Singapore', 'SK'=>'Slovakia', 'SI'=>'Slovenia', 'SB'=>'Solomon Islands', 'SO'=>'Somalia', 'ZA'=>'South Africa', 'GS'=>'South Georgia and the South Sandwich Islands', 'ES'=>'Spain', 'LK'=>'Sri Lanka', 'SD'=>'Sudan', 'SR'=>'Suriname', 'SJ'=>'Svalbard and Jan Mayen', 'SZ'=>'Swaziland', 'SE'=>'Sweden', 'CH'=>'Switzerland', 'SY'=>'Syrian Arab Republic', 'TW'=>'Taiwan, Province of China', 'TJ'=>'Tajikistan', 'TZ'=>'Tanzania, United Republic of', 'TH'=>'Thailand', 'TL'=>'Timor-Leste', 'TG'=>'Togo', 'TK'=>'Tokelau', 'TO'=>'Tonga', 'TT'=>'Trinidad and Tobago', 'TN'=>'Tunisia', 'TR'=>'Turkey', 'TM'=>'Turkmenistan', 'TC'=>'Turks and Caicos Islands', 'TV'=>'Tuvalu', 'UG'=>'Uganda', 'UA'=>'Ukraine', 'GB'=>'United Kingdom', 'US'=>'United States', 'AE'=>'United Arab Emirates', 'UM'=>'United States Minor Outlying Islands', 'UY'=>'Uruguay', 'UZ'=>'Uzbekistan', 'VU'=>'Vanuatu', 'VE'=>'Venezuela', 'VN'=>'Viet Nam', 'VG'=>'Virgin Islands, British', 'VI'=>'Virgin Islands, U.s.', 'WF'=>'Wallis and Futuna', 'EH'=>'Western Sahara', 'YE'=>'Yemen', 'ZM'=>'Zambia', 'ZW'=>'Zimbabwe' ); if(is_null($index)) return $countries; else { if(strlen($index)>2) { $key = array_search($index,$countries); return $key; } else { if(isset($countries[$index])) return $countries[$index]; else return ""; } } } function getCurrentDateTime() { return date("Y-m-d G:i:s"); } function t($line,$mode=0) { $CI =& get_instance(); $text = $CI->lang->line($line); if($mode==0) echo $text; else return $text; } function ts($line,$mode=0) { $CI =& get_instance(); $array = explode(' ',$line); $text = ''; foreach($array as $key=>$data) { $text .= $CI->lang->line($data); $text .= (currLang()=='en' && count($array)-1!=$key)?' ':''; } if($mode==0) echo $text; else return $text; } function trim_text($string, $limit = 10, $dots = TRUE) { //echo $dots; $stringlen = strlen($string); if($stringlen > $limit) { if($dots) { $temp_string = substr($string, 0, ($limit - 3)); $new_string = $temp_string."..."; echo $new_string; } else { $temp_string = substr($string, 0, $limit); $new_string = $temp_string; echo $new_string; } } else { echo $string; } } function recursive_remove_directory($directory, $empty=FALSE) { // if the path has a slash at the end we remove it here if(substr($directory,-1) == '/') { $directory = substr($directory,0,-1); } // if the path is not valid or is not a directory ... if(!file_exists($directory) || !is_dir($directory)) { // ... we return false and exit the function return FALSE; // ... if the path is not readable }elseif(!is_readable($directory)) { // ... we return false and exit the function return FALSE; // ... else if the path is readable }else{ // we open the directory $handle = opendir($directory); // and scan through the items inside while (FALSE !== ($item = readdir($handle))) { // if the filepointer is not the current directory // or the parent directory if($item != '.' && $item != '..') { // we build the new path to delete $path = $directory.'/'.$item; // if the new path is a directory if(is_dir($path)) { // we call this function with the new path recursive_remove_directory($path); // if the new path is a file }else{ // we remove the file unlink($path); } } } // close the directory closedir($handle); // if the option to empty is not set to true if($empty == FALSE) { // try to delete the now empty directory if(!rmdir($directory)) { // return false if not possible return FALSE; } } // return success return TRUE; } } function getOriginal($url_title) { $title_parts = explode("-",$url_title); return implode(" ",$title_parts); } function normalize_str($str) { $invalid = array('á'=>'a', 'Á'=>'a', 'č'=>'c', 'Č'=>'c', 'ď'=>'d', 'Ď'=>'d', 'é'=>'e','ě'=>'e','É'=>'e','Ě'=>'e','í'=>"i",'Í'=>'i','ň'=>'n','Ň'=>'n', 'ó'=>'o','Ó'=>'o','ř'=>'r','Ř'=>'r','š'=>'s','Š'=>'s','ť'=>'t','Ť'=>'t','ú'=>'u', 'Ú'=>'u','ů'=>'u','Ů'=>'u','ž'=>'z','Ž'=>'z' ); $str = str_replace(array_keys($invalid), array_values($invalid), $str); return $str; } function is_allowed() { return TRUE; } function email($uid, $subject, $msg, $link,$module) { $CI =& get_instance(); $CI->notify->in_app($uid,$subject,$link,$module); $CI->notify->mail($uid,$subject,$msg,$link); } function addhttp($url) { if (!preg_match("~^(?:f|ht)tps?://~i", $url)) { $url = "http://" . $url; } return $url; } function array_count_values_of($value, $array) { $counts = array_count_values($array); if(isset($counts[$value])) return $counts[$value]; else return 0; } function generateRandomString($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; } function recurse_copy($src,$dst) { if(file_exists($src)) { $dir = opendir($src); @mkdir($dst); while(false !== ( $file = readdir($dir)) ) { if (( $file != '.' ) && ( $file != '..' )) { if ( is_dir($src . '/' . $file) ) { recurse_copy($src . '/' . $file,$dst . '/' . $file); } else { copy($src . '/' . $file,$dst . '/' . $file); } } } closedir($dir); } } function parse_read_template($content){ return preg_replace_callback('/\<\?php echo \$([a-zA-Z\d_]+)\; ?\?\>/', function ($matches){ if(count($matches) >= 2 ){ return '{' . $matches[1] . '}'; } }, $content); } function parse_write_template($content){ return preg_replace_callback('/{([a-zA-Z\d_]+)}/', function ($matches){ if(count($matches) >= 2 ){ return '<?php echo $' . $matches[1] . '; ?>'; } }, $content); } function _ToCamelCase($string, $capitalizeFirstCharacter = false) { $str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string))); if (!$capitalizeFirstCharacter) { $str[0] = strtolower($str[0]); } return $str; } function array_msort($array, $cols) { $colarr = array(); foreach ($cols as $col => $order) { $colarr[$col] = array(); foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); } } $eval = 'array_multisort('; foreach ($cols as $col => $order) { $eval .= '$colarr[\''.$col.'\'],'.$order.','; } $eval = substr($eval,0,-1).');'; eval($eval); $ret = array(); foreach ($colarr as $col => $arr) { foreach ($arr as $k => $v) { $k = substr($k,1); if (!isset($ret[$k])) $ret[$k] = $array[$k]; $ret[$k][$col] = $array[$k][$col]; } } return $ret; } function hoursToMinutes($hours) { /* pr(debug_backtrace());exit;*/ /*pr($hours); */$minutes = 0; if (strpos($hours, ':') !== false) { // Split hours and minutes. list($hours, $minutes) = explode(':', $hours); } return $hours * 60 + $minutes; } function getDailyDates($startDate,$dayType,$stopRepeatingType,$stopRepeatOn,$stopRepeatAfter,$daysCount,$excludeDates,$includeDates){ $dateArray = array(); if(is_array($startDate)) $startDate = $startDate[0]; if($startDate == '')return array(); $startDate = date_create_from_format('d/m/Y', $startDate); array_push($dateArray, $startDate->format('d/m/Y')); $stopRepeatOn = date_create_from_format('d/m/Y', $stopRepeatOn); if($dayType == 'weekday'){ if($stopRepeatingType == 1){ $endDate = clone $startDate; $endDate->add(new DateInterval('P'.($stopRepeatAfter).'D')); $dtA = getDateArray($startDate->format('d/m/Y'),$endDate->format('d/m/Y')); foreach ($dtA as $date) { if(!in_array($date, $dateArray)){ $date = date_create_from_format('d/m/Y', $date); if($date->format('w') != 6 && $date->format('w') != 0) array_push($dateArray, $date->format('d/m/Y')); } } } if($stopRepeatingType == 2){ $dtA = getDateArray($startDate->format('d/m/Y'),$stopRepeatOn->format('d/m/Y')); foreach ($dtA as $date) { if(!in_array($date, $dateArray)){ $date = date_create_from_format('d/m/Y', $date); if($date->format('w') != 6 && $date->format('w') != 0) array_push($dateArray, $date->format('d/m/Y')); } } } } if($dayType == 'days'){ if($stopRepeatingType == 1){ $endDate = clone $startDate; $endDate->add(new DateInterval('P'.($daysCount*$stopRepeatAfter).'D')); $dtA = getDateArray($startDate->format('d/m/Y'),$endDate->format('d/m/Y')); foreach ($dtA as $date) { for ($i=0; $i < count($dtA); $i+=$daysCount) { if(!in_array($dtA[$i], $dateArray)) array_push($dateArray, $dtA[$i]); } } } if($stopRepeatingType == 2){ $dtA = getDateArray($startDate->format('d/m/Y'),$stopRepeatOn->format('d/m/Y')); foreach ($dtA as $date) { if(!in_array($date, $dateArray)) array_push($dateArray, $date); } } } if($dayType == 'mon_wed_fri'){ $getDay = $startDate->format('w'); $differenceDay = 0; if($getDay < 5)$differenceDay = 5 - $getDay; if($getDay == 6)$differenceDay = 6; if($stopRepeatingType == 1){ $endDate = clone $startDate; $endDate->add(new DateInterval('P'.($differenceDay + ( ($stopRepeatAfter -1) *7)).'D')); } if($stopRepeatingType == 2){ $endDate = $stopRepeatOn; } $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 1); foreach ($dtA as $date) { if(!in_array($date, $dateArray)) array_push($dateArray, $date); } $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 3); foreach ($dtA as $date) { if(!in_array($date, $dateArray)) array_push($dateArray, $date); } $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 5); foreach ($dtA as $date) { if(!in_array($date, $dateArray)) array_push($dateArray, $date); } } if($dayType == 'tue_thu'){ $getDay = $startDate->format('w'); $differenceDay = 0; if($getDay < 4)$differenceDay = 4 - $getDay; if($getDay == 5)$differenceDay = 6; if($getDay == 6)$differenceDay = 5 ; if($stopRepeatingType == 1){ $endDate = clone $startDate; $endDate->add(new DateInterval('P'.($differenceDay + (($stopRepeatAfter -1) *7)).'D')); } if($stopRepeatingType == 2){ $endDate = $stopRepeatOn; } $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 2); foreach ($dtA as $date) { if(!in_array($date, $dateArray)) array_push($dateArray, $date); } $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 4); foreach ($dtA as $date) { if(!in_array($date, $dateArray)) array_push($dateArray, $date); } } $excludeDates = explode(',', $excludeDates); foreach ($excludeDates as $dt) { if($dt != ''){ //$dt = date('d/m/Y',strtotime($dt)); if(in_array($dt, $dateArray)){ $key = array_search ($dt, $dateArray); unset($dateArray[$key]); } } } $includeDates = explode(',', $includeDates); foreach ($includeDates as $dt) { if($dt != ''){ //$dt = date('d/m/Y',strtotime($dt)); if(!in_array($dt, $dateArray)){ array_push($dateArray, $dt); } } } return $dateArray; } function getWeeklyDates($startDate,$repeatOn=array(),$stopRepeatingType,$stopRepeatOn,$stopRepeatAfter,$weeksCount,$excludeDates,$includeDates){ $dateArray = array(); $dayCounter = 0; if(is_array($startDate)) $startDate = $startDate[0]; if($startDate == '')return array(); $startDate = date_create_from_format('d/m/Y', $startDate); array_push($dateArray, $startDate->format('d/m/Y')); $stopRepeatOn = date_create_from_format('d/m/Y', $stopRepeatOn); if(!empty($repeatOn)){ if($stopRepeatingType == 1){ $endDate = clone $startDate; $endDate->add(new DateInterval('P'.($stopRepeatAfter*$weeksCount*7).'D')); if(in_array('sun', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 0); $counter = 0; foreach ($dtA as $value) { if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); } $counter++; } } if(in_array('mon', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 1); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('tue', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 2); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('wed', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 3); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('thu', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 4); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('fri', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 5); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('sat', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 6); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } } if($stopRepeatingType == 2){ $endDate = $stopRepeatOn; if(in_array('sun', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 0); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('mon', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 1); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('tue', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 2); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('wed', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 3); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('thu', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 4); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('fri', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 5); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } if(in_array('sat', $repeatOn)){ $dtA = getDateForSpecificDayBetweenDates($startDate->format('d/m/Y'), $endDate->format('d/m/Y'), 6); $counter = 0; foreach ($dtA as $value) { //if($counter % $stopRepeatAfter == 0){ array_push($dateArray, $value); //} $counter++; } } } } $excludeDates = explode(',', $excludeDates); foreach ($excludeDates as $dt) { if($dt != ''){ //$dt = date('d/m/Y',strtotime($dt)); if(in_array($dt, $dateArray)){ $key = array_search ($dt, $dateArray); unset($dateArray[$key]); } } } $includeDates = explode(',', $includeDates); foreach ($includeDates as $key => $dt) { if($dt != ''){ //$dt = date('d/m/Y',strtotime($dt)); if(!in_array($dt, $dateArray)){ array_push($dateArray, $dt); } } } //pr($dateArray);exit; return $dateArray; } function getMonthlyDates($startDate,$stopRepeatingType,$stopRepeatOn,$stopRepeatAfter,$excludeDates,$includeDates,$monthlyType,$monthlyOnDaySelect,$monthlyOnOccurences,$monthlyOnWeekDaySelect,$months){ if(is_array($startDate)) $startDate = $startDate[0]; if($startDate == '')return array(); $dateArray = array(); $startDate = date_create_from_format('d/m/Y', $startDate); array_push($dateArray, $startDate->format('d/m/Y')); $stopRepeatOn = date_create_from_format('d/m/Y', $stopRepeatOn); $monthArray = array( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12' ); $monthArrayText = array( 'Jan' => 'January', 'Feb' => 'February', 'Mar' => 'March', 'Apr' => 'April', 'May' => 'May', 'Jun' => 'June', 'Jul' => 'July', 'Aug' => 'August', 'Sep' => 'September', 'Oct' => 'October', 'Nov' => 'November', 'Dec' => 'December' ); $dayArray = array( 'Sunday' => '0', 'Monday' => '1', 'Tuesday' => '2', 'Wednesday' => '3', 'Thursday' => '4', 'Friday' => '5', 'Saturday' => '6' ); $occurenceA = array('First','Second','Third','Fourth','Fifth','Last'); $dayCounter = 0; //echo $stopRepeatingType.' '.$monthlyType; if($stopRepeatingType == 1){ if($monthlyType == 1){ for ($i=$startDate->format('Y'); $i < ($startDate->format('Y') + $stopRepeatAfter); $i++) { if($monthlyOnDaySelect > 0){ foreach ($months as $key => $month) { $dt = date('d/m/Y',strtotime($i.'-'.$monthArrayText[$month].'-'.$monthlyOnDaySelect)); if(!in_array($dt, $dateArray)) array_push($dateArray, $dt); } } else { foreach ($months as $key => $month) { $no_of_days = cal_days_in_month(CAL_GREGORIAN,$monthArrayText[$month],$i); $difference = $no_of_days + $monthlyOnDaySelect; if($difference > 0){ $dt = date('d/m/Y',strtotime($i.'-'.$monthArrayText[$month].'-'.$difference)); if(!in_array($dt, $dateArray)) array_push($dateArray, $dt); } } } } } if($monthlyType == 2){ for ($i=$startDate->format('Y'); $i < ($startDate->format('Y') + $stopRepeatAfter); $i++) { foreach ($months as $key => $month) { if(!in_array($monthlyOnOccurences, $occurenceA)){ $day = getDateForSpecificDayBetweenDates(date("Y-m-01", strtotime($i.'/'.$monthArray[$month].'/01')),date("Y-m-t", strtotime($i.'/'.$monthArray[$month].'/01')),$dayArray[$monthlyOnWeekDaySelect]); if($monthlyOnOccurences == 'Next to last'){ array_push($dateArray, $day[count($day)-2]); } if($monthlyOnOccurences == 'Third from last'){ array_push($dateArray, $day[count($day)-3]); } if($monthlyOnOccurences == 'Fourth from last' && count($day) > 3){ array_push($dateArray, $day[count($day)-4]); } if($monthlyOnOccurences == 'Fifth from last' && count($day) > 4){ array_push($dateArray, $day[count($day)-5]); } } else { $dt = date('d/m/Y',strtotime($monthlyOnOccurences." ".$monthlyOnWeekDaySelect." of ".$monthArrayText[$month]." ".$i)); if(!in_array($dt, $dateArray)) array_push($dateArray, $dt); } } } } } if($stopRepeatingType == 2){ if($monthlyType == 1){ for ($i=$startDate->format('Y'); $i < $stopRepeatOn->format('Y'); $i++) { foreach ($months as $key => $month) { if(strcmp($startDate->format('Y-m') , $i.'-'.$monthArray[$month]) > 0){ if($monthlyOnDaySelect > 0){ $dt = date('d/m/Y',strtotime($i.'-'.$monthArrayText[$month].'-'.$monthlyOnDaySelect)); if(!in_array($dt, $dateArray)) array_push($dateArray, $dt); } else { foreach ($months as $key => $month) { $no_of_days = cal_days_in_month(CAL_GREGORIAN,$monthArrayText[$month],$i); $difference = $no_of_days + $monthlyOnDaySelect; if($difference > 0){ $dt = date('d/m/Y',strtotime($i.'-'.$monthArrayText[$month].'-'.$difference)); if(!in_array($dt, $dateArray)) array_push($dateArray, $dt); } } } } } } } if($monthlyType == 2){ for ($i=$startDate->format('Y'); $i <= $stopRepeatOn->format('Y'); $i++) { foreach ($months as $key => $month) { if(!in_array($monthlyOnOccurences, $occurenceA)){ $day = getDateForSpecificDayBetweenDates(date("Y-m-01", strtotime($i.'/'.$monthArray[$month].'/01')),date("Y-m-t", strtotime($i.'/'.$monthArray[$month].'/01')),$dayArray[$monthlyOnWeekDaySelect]); if($monthlyOnOccurences == 'Next to last'){ array_push($dateArray, $day[count($day)-2]); } if($monthlyOnOccurences == 'Third from last'){ array_push($dateArray, $day[count($day)-3]); } if($monthlyOnOccurences == 'Fourth from last' && count($day) > 3){ array_push($dateArray, $day[count($day)-4]); } if($monthlyOnOccurences == 'Fifth from last' && count($day) > 4){ array_push($dateArray, $day[count($day)-5]); } } else { $dt = date('d/m/Y',strtotime($monthlyOnOccurences." ".$monthlyOnWeekDaySelect." of ".$monthArrayText[$month]." ".$i)); if(!in_array($dt, $dateArray)) array_push($dateArray, $dt); } } } } } $excludeDates = explode(',', $excludeDates); foreach ($excludeDates as $dt) { if($dt != ''){ //$dt = date('d/m/Y',strtotime($dt)); if(in_array($dt, $dateArray)){ $key = array_search ($dt, $dateArray); unset($dateArray[$key]); } } } $includeDates = explode(',', $includeDates); foreach ($includeDates as $key => $dt) { if($dt != ''){ //$dt = date('d/m/Y',strtotime($dt)); if(!in_array($dt, $dateArray)){ array_push($dateArray, $dt); } } } //pr($dateArray); return $dateArray; } function getDateArray($start,$end){ //$dates = array($start); $start = date_create_from_format('d/m/Y', $start); $end = date_create_from_format('d/m/Y', $end); $new_dates = array($start->format('d/m/Y')); $counter = $start->diff($end)->format('%a'); for($i=1; $i<=$counter; $i++){ $t = $start->add(new DateInterval('P1D'))->format('d/m/Y'); $new_dates[] = $t; } return $new_dates; } function getDateForSpecificDayBetweenDates($startDate, $endDate, $weekdayNumber) { $startDate = date_create_from_format('d/m/Y', $startDate); $endDate = date_create_from_format('d/m/Y', $endDate); $dateArr = array(); do { if($startDate->format("w") != $weekdayNumber) { $startDate->add(new DateInterval('P1D')); // add 1 day } } while($startDate->format("w") != $weekdayNumber); while($startDate <= $endDate) { $dateArr[] = $startDate->format('d/m/Y'); $startDate->add(new DateInterval('P7D')); // add 7 days } return($dateArr); } function filter_callback($val) { $val = trim($val); return $val != ''; } function subval_sort($a,$subkey) { if(!empty($a)){ foreach($a as $k=>$v) { $b[$k] = strtolower($v[$subkey]); } asort($b); foreach($b as $key=>$val) { $c[] = $a[$key]; } return $c; } else return array(); } function clean($string) { $result = str_replace("'", "", $string); $result = str_replace('"', "", $result); return $result; } function eventMinimumDate($eventId = 0){ if($eventId == 0) return ''; $CI =& get_instance(); $dateArray = array(); $CI->db->select('startDate'); $sections = $CI->db->get_where('event_has_sections',array('event_id' => $eventId))->result_array(); foreach ($sections as $key => $section) $dateArray[] = $section['startDate']; if(!empty($dateArray)) return min($dateArray); else return ''; } function getMinDate($dates = ''){ $dates = explode(',', $dates); return min($dates); } function currLang() { if(!empty($_COOKIE)) { return $_COOKIE['language']; } else { return 'en'; } } function userExist($username) { $CI =& get_instance(); $CI->db->select('id'); $CI->db->from('users'); $uid = $CI->db->where('username="'.$username . '"')->get()->row_array(); if(!empty($uid)) return $uid['id']; else return false; } function userPermission($key) { $user_array = array(); $CI =& get_instance(); $CI->db->select('*'); $CI->db->from('role_acl'); $CI->db->where('permission LIKE','%'.$key.'%'); $roles = $CI->db->get()->result_array(); foreach ($roles as $role){ $CI->db->select('user_id'); $users = $CI->db->get_where('users_roles',array('role_id'=>$role['role_id']))->result_array(); foreach ($users as $user) { if(!in_array($user['user_id'],$user_array)){ array_push($user_array,$user['user_id']); } } } //sort($user_array); return $user_array; } function checkModule($module = 'core') { $CI =& get_instance(); $CI->db->select('value'); $serialized_module = $CI->db->get_where('site_settings',array('setting'=>'modules'))->row_array(); $modules = unserialize($serialized_module['value'])['modules']; if(in_array($module,$modules)) { return true; } else { return false; } } function allUsers($userID = 0,$mode = 0,$username=null){ //mode 0: show username; mode 1: show user real name; mode 3: show email $CI =& get_instance(); $returnArray = array(); if($username==null) { $users = $CI->db->get_where('users',array('id' => $userID))->row_array(); } else { $users = $CI->db->get_where('users',array('username' => $username))->row_array(); } switch ($mode) { case 0: return $users['username']; break; case 1: if(currLang()=='en') { return $users['name_english']; } else { return $users['name_chinese']; } break; case 2: return $users['email']; break; } } function user_array() { $CI =& get_instance(); if(currLang()=="en") $CI->db->select("id,name_english as name"); else $CI->db->select("id,name_chinese as name"); $users = $CI->db->from('users')->get()->result_array(); foreach($users as $user) { $array[$user['id']] = $user['name']; } return $array; } function module_abbr($value) { $type='CORE'; if($value == 'core') $type = 'CORE'; if($value == 'purchase_request') $type = 'PR'; if($value == 'tender') $type = 'TD'; if($value == 'event_booking') $type = 'EB'; if($value == 'appraisal_deprecated') $type = 'AP_old'; if($value == 'appraisal') $type = 'AP'; if($value == 'form') $type = 'FORM'; if($value == 'workflow') $type = 'WF'; if($value == 'timetable') $type = 'TB'; if($value == 'attendance') $type = 'AT'; if($value == 'leave_sub') $type = 'LS'; if($value == 'overtime') $type = 'OT'; if($value == 'academic_year') $type = 'MIS'; if($value == 'facility') $type = 'MIS'; if($value == 'venue') $type = 'MIS'; if($value == 'holiday') $type = 'MIS'; if($value == 'supplier') $type = 'MIS'; if($value == 'timeslot') $type = 'MIS'; return $type; } function utf8ize($d) { if (is_array($d)) { foreach ($d as $k => $v) { $d[$k] = utf8ize($v); } } else if (is_string ($d)) { return utf8_encode($d); } return $d; } function export_xls($header,$arr,$filename,$hide=[]) { $CI =& get_instance(); //load our new PHPExcel library $CI->load->library('excel'); //activate worksheet number 1 $CI->excel->setActiveSheetIndex(0); //name the worksheet if(strlen($filename)<30) $title = $filename; else $title = substr($filename,0,30); $CI->excel->getActiveSheet()->setTitle($title); $outarray = []; if(!empty($hide)) { foreach($hide as $key=>$column){ if($column==0) unset($header[$key]); } } $outarray[] = array_values($header); for($i=0;$i<count($arr);$i++) { if(!empty($hide)) { $temp = array_values($arr[$i]); foreach($hide as $key=>$column){ if($column==0) unset($temp[$key]); } if(empty($temp)) $temp = ' '; $outarray[]=$temp; } else { $outarray[]=array_values($arr[$i]); } } // read data to active sheet $CI->excel->getActiveSheet()->fromArray($outarray); header('Content-Type: application/vnd.ms-excel'); //mime type header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); //tell browser what's the file name header('Cache-Control: max-age=0'); //no cache $objWriter = PHPExcel_IOFactory::createWriter($CI->excel, 'Excel5'); $objWriter->save('php://output'); } function import_xls($name,$path,$empty=array(),$remove_empty=0) { // $name = field name // $path = upload directory // $empty = check if that column is empty (required) // $remove_empty=1, row that do not fits all required fit will be ignored $CI =& get_instance(); $exceldata=[]; $error=[]; $header = []; $config['upload_path'] = $path; //'./files/import'; $config['allowed_types'] = '*'; $config['max_size'] = 10000; $config['max_width'] = 10024; $config['max_height'] = 10008; $CI->load->library('upload', $config); if ( ! $CI->upload->do_upload($name)) { $error['upload']=$CI->upload->display_errors(); } else { $data = $CI->upload->data(); $file = $data['full_path']; //load the excel library $CI->load->library('excel'); //read file from path $objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007'); $objPHPExcel->setReadDataOnly(true); $objPHPExcel = PHPExcel_IOFactory::load($file); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); // Loop through each row of the worksheet in turn for ($row = 1; $row <= $highestRow; $row++) { // Read a row of data into an array $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, TRUE); $add = true; if($row==1) $header = $rowData[0]; else { $all_empty=true; //check if any column is empty foreach ($rowData[0] as $col) { if($col!="") $all_empty = false; } if($all_empty!=true) { foreach($empty as $key=>$column) { if($column==1) { if($rowData[0][$key]=="") { $add = false; $error['empty'][$row-1] = $key+1; } } } } if($remove_empty==1 && $add==false) { } else { if($all_empty==false) $exceldata[]=$rowData[0]; } } } } $array = array('data'=>$exceldata, 'error'=>$error, 'header'=>$header); //print_r($array);exit; return $array; } function generate_users($users,$mode=1) { usort($users, "cmp"); foreach ($users as $user) { if($mode==1) echo "<option value='" . $user['id'] . "'>"; else echo "<option value='" . $user['username'] . "'>"; echo $user[currLang()=='en' ? 'name_english' : 'name_chinese']; echo " (".$user['username'].")"; echo "</option>"; } } function generate_groups($groups) { foreach ($groups as $group) { echo "<option value='gp" . $group['id'] . "'> ".$group['name'] ." </option>"; } } function generate_usergroups($class='',$id='',$id_mode=true, $attr='') { $CI =& get_instance(); $groups = $CI->db->get('groups')->result_array(); $CI =& get_instance(); $users = $CI->db->get('users')->result_array(); echo "<select class='grpuser select2 " . $class; echo ($id_mode)? "' id='".$id:"' name='".$id; echo $attr; echo "' multiple>"; echo "<optgroup label='Groups'>"; generate_groups($groups); echo "</optgroup>"; echo "<optgroup label='Users'>"; generate_users($users); echo "</optgroup>"; echo "</select>"; echo "<script>$(document).ready(function(){" . PHP_EOL; echo ($id_mode)? "$('#".$id."').on('select2:selecting', function (e) {" : "$('.grpuser').on('select2:selecting', function (e) {" . PHP_EOL; echo "console.log(e.params.args) if (e.params.args.data.element.value.indexOf('gp') >= 0) { e.preventDefault() console.log('group selected') var selected = $(e.target).val(); if(selected==null) { selected=[] } $.get( '" . base_url(). "core/User_group/getGroupUsers/' + e.params.args.data.element.value.replace('gp',''), function( data ) { $.each(data,function(i,val){ selected.push(val.id) }) $(e.target).val(selected).trigger('change') $(e.target).select2('close') }, 'json'); } }) });</script> "; } function cmp($a, $b) { return strcmp($a["name_english"], $b["name_english"]); } function unescape($data){ $patterns = array('/"/'); $replace = array('"'); return preg_replace($patterns, $replace, $data); } function uid(){ $CI =& get_instance(); echo $CI->session->userdata('id'); } function student_pic($sid) { if(file_exists(FCPATH.'/files/student_picture/'.$sid.'.jpg')) { return base_url().'/files/student_picture/'.$sid.'.jpg'; } else if(file_exists(FCPATH.'/files/student_picture/'.$sid.'.JPG')) { return base_url().'/files/student_picture/'.$sid.'.JPG'; } else if(file_exists(FCPATH.'/files/student_picture/'.$sid.'.png')) { return base_url().'/files/student_picture/'.$sid.'.png'; } else if(file_exists(FCPATH.'/files/student_picture/'.$sid.'.PNG')) { return base_url().'/files/student_picture/'.$sid.'.PNG'; } else { return base_url().'/files/_default.jpg'; } } function checkUnique($array,$table) { $CI =& get_instance(); if(!is_array($array['field'])){ return $CI->db->get_where($table, array($array['field'] => $array[$array['field']]))->row_array(); }else{ $where=array(); foreach($array['field'] as $k=>$v){ $where[$v]=$array['value'][$k]; } if($where=='') return ""; return $CI->db->get_where($table, $where)->row_array(); } } function file_upload_max_size() { static $max_size = -1; if ($max_size < 0) { // Start with post_max_size. $post_max_size = parse_size(ini_get('post_max_size')); if ($post_max_size > 0) { $max_size = $post_max_size; } // If upload_max_size is less, then reduce. Except if upload_max_size is // zero, which indicates no limit. $upload_max = parse_size(ini_get('upload_max_filesize')); if ($upload_max > 0 && $upload_max < $max_size) { $max_size = $upload_max; } } return humanFileSize($max_size); } function parse_size($size) { $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size. $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size. if ($unit) { // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by. return round($size * pow(1024, stripos('bkmgtpezy', $unit[0]))); } else { return round($size); } } function humanFileSize($size,$unit="") { if( (!$unit && $size >= 1<<30) || $unit == "GB") return number_format($size/(1<<30),2)."GB"; if( (!$unit && $size >= 1<<20) || $unit == "MB") return number_format($size/(1<<20),2)."MB"; if( (!$unit && $size >= 1<<10) || $unit == "KB") return number_format($size/(1<<10),2)."KB"; return number_format($size)." bytes"; }
| ver. 1.4 |
Github
|
.
| PHP 7.2.34 | Generation time: 0.16 |
proxy
|
phpinfo
|
Settings