PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/application/models/Common_model.php

https://bitbucket.org/Yatindra_Mohite/lift
PHP | 400 lines | 284 code | 68 blank | 48 comment | 38 complexity | e66baf52f284896e7e60a3580d3ebc2a MD5 | raw file
Possible License(s): Apache-2.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Common_model extends CI_Model {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. //date_default_timezone_set("Europe/London");
  8. }
  9. public function common_insert($tbl_name = false, $data_array = false)
  10. {
  11. $ins_data = $this->db->insert($tbl_name, $data_array);
  12. if($ins_data){
  13. return $last_id = $this->db->insert_id();
  14. }
  15. else{
  16. return false;
  17. }
  18. }
  19. public function updateData($table,$data,$where_array)
  20. {
  21. $this->db->where($where_array);
  22. if($this->db->update($table,$data)){
  23. //print_r($this->db->last_query()); exit;
  24. return true;
  25. }
  26. else{
  27. //print_r($this->db->last_query()); exit;
  28. return false;
  29. }
  30. }
  31. // Function for select data
  32. public function getData($table,$where='', $order_by = false, $order = false, $join_array = false, $limit = false)
  33. {
  34. //$this->db->select('*');
  35. $this->db->from($table);
  36. if(!empty($where))
  37. {
  38. $this->db->where($where);
  39. }
  40. if(!empty($order_by))
  41. {
  42. $this->db->order_by($order_by, $order);
  43. }
  44. if(!empty($join_array))
  45. {
  46. foreach ($join_array as $key => $value) {
  47. $this->db->join($key, $value);
  48. }
  49. }
  50. if(!empty($limit))
  51. {
  52. $this->db->limit($limit);
  53. }
  54. $result = $this->db->get();
  55. //print_r($this->db->last_query()); exit;
  56. return $result->result();
  57. //return $result;
  58. }
  59. // Function for select data
  60. public function getDataField($field = false, $table, $where='', $order_by = false, $order = false, $join_array = false, $limit = false, $join_type = false )
  61. {
  62. $this->db->select($field);
  63. $this->db->from($table);
  64. if(!empty($where))
  65. {
  66. $this->db->where($where);
  67. }
  68. if(!empty($order_by))
  69. {
  70. $this->db->order_by($order_by, $order);
  71. }
  72. if(!empty($join_array))
  73. {
  74. foreach ($join_array as $key => $value) {
  75. if(!empty($join_type))
  76. $this->db->join($key, $value, 'left');
  77. else
  78. $this->db->join($key, $value);
  79. }
  80. }
  81. if(!empty($limit))
  82. {
  83. $this->db->limit($limit);
  84. }
  85. $result = $this->db->get();
  86. //print_r($this->db->last_query()); exit;
  87. return $result->result();
  88. //return $result;
  89. }
  90. public function common_getRow($tbl_name = flase, $where = false, $join_array = false)
  91. {
  92. $this->db->select('*');
  93. $this->db->from($tbl_name);
  94. if(isset($where) && !empty($where))
  95. {
  96. $this->db->where($where);
  97. }
  98. if(!empty($join_array))
  99. {
  100. foreach($join_array as $key=>$value){
  101. $this->db->join($key,$value);
  102. }
  103. }
  104. $query = $this->db->get();
  105. $data_array = $query->row();
  106. //print_r($this->db->last_query()); exit;
  107. if($data_array)
  108. {
  109. return $data_array;
  110. }
  111. else{
  112. return false;
  113. }
  114. }
  115. public function deleteData($table,$where)
  116. {
  117. $this->db->where($where);
  118. if($this->db->delete($table))
  119. {
  120. return true;
  121. }
  122. else{
  123. return false;
  124. }
  125. }
  126. public function sqlcount($table = false,$where = false)
  127. {
  128. $this->db->select('*');
  129. $this->db->from($table);
  130. if(isset($where) && !empty($where))
  131. {
  132. $this->db->where($where);
  133. }
  134. //$this->db->limit($limit, $start);
  135. $query = $this->db->get();
  136. //print_r($this->db->last_query()); exit;
  137. return $query->num_rows();
  138. }
  139. public function id_encrypt($str = false) // Id Encryption
  140. {
  141. return (56*$str);
  142. }
  143. public function id_decrypt($str = false) // Id Decryption
  144. {
  145. return ($str/56);
  146. }
  147. public function id_encrypt_zub($str = false) // Id Encryption
  148. {
  149. $str=(56*$str)+111;
  150. return ($str);
  151. }
  152. public function id_decrypt_zub($str = false) // Id Decryption
  153. {
  154. $str=($str-111)/56;
  155. return ($str);
  156. }
  157. public function milliseconds() // Id Decryption
  158. {
  159. $str = round(microtime(true) * 1000);
  160. return ($str);
  161. }
  162. public function getAddress($lat, $lon)
  163. {
  164. $url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lon."&sensor=false";
  165. $json = @file_get_contents($url);
  166. $data = json_decode($json);
  167. $status = $data->status;
  168. $address = '';
  169. if($status == "OK")
  170. {
  171. $address = $data->results[0]->formatted_address;
  172. }
  173. return $address;
  174. }
  175. public function randomuniqueCode()
  176. {
  177. $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
  178. $pass = array(); /*remember to declare $pass as an array*/
  179. $alphaLength = strlen($alphabet) - 1; /*put the length -1 in cache*/
  180. for ($i = 0; $i < 6; $i++) {
  181. $n = rand(0, $alphaLength);
  182. $pass[] = $alphabet[$n];
  183. }
  184. return implode($pass); /*turn the array into a string*/
  185. }
  186. public function random_number()
  187. {
  188. $alphabet = "123456789";
  189. $pass = array(); /*remember to declare $pass as an array*/
  190. $alphaLength = strlen($alphabet) - 1; /*put the length -1 in cache*/
  191. for ($i = 0; $i < 4; $i++) {
  192. $n = rand(0, $alphaLength);
  193. $pass[] = $alphabet[$n];
  194. }
  195. return implode($pass); /*turn the array into a string*/
  196. }
  197. function sendPushNotification($registration_ids, $message) ///For Single User*********
  198. {
  199. $url = 'https://fcm.googleapis.com/fcm/send';
  200. $fields = array(
  201. 'registration_ids' => array($registration_ids),
  202. 'data' => $message,
  203. );
  204. $headers = array(
  205. 'Authorization:key=' . GOOGLE_API_KEY,
  206. 'Content-Type: application/json'
  207. );
  208. $ch = curl_init();
  209. curl_setopt($ch, CURLOPT_URL, $url);
  210. curl_setopt($ch, CURLOPT_POST, true);
  211. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  212. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  213. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  214. // curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  215. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  216. $result = curl_exec($ch);
  217. curl_close($ch);
  218. //return $result;
  219. //cho $result; exit;
  220. }
  221. function sendNotification($registration_ids, $message) // For Multiple Users********
  222. {
  223. // echo '<pre>';
  224. // print_r($registration_ids);exit;
  225. $url = 'https://fcm.googleapis.com/fcm/send';
  226. $fields = array(
  227. 'registration_ids' => $registration_ids,
  228. 'data' => $message
  229. );
  230. // echo '<pre>';
  231. // print_r($fields);exit;
  232. $headers = array(
  233. 'Authorization:key=' . GOOGLE_API_KEY,
  234. 'Content-Type: application/json'
  235. );
  236. $ch = curl_init();
  237. curl_setopt($ch, CURLOPT_URL, $url);
  238. curl_setopt($ch, CURLOPT_POST, true);
  239. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  240. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  241. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  242. // curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  243. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  244. $result = curl_exec($ch);
  245. curl_close($ch);
  246. //return $result;
  247. //$result;
  248. }
  249. function encryptor_ym($action, $string) {
  250. $output = false;
  251. $encrypt_method = "AES-256-CBC";
  252. //pls set your unique hashing key
  253. $secret_key = 'muni';
  254. $secret_iv = 'muni123';
  255. // hash
  256. $key = hash('sha256', $secret_key);
  257. // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
  258. $iv = substr(hash('sha256', $secret_iv), 0, 16);
  259. //do the encyption given text/string/number
  260. if( $action == 'encrypt' ) {
  261. $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
  262. $output = base64_encode($output);
  263. }
  264. else if( $action == 'decrypt' ){
  265. //decrypt the given text/string/number
  266. $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
  267. }
  268. return $output;
  269. }
  270. function hour_minute_time($a,$b) {
  271. //get current timestampt
  272. $b = strtotime($b);
  273. //get timestamp when tweet created
  274. $c = strtotime($a);
  275. //get difference
  276. $d = $b - $c;
  277. //calculate different time values
  278. $minute = 60;
  279. $hour = $minute * 60;
  280. $day = $hour * 24;
  281. $week = $day * 7;
  282. if(is_numeric($d) && $d >= 0) {
  283. //if less then 3 seconds
  284. if($d < 3) return "Right now";
  285. //if less then minute
  286. if($d < $minute) return floor($d) . " seconds ago";
  287. //if less then 2 minutes
  288. if($d < $minute * 2) return "about 1 minute ago";
  289. //if less then hour
  290. if($d < $hour) return floor($d / $minute) . " minutes ago";
  291. //if less then 2 hours
  292. if($d < $hour * 2) return "about 1 hour ago";
  293. //if less then day
  294. if($d < $day) return floor($d / $hour) . " hours ago";
  295. //if more then day, but less then 2 days
  296. if($d > $day ) return strtoupper(date("Y M d H:i A",strtotime($a)));
  297. //if less then year
  298. //if($d < $day * 365) return floor($d / $day) . " days ago";
  299. //else return more than a year
  300. return "over a year ago";
  301. }
  302. }
  303. function getrowoffset($latitude,$longitude)
  304. {
  305. $timee = '0';
  306. $timezone = "https://maps.googleapis.com/maps/api/timezone/json?location=".$latitude.",".$longitude."&timestamp=".$timee."&key=AIzaSyAW_luhvBnHnLb8DuPCBV2PsC0Ea0BbX6E";
  307. $a = file_get_contents($timezone);
  308. $p = json_decode($a);
  309. $rowoffset[] = $p->rawOffset;
  310. $rowoffset[] = $p->timeZoneId;
  311. return $rowoffset;
  312. }
  313. function getlocaldatetime($datetime,$rowoffset,$mode) {
  314. //$date = gmdate("Y-m-d H:i", time() + 3600*-$rowoffset/3600);
  315. //$rowoffset = $rowoffset*3600;
  316. if($mode == "+")
  317. $date = date("H:i:s", strtotime($datetime) + $rowoffset);
  318. else
  319. $date = date("H:i:s", strtotime($datetime) - $rowoffset);
  320. /* if ($mode == "+")
  321. $new_datetime = date('Y-m-d H:i',strtotime($datetime) + $rowoffset);
  322. else
  323. $new_datetime = date('Y-m-d H:i',strtotime($datetime) - $rowoffset);*/
  324. return $date;
  325. }
  326. function get_local_time($lat,$lng) //main function for local time
  327. {
  328. $rowoffset = $this->getrowoffset($lat,$lng);
  329. return $rowoffset;
  330. }
  331. }
  332. ?>