if( ! function_exists('dropdown_list') ) { function dropdown_list($table, $where=0, $select='id, name as label', $default="select"){ if($table == "") return false; $CI =& get_instance(); if(is_array($where)) $Rows = $CI->db->select($select)->get_where($table, $where)->result(); else $Rows = $CI->db->select($select)->get($table)->result(); $return = array(); $return[0] = $default; foreach($Rows as $r){ $return[$r->id] = trim($r->label); } return $return; } } if( ! function_exists('get_row') ) { function get_row($table, $where=0, $select="*"){ $CI =& get_instance(); return (is_array($where))? $CI->db->select($select)->get_where($table, $where)->row() : $CI->db->select($select)->get($table)->row(); } } if( ! function_exists('get_results') ) { function get_results($table, $where=0, $select="*", $order=null){ $CI =& get_instance(); if(is_array($order)){ $CI->db->order_by($order['field'], $order['type']); } return (is_array($where))? $CI->db->select($select)->get_where($table, $where)->result() : $CI->db->select($select)->get($table)->result(); } } if( ! function_exists('unique_filename') ) { function unique_filename($fileName, $title='new-file'){ $extensie = strtolower(end(explode('.', $fileName))); return url_title(trim($title), '-', 'dash') . '-' . random_string('numeric', 5). '.' . $extensie; } } if( ! function_exists('delete_item') ) { function delete_item($table, $where=array()){ $CI =& get_instance(); if($table=="") return false; $CI->db->delete($table, $where); } } if( ! function_exists('count_query') ) { function count_query($table, $where) { $CI =& get_instance(); $query = $CI->db->select('1', FALSE)->get_where($table, $where); return $query->num_rows(); } } if( ! function_exists('upload_file') ) { function upload_file($fileName, $path, $newfilename='new-file'){ if($fileName['name'] == "" or $fileName['tmp_name'] == "") return false; if(!is_dir($path)) return false; //$NumeFisier = unique_filename($fileName['name'], $newfilename); $NumeFisier = $fileName['name']; if(@move_uploaded_file($fileName['tmp_name'], $path . $NumeFisier)) return $NumeFisier; return false; } } if( ! function_exists('delete_file') ) { function delete_file($filePath){ if($filePath == "") return false; if(@is_file($filePath)) @unlink($filePath); } } if( ! function_exists("delete_folder") ) { function delete_folder($path) { if(!is_string($path) and empty($path)) return false; if(!is_dir($path)) return false; $d = dir($path); while($entry = $d->read()): if ($entry!= "." && $entry!= "..") unlink($path.'/'.$entry); endwhile; $d->close(); rmdir($path); } } if( ! function_exists('create_folder') ) { function create_folder($path) { if(!is_dir($path)) mkdir($path); } } if( ! function_exists('multiple_upload') ) { function multiple_upload($input_file, $upload_dir = 'uploads', $names=array()) { if(!is_dir($upload_dir)) return false; $files = array(); foreach ($_FILES[$input_file] as $k => $l) { foreach ($l as $i => $v) { if (!array_key_exists($i, $files)) $files[$i] = array(); $files[$i][$k] = $v; } } $return = array(); $k=0; foreach($files as $f) { if($f["name"]!=""){ $filename = (isset($names[$k])) ? unique_filename($f['name'], $names[$k]) : unique_filename($f['name'], 'new-file-'.$k); if(move_uploaded_file($f["tmp_name"], $upload_dir . $filename)){ $return[] = $filename; } } $k++; } return $return; } } if( ! function_exists('send_email') ) { function send_email($to, $from, $subject, $message){ if($to == "" or $from == "") return false; $config = array('protocol'=>'mail', 'charset'=>'UTF-8', 'wordwrap'=>TRUE, 'mailtype'=>'html'); $CI =& get_instance(); $CI->load->library('email', $config); $CI->email->from($from); $CI->email->to($to); $CI->email->subject($subject); $CI->email->message($message); if(@$CI->email->send()) return true; } } if( ! function_exists('get_post_dates')) { function get_post_dates($inputName="post", $fields=array()){ if($inputName == "") return false; return array_intersect_key($_POST[$inputName], array_flip($fields)); } } if( ! function_exists('ExcelExport') ) { //EXCEL EXPORT function ExcelExport($query, $filename, $forcedownload=1) { if(!$query) return false; $CI =& get_instance(); $sql_csv = $CI->db->query($query); if($forecedownload == 1) { header("Content-type:application/ms-excel"); header("Content-Disposition:attachment;filename=$filename.xls"); if($sql_csv->num_rows()>0){ foreach($sql_csv->result_array() as $sql){ print '"' . stripslashes(implode('","',$sql)) . "\"\n"; } } exit; }else{ $return = ""; if($sql_csv->num_rows()>0){ foreach($sql_csv->result_array() as $sql){ $return .= '"' . stripslashes(implode('","',$sql)) . "\"\n"; } } return $return; } } } if( ! function_exists('resize_image')) { function resize_image($source, $dest, $max_width, $max_height){ $dstx = $max_width; $dsty = $max_height; $size = getimagesize($source); $width = $size[0]; $height = $size[1]; $proportion_X = $width / $dstx; $proportion_Y = $height / $dsty; if($proportion_X > $proportion_Y ){ $proportion = $proportion_Y; }else{ $proportion = $proportion_X ; } $target['width'] = $dstx * $proportion; $target['height'] = $dsty * $proportion; $original['diagonal_center'] = round(sqrt(($width*$width)+($height*$height))/2); $target['diagonal_center'] = round(sqrt(($target['width']*$target['width'])+ ($target['height']*$target['height']))/2); $crop = round($original['diagonal_center'] - $target['diagonal_center']); if($proportion_X < $proportion_Y ){ $target['x'] = 0; $target['y'] = round((($height/2)*$crop)/$target['diagonal_center']); }else{ $target['x'] = round((($width/2)*$crop)/$target['diagonal_center']); $target['y'] = 0; } $new_im = ImageCreatetruecolor($dstx,$dsty); $ext = strtolower(end(explode(".", $dest))); switch($ext){ case 'jpg': $im = imagecreatefromjpeg($source); imagecopyresampled($new_im, $im, 0, 0, $target['x'], $target['y'], $dstx, $dsty, $target['width'], $target['height']); imagejpeg($new_im,$dest,100); break; case 'jpeg': $im = imagecreatefromjpeg($source); imagecopyresampled($new_im, $im, 0, 0, $target['x'], $target['y'], $dstx, $dsty, $target['width'], $target['height']); imagejpeg($new_im,$dest,100); break; case 'png': $im = imagecreatefrompng($source); imagecolortransparent($new_im, imagecolorallocatealpha($new_im, 0, 0, 0, 127)); imagealphablending($new_im, false); imagesavealpha($new_im, true); imagecopyresampled($new_im, $im, 0, 0, $target['x'], $target['y'], $dstx, $dsty, $target['width'], $target['height']); imagepng($new_im,$dest); break; } } } if( ! function_exists('pdf_create') ) { function pdf_create($html, $filename='', $stream=TRUE) { require_once("application/libraries/dompdf/dompdf_config.inc.php"); $dompdf = new DOMPDF(); $script = ''; $html = str_replace('
', ''.$script, $html); $dompdf->load_html($html); $dompdf->render(); if ($stream) { $dompdf->stream($filename.".pdf"); } else { return $dompdf->output(); } } } if( ! function_exists('show_picture')) { function show_picture($source, $width=0, $height=0, $other='', $else=''){ $img = ''; if($width >0) $img .= ' width="'.$width.'" '; if($height >0) $img .= ' height="'.$height.'" '; if($source == "") return false; if(is_file($source)){ return '