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

/application/core/MY_Model.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 1285 lines | 896 code | 205 blank | 184 comment | 102 complexity | 859d1c6f47ecba94879ff92962a91d22 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. require APPPATH . 'controllers/cache_controller.php';
  3. class MY_Model extends DataMapper
  4. {
  5. public $cache_id = '';
  6. public $dm_object = null;
  7. function __construct($id = null)
  8. {
  9. $cache = new Cache_Controller();
  10. $this -> cache = $cache -> getCache();
  11. parent::__construct($id);
  12. $this -> dm_object = '';
  13. }
  14. protected function create_db()
  15. {
  16. $CI = &get_instance();
  17. $CI -> teamleaf -> create_db($this -> table, $this -> has_one, $this -> has_many);
  18. }
  19. /*
  20. public function getTimelineTransaction($transactionID, $cacheType = 'file', $recreate = true)
  21. {
  22. $CI = &get_instance();
  23. $this -> cache_id = 'view/tasktimelines/[View Cache - Timeline-' . $transactionID . ']';
  24. $limit = 5;
  25. switch($cacheType)
  26. {
  27. // START - FILE CACHE
  28. // ------------------------------------------------------------------------------------
  29. case 'file' :
  30. $cacheLength = 3600;
  31. $cache = $this -> cache -> file -> get($this -> cache_id);
  32. if ($cache && !$recreate)
  33. {
  34. $CI -> firephp -> info("Timeline Cache Loaded for: " . $this -> cache_id);
  35. return $cache;
  36. }
  37. else
  38. {
  39. $CI -> firephp -> info("Creating Timeline Cache for: " . $this -> cache_id);
  40. $timeline = new Timeline();
  41. $view = $timeline -> createTimelineTransactionMarkup($transactionID);
  42. $this -> cache -> file -> save($this -> cache_id, $view, $cacheLength);
  43. return $view;
  44. }
  45. }
  46. }
  47. */
  48. public function getObjectv2($array, $model, $id = null, $parentObject = null, $cacheType = 'file')
  49. {
  50. /*****
  51. *
  52. * Formatting notes
  53. * ----------------
  54. *
  55. * WHERE
  56. * Pass an Array of
  57. * $arr['field'] = field name to check against
  58. * $arr['value] = value
  59. *
  60. * WHERE IN
  61. * Pass an Array of
  62. * $arr['field'] = field name to check against
  63. * $arr['values] Aarray of values (key not required)
  64. *
  65. *
  66. */
  67. $CI = &get_instance();
  68. // BUILD CACHE ID
  69. // ------------------------------------------------------------------------------------
  70. $this -> cache_id = 'database/[' . $model . '-' . $id . ']';
  71. if ($parentObject)
  72. {
  73. $parent = $parentObject -> model;
  74. $this -> cache_id = 'database/[' . $model . '-' . $id . ']';
  75. $this -> cache_id .= '[ON_PARENT-' . $parent . '-with-ID-of-' . $parentObject -> id . ']';
  76. }
  77. foreach ($array as $key => $value)
  78. {
  79. //$CI -> firephp -> info("ARRAY VALUES: " . $key .'----'.$value);
  80. switch($key)
  81. {
  82. case 'where' :
  83. if (is_array($value))
  84. {
  85. $this -> cache_id .= '___Where--';
  86. foreach ($value as $sk => $sv)
  87. {
  88. $this -> cache_id .= $sk . '-' . $sv . '-';
  89. }
  90. }
  91. break;
  92. case 'or_where' :
  93. if (is_array($value))
  94. {
  95. //print_r($value);
  96. $this -> cache_id .= '___OrWhere--';
  97. foreach ($value['values'] as $svk => $svV)
  98. {
  99. $this -> cache_id .= '-' . $svk . '-' . $svV . '-';
  100. }
  101. }
  102. break;
  103. case 'or_like' :
  104. if (is_array($value))
  105. {
  106. //print_r($value);
  107. $this -> cache_id .= '___OrLike--' . $value['field'] . '-';
  108. foreach ($value['values'] as $svk => $svV)
  109. {
  110. $this -> cache_id .= '-' . $svk . '-' . $svV . '-';
  111. }
  112. }
  113. break;
  114. case 'wherein' :
  115. if (is_array($value))
  116. {
  117. $this -> cache_id .= '___WhereIn--' . $value['field'] . '-hasTheValues-';
  118. foreach ($value['values'] as $whereinvals)
  119. {
  120. $this -> cache_id .= $whereinvals . '-or-';
  121. }
  122. }
  123. break;
  124. case 'wherein2' :
  125. if (is_array($value))
  126. {
  127. $this -> cache_id .= '___WhereIn2--' . $value['field'] . '-hasTheValues-';
  128. foreach ($value['values'] as $whereinvals)
  129. {
  130. $this -> cache_id .= $whereinvals . '-or-';
  131. }
  132. }
  133. break;
  134. case 'sort' :
  135. if (is_array($value))
  136. {
  137. $this -> cache_id .= '___Orderby--';
  138. foreach ($value as $sk => $sv)
  139. {
  140. $this -> cache_id .= $sk . '-' . $sv . '-';
  141. }
  142. }
  143. break;
  144. case 'limit' :
  145. $this -> cache_id .= '___Limit--' . $value;
  146. break;
  147. case 'groupby' :
  148. $this -> cache_id .= '___Groupby--' . $value;
  149. break;
  150. case 'orderby' :
  151. $this -> cache_id .= '___Orderby--';
  152. foreach ($value as $orderKey => $orderValue)
  153. {
  154. $this -> cache_id .= $orderKey . '=' . $orderValue . '-';
  155. }
  156. break;
  157. }
  158. }
  159. // END -- BUILD CACHE ID
  160. // ------------------------------------------------------------------------------------
  161. switch($cacheType)
  162. {
  163. case 'db' :
  164. $CI -> firephp -> warn("Forced DB: " . $this -> cache_id);
  165. if ($parentObject)
  166. {
  167. $parentObject -> $model -> get();
  168. $this -> dm_object = $parentObject -> $model;
  169. }
  170. else
  171. {
  172. $this -> dm_object -> get();
  173. }
  174. break;
  175. // START - FILE CACHE
  176. // ------------------------------------------------------------------------------------
  177. case 'file' :
  178. $cacheLength = 3600;
  179. $cache = $this -> cache -> file -> get($this -> cache_id);
  180. if ($cache)
  181. {
  182. $CI -> firephp -> info("File Cache Loaded for: " . $this -> cache_id);
  183. $this -> dm_object = new $model();
  184. $this -> dm_object = $this -> dm_object -> all_from_array($cache);
  185. }
  186. else
  187. {
  188. if ($parentObject)
  189. {
  190. foreach ($array as $key => $value)
  191. {
  192. switch($key)
  193. {
  194. case 'where' :
  195. $this -> addWhereClause($parentObject -> $model, $value);
  196. break;
  197. case 'or_where' :
  198. $this -> addOrWhereClause($parentObject -> $model, $value);
  199. break;
  200. case 'or_like' :
  201. $this -> addOrLikeClause($parentObject -> $model, $value);
  202. break;
  203. case 'wherein' :
  204. $this -> addWhereInClause($parentObject -> $model, $value);
  205. break;
  206. case 'wherein2' :
  207. $this -> addWhereInClause($parentObject -> $model, $value);
  208. break;
  209. case 'sort' :
  210. $this -> addSortClause($parentObject -> $model, $value);
  211. break;
  212. case 'limit' :
  213. $this -> addLimitClause($parentObject -> $model, $value);
  214. break;
  215. case 'groupby' :
  216. $this -> addGroupByClause($parentObject -> $model, $value);
  217. break;
  218. case 'orderby' :
  219. $CI -> firephp -> info("ORDER BY CALLED");
  220. $this -> addOrderByClause($parentObject -> $model, $value);
  221. break;
  222. }
  223. }
  224. if ($id)
  225. {
  226. $CI -> firephp -> info("ID PASSED");
  227. $this -> addWhereClause($parentObject -> model, array('id' => $id));
  228. }
  229. $parentObject -> $model -> get();
  230. $this -> dm_object = $parentObject -> $model;
  231. $arr = $this -> dm_object -> all_to_array();
  232. $this -> cache -> file -> save($this -> cache_id, $arr, $cacheLength);
  233. $this -> dm_object = $parentObject -> $model;
  234. $CI -> firephp -> info("No Cache for: " . $this -> cache_id . ' A cache has been created');
  235. }
  236. // NO PARENT
  237. else
  238. {
  239. $this -> dm_object = new $model();
  240. if ($id)
  241. {
  242. $CI -> firephp -> info("ID PASSED");
  243. $this -> addWhereClause($this -> dm_object, array('id' => $id));
  244. }
  245. foreach ($array as $key => $value)
  246. {
  247. switch($key)
  248. {
  249. case 'where' :
  250. $this -> addWhereClause($this -> dm_object, $value);
  251. break;
  252. case 'or_where' :
  253. $this -> addOrWhereClause($this -> dm_object, $value);
  254. break;
  255. case 'or_like' :
  256. $this -> addOrLikeClause($this -> dm_object, $value);
  257. break;
  258. case 'wherein' :
  259. $this -> addWhereInClause($this -> dm_object, $value);
  260. break;
  261. case 'wherein2' :
  262. $this -> addWhereInClause($this -> dm_object, $value);
  263. break;
  264. case 'sort' :
  265. $this -> addSortClause($this -> dm_object, $value);
  266. break;
  267. case 'limit' :
  268. $this -> addLimitClause($this -> dm_object, $value);
  269. break;
  270. case 'groupby' :
  271. $this -> addGroupByClause($this -> dm_object, $value);
  272. break;
  273. case 'orderby' :
  274. $this -> addOrderByClause($this -> dm_object, $value);
  275. break;
  276. }
  277. }
  278. $this -> dm_object -> get();
  279. $arr = $this -> dm_object -> all_to_array();
  280. $this -> cache -> file -> save($this -> cache_id, $arr, $cacheLength);
  281. $CI -> firephp -> info("No Cache for: " . $this -> cache_id . ' A cache has been created');
  282. //$CI -> firephp -> info('LAST:'. $this -> dm_object -> check_last_query());
  283. }
  284. }
  285. break;
  286. }
  287. // return this , which includes the dm_object
  288. $this -> dm_object -> cache_id = $this -> cache_id;
  289. return $this;
  290. }
  291. /**
  292. * Get a data mapper, if it is cached return the cached version!
  293. *
  294. * @param String $model
  295. * @param Int $id
  296. * @param DataMapper $parentbject
  297. * @param Boolean $from_db
  298. * @param Array $where
  299. */
  300. public function getObject($model, $id, DataMapper $parentObject = null, $from_db = false, $where = '', $sort = '', $whereInObject = null, $limit = null, $whereIn = null)
  301. {
  302. $CI = &get_instance();
  303. if (!$parentObject)
  304. {
  305. $this -> cache_id = 'database/[' . $model . '-' . $id . ']';
  306. if (is_array($where))
  307. {
  308. $this -> cache_id .= '___WHERE_' . implode('_', $where) . '_' . implode('_', array_flip($where));
  309. }
  310. if (is_array($whereIn))
  311. {
  312. $str = '___WHERE-IN__field-' . $whereIn['field'];
  313. foreach ($whereIn['values'] as $v)
  314. {
  315. $str .= '_id-' . $v;
  316. }
  317. $this -> cache_id .= $str;
  318. }
  319. if (is_array($whereInObject))
  320. {
  321. if (isset($whereInObject['object']) && isset($whereInObject['column']) && isset($whereInObject['value']))
  322. {
  323. // do stuff
  324. $whereinmodel = $whereInObject['object'];
  325. @$this -> cache_id .= '___WHEREIN_' . $whereinmodel . '__column-' . $whereInObject['column'] . '_value-' . $whereInObject['value'];
  326. }
  327. else
  328. {
  329. $CI -> firephp -> error("Error - included related used, or bad where in called - this is not supported. Cache id so far: " . $this -> cache_id);
  330. //$from_db = true;
  331. }
  332. }
  333. if ($sort)
  334. {
  335. $this -> cache_id .= '___SORT_' . '_' . implode('_', array_flip($sort));
  336. }
  337. if ($limit)
  338. {
  339. $this -> cache_id .= '___LIMIT_' . $limit;
  340. }
  341. //check the cache
  342. if ($from_db)
  343. {
  344. $CI -> firephp -> warn("No Cache. Forced DB [P]: " . $this -> cache_id);
  345. $this -> dm_object = new $model($id);
  346. if (is_array($sort))
  347. {
  348. $this -> addSortClause($this -> dm_object, $sort);
  349. }
  350. if (is_array($where))
  351. {
  352. $this -> addWhereClause($this -> dm_object, $where);
  353. }
  354. if (is_array($whereIn))
  355. {
  356. $this -> addWhereInClause($this -> dm_object, $whereIn);
  357. }
  358. if ($limit)
  359. {
  360. $this -> addLimitClause($this -> dm_object, $limit);
  361. }
  362. if (is_array($whereInObject))
  363. {
  364. if (isset($whereInObject['object']) && isset($whereInObject['column']) && isset($whereInObject['value']))
  365. {
  366. $this -> addWhereInRelatedClause($this -> dm_object, $whereInObject);
  367. }
  368. else
  369. {
  370. $CI -> firephp -> error("Error - included related used, or bad where in called - this is not supported. Cache id so far: " . $this -> cache_id);
  371. }
  372. }
  373. //echo('<div class="cachefeedback"> Calling from the DB for the '. $model .' <br>ID '.$id.' Cache ID: '.$this->cache_id.'</div>');
  374. $this -> dm_object -> get();
  375. }
  376. else
  377. {
  378. $cache = $this -> cache -> file -> get($this -> cache_id);
  379. if ($cache)
  380. {
  381. //$cache = unserialize($cache);
  382. $CI -> firephp -> info("Cache Loaded [P]: " . $this -> cache_id);
  383. //$CI->firephp->info("Cache Type: " . $cache);
  384. $this -> dm_object = new $model();
  385. //$CI->firephp->info("Model: " . $model);
  386. //$CI->firephp->info("Cache RAW for ".$this->cache_id." : " . print_r($cache, true));
  387. $this -> dm_object = $this -> dm_object -> all_from_array($cache);
  388. //$CI->firephp->info("Cache DM: " . print_r($this->dm_object, true));
  389. //$this->dm_object = $tempModel;
  390. //$CI->firephp->info("CACHE: ".$this->dm_object->all);
  391. }
  392. else
  393. {
  394. $CI -> firephp -> info("No Cache. Getting and creating [P]: " . $this -> cache_id);
  395. //echo( '<hr> Creating a cache for model '. $model .' <br>ID '.$id );
  396. // we DO NOT have a cache...
  397. $this -> dm_object = new $model();
  398. if ($id)
  399. {
  400. $this -> addWhereClause($this -> dm_object, array('id' => $id));
  401. }
  402. if (is_array($sort))
  403. {
  404. $this -> addSortClause($this -> dm_object, $sort);
  405. }
  406. if (is_array($where))
  407. {
  408. $this -> addWhereClause($this -> dm_object, $where);
  409. }
  410. if (is_array($whereIn))
  411. {
  412. $this -> addWhereInClause($this -> dm_object, $whereIn);
  413. }
  414. if ($limit)
  415. {
  416. $this -> addLimitClause($this -> dm_object, $limit);
  417. }
  418. if (is_array($whereInObject))
  419. {
  420. if (isset($whereInObject['object']) && isset($whereInObject['column']) && isset($whereInObject['value']))
  421. {
  422. $this -> addWhereInRelatedClause($this -> dm_object, $whereInObject);
  423. }
  424. else
  425. {
  426. $CI -> firephp -> error("Error - included related used, or bad where in called - this is not supported. Cache id so far: " . $this -> cache_id);
  427. }
  428. }
  429. $this -> dm_object -> get();
  430. //
  431. $arr = $this -> dm_object -> all_to_array();
  432. //print_r($arr);
  433. //$CI->firephp->info("Cache save: ".$this->cache_id." : " . print_r($arr, true));
  434. $this -> cache -> file -> save($this -> cache_id, $arr, 3600);
  435. //echo('<div class="cachefeedback"> Creating a cache for the '. $model .' <br>ID '.$id.' Cache ID: '.$this->cache_id.'</div>');
  436. //$CI->firephp->info("NO CACHE: ".$this->dm_object->all);
  437. }
  438. }
  439. // datamapper is inside;
  440. //$CI->firephp->info("Returning -this- Marker 1: " );
  441. return $this;
  442. }
  443. else
  444. {
  445. $parent = $parentObject -> model;
  446. $this -> cache_id = 'database/[' . $model . '-' . $id . ']';
  447. $this -> cache_id .= '___ON_PARENT_' . $parent . '-with-ID-of-' . $parentObject -> id;
  448. if (is_array($where))
  449. {
  450. @$this -> cache_id .= '___WHERE_' . implode('_', $where) . '_' . implode('_', array_flip($where));
  451. }
  452. if (is_array($whereIn))
  453. {
  454. $str = '___WHERE-IN__field-' . $whereIn['field'];
  455. foreach ($whereIn['values'] as $v)
  456. {
  457. $str .= '_id-' . $v;
  458. }
  459. $this -> cache_id .= $str;
  460. }
  461. if (is_array($whereInObject))
  462. {
  463. if (isset($whereInObject['object']) && isset($whereInObject['column']) && isset($whereInObject['value']))
  464. {
  465. $CI -> firephp -> error("Error - WHERE IN cannot be called on a parent");
  466. }
  467. else
  468. {
  469. $CI -> firephp -> error("Error - included related used, or bad where in called - this is not supported. Cache id so far: " . $this -> cache_id);
  470. }
  471. }
  472. if ($sort)
  473. {
  474. $this -> cache_id .= '___SORT_' . '_' . implode('_', array_flip($sort));
  475. }
  476. if ($limit)
  477. {
  478. $this -> cache_id .= '___LIMIT_' . implode('_', $limit);
  479. }
  480. //echo $this->cache_id.'<br/>';
  481. //echo $this->cache->file->get( $this->cache_id );
  482. // cache id based on model parent combo
  483. if ($from_db)
  484. {
  485. $CI -> firephp -> info("No Cache. Forced DB: " . $this -> cache_id);
  486. //$parentObject->$model->get();
  487. if (is_array($where))
  488. {
  489. $this -> addWhereClause($parentObject -> $model, $where);
  490. }
  491. if (is_array($whereIn))
  492. {
  493. $this -> addWhereInClause($this -> dm_object, $whereIn);
  494. }
  495. if (is_array($sort))
  496. {
  497. $this -> addSortClause($this -> dm_object, $sort);
  498. }
  499. if ($limit)
  500. {
  501. $this -> addLimitClause($this -> dm_object, $limit);
  502. }
  503. $parentObject -> $model -> get();
  504. $this -> dm_object = $parentObject -> $model;
  505. //echo('<div class="cachefeedback"> Calling from DB for the '. $model .' <br>ID '.$id.' Parent model ' . $parent.'</div>');
  506. }
  507. if ($cache = $this -> cache -> file -> get($this -> cache_id))
  508. {
  509. $CI -> firephp -> info("Cache Loaded: " . $this -> cache_id);
  510. $this -> dm_object = new $model();
  511. //$CI->firephp->info("Model: " . $model);
  512. //$CI->firephp->info("Cache RAW for ".$this->cache_id." : " . print_r($cache, true));
  513. $this -> dm_object -> all_from_array($cache, '');
  514. //$CI->firephp->info("ID: " . $this->dm_object->id);
  515. }
  516. else
  517. {
  518. $CI -> firephp -> info("No Cache. Getting and creating: " . $this -> cache_id);
  519. //echo 'failled to load cache';
  520. //$parentObject->$model->get();
  521. if (is_array($where))
  522. {
  523. $this -> addWhereClause($parentObject -> $model, $where);
  524. }
  525. if (is_array($whereIn))
  526. {
  527. $this -> addWhereInClause($parentObject -> $model, $whereIn);
  528. }
  529. if (is_array($sort))
  530. {
  531. $this -> addSortClause($parentObject -> $model, $sort);
  532. }
  533. if ($limit)
  534. {
  535. $this -> addLimitClause($parentObject -> $model, $limit);
  536. }
  537. $parentObject -> $model -> get();
  538. $this -> dm_object = $parentObject -> $model;
  539. // Save the cache file
  540. $arr = $this -> dm_object -> all_to_array();
  541. $this -> cache -> file -> save($this -> cache_id, $arr, 3600);
  542. }
  543. return $this;
  544. }
  545. }
  546. private function addWhereClause(DataMapper &$dm_object, array $where)
  547. {
  548. foreach ($where as $field => $condition)
  549. {
  550. $dm_object -> where($field, $condition);
  551. }
  552. //print_R( get_class_methods( $dm_object) );
  553. //print_R( $dm_object->get_sql() );
  554. return true;
  555. }
  556. private function addOrWhereClause(DataMapper &$dm_object, array $where)
  557. {
  558. $CI = &get_instance();
  559. $count = 1;
  560. $CI -> firephp -> error("Error - OR WHERE");
  561. $dm_object -> group_start();
  562. foreach ($where['values'] as $svK => $svV)
  563. {
  564. if ($count)
  565. {
  566. log_message("error", '<br/>WHERE ' . $svK . ' is ' . $svV);
  567. $dm_object -> where($svK, $svV);
  568. $count = 0;
  569. }
  570. else
  571. {
  572. log_message("error", '<br/>OR WHERE ' . $svK . ' is ' . $svV);
  573. $dm_object -> or_where($svK, $svV);
  574. }
  575. }
  576. $dm_object -> group_end();
  577. return true;
  578. }
  579. private function addOrLikeClause(DataMapper &$dm_object, array $where)
  580. {
  581. $CI = &get_instance();
  582. $count = 1;
  583. $CI -> firephp -> error("Error - OR LIKE");
  584. $dm_object -> group_start();
  585. foreach ($where['values'] as $svK => $svV)
  586. {
  587. if ($count)
  588. {
  589. log_message("error", '<br/>LIKE ' . $svK . ' is ' . $svV);
  590. $dm_object -> like($svK, $svV);
  591. $count = 0;
  592. }
  593. else
  594. {
  595. log_message("error", '<br/>OR LIKE ' . $svK . ' is ' . $svV);
  596. $dm_object -> or_like($svK, $svV);
  597. }
  598. }
  599. $dm_object -> group_end();
  600. return true;
  601. }
  602. private function addWhereInClause(DataMapper &$dm_object, array $whereIn)
  603. {
  604. $field = $whereIn['field'];
  605. $values = $whereIn['values'];
  606. $dm_object -> where_in($field, $values);
  607. return true;
  608. }
  609. private function addWhereInRelatedClause(DataMapper &$dm_object, array $wherein)
  610. {
  611. $dm_object -> where_in_related($wherein['object'], $wherein['column'], $wherein['value']);
  612. //print_R( get_class_methods( $dm_object) );
  613. //print_R( $dm_object->get_sql() );
  614. return true;
  615. }
  616. private function addSortClause(DataMapper &$dm_object, array $sort)
  617. {
  618. foreach ($sort as $field => $condition)
  619. {
  620. $dm_object -> order_by($field, $condition);
  621. }
  622. //print_R( get_class_methods( $dm_object) );
  623. //print_R( $dm_object->get_sql() );
  624. return true;
  625. }
  626. private function addLimitClause(DataMapper &$dm_object, $limit)
  627. {
  628. $dm_object -> limit($limit);
  629. //print_R( get_class_methods( $dm_object) );
  630. //print_R( $dm_object->get_sql() );
  631. return true;
  632. }
  633. private function addOrderByClause(DataMapper &$dm_object, $arr)
  634. {
  635. $CI = &get_instance();
  636. foreach ($arr as $key => $value)
  637. {
  638. $CI -> firephp -> info("OREDERBY INSIDE: ARRAY VALUES: " . $key . '----' . $value);
  639. $dm_object -> order_by($key, $value);
  640. }
  641. //print_R( get_class_methods( $dm_object) );
  642. //print_R( $dm_object->get_sql() );
  643. return true;
  644. }
  645. private function addGroupByClause(DataMapper &$dm_object, $group)
  646. {
  647. $dm_object -> group_by($group);
  648. //print_R( get_class_methods( $dm_object) );
  649. //print_R( $dm_object->get_sql() );
  650. return true;
  651. }
  652. private function addIncludeRelatedClause(DataMapper &$dm_object, array $include_related)
  653. {
  654. foreach ($include_related as $condition)
  655. {
  656. $dm_object -> include_related($condition, '*', true, true);
  657. }
  658. //echo $field;
  659. //print_R( get_class_methods( $dm_object) );
  660. //print_R( $dm_object->get_sql() );
  661. return true;
  662. }
  663. final function removeCachedObject($model, $id, DataMapper $parentObject = null, $where = '', $sort = '', $include_related = '', $limit = '')
  664. {
  665. $CI = &get_instance();
  666. if (!$parentObject)
  667. {
  668. $this -> cache_id = 'database/[' . $model . '-' . $id . ']';
  669. if (is_array($where))
  670. {
  671. @$this -> cache_id .= '___WHERE_' . implode('_', $where) . '_' . implode('_', array_flip($where));
  672. }
  673. if (is_array($include_related))
  674. {
  675. @$this -> cache_id .= '___INCLUDERELATED_' . implode('_', $include_related) . '_' . implode('_', array_flip($include_related));
  676. }
  677. if ($sort)
  678. {
  679. $this -> cache_id .= '___SORT_' . '_' . implode('_', array_flip($sort));
  680. }
  681. if ($limit)
  682. {
  683. $this -> cache_id .= '___LIMIT_' . $limit;
  684. }
  685. }
  686. else
  687. {
  688. $parent = $parentObject -> model;
  689. $this -> cache_id = 'database/[' . $model . '-' . $id . ']';
  690. $this -> cache_id .= '___ON_PARENT_' . $parent . '-with-ID-of-' . $parentObject -> id;
  691. if (is_array($where))
  692. {
  693. @$this -> cache_id .= '___WHERE_' . implode('_', $where) . '_' . implode('_', array_flip($where));
  694. }
  695. if (is_array($include_related))
  696. {
  697. @$this -> cache_id .= '___INCLUDERELATED_' . implode('_', $include_related) . '_' . implode('_', array_flip($include_related));
  698. }
  699. if ($sort)
  700. {
  701. $this -> cache_id .= '___SORT_' . '_' . implode('_', array_flip($sort));
  702. }
  703. if ($limit)
  704. {
  705. $this -> cache_id .= '___LIMIT_' . $limit;
  706. }
  707. }
  708. //$CI->firephp->error("Cache about to be deleted: ".$this->cache_id);
  709. //log_message('error', "Cache about to be deleted: ".$this->cache_id);
  710. if (@$this -> cache -> file -> delete($this -> cache_id))
  711. {
  712. $CI -> firephp -> info("Cache deleted successfully: " . $this -> cache_id);
  713. return true;
  714. }
  715. else
  716. {
  717. $CI -> firephp -> error("Cache failed to delete: " . $this -> cache_id);
  718. return false;
  719. }
  720. }
  721. function dm_timestamp()
  722. {
  723. $timestamp = (DataMapper::$config['local_time']) ? date(DataMapper::$config['timestamp_format']) : gmdate(DataMapper::$config['timestamp_format']);
  724. return (DataMapper::$config['unix_timestamp']) ? strtotime($timestamp) : $timestamp;
  725. }
  726. function timeline($object, $userID, $timestamp, $data, $newCreation = false)
  727. {
  728. //log_message('error', 'Timeline Call with Object of:'.get_class($object).' $data of: '.get_class($data));
  729. //log_message('error', '$data of: '.$data->exists());
  730. if ($data && strtolower(get_class($data)) != 'my_model')
  731. {
  732. log_message('error', 'we have data and $data is not my_model');
  733. log_message('error', '$data is: ' . get_class($data) . ' with id of: ' . $data -> id);
  734. log_message('error', '$object is: ' . get_class($object) . ' with id of: ' . $object -> id);
  735. $dataModelName = get_class($data);
  736. if (0)//$object -> is_related_to($dataModelName, $data->id))
  737. {
  738. // already related, no change
  739. log_message('error', 'already related, no change');
  740. }
  741. else
  742. {
  743. log_message('error', 'not related, in else');
  744. $modelName = get_class($object);
  745. log_message('error', 'we have model name: ' . $modelName);
  746. $transactionID = uniqid(rand(), true);
  747. //log_message('error', 'Data is??? ' . get_class($data));
  748. $tr = new Timeline();
  749. $tr -> related_model = get_class($data);
  750. $tr -> related_model_id = $data -> id;
  751. $tr -> model = $modelName;
  752. $tr -> model_id = $object -> id;
  753. $tr -> updated_by = $userID;
  754. $tr -> updated = $timestamp;
  755. $tr -> created = $timestamp;
  756. $tr -> created_by = $userID;
  757. $tr -> transactionID = $transactionID;
  758. $tr -> type = 'relationship';
  759. log_message('error', 'timeline - about to save');
  760. $tr -> save();
  761. log_message('error', 'change - timeline changed');
  762. $newTimelineMarkup = $tr -> createTimelineTransactionMarkup($tr);
  763. }
  764. }
  765. else
  766. {
  767. //log_message('error', 'invlaid data, no relation');
  768. $modelName = get_class($object);
  769. $oldObject = new $modelName($object -> id);
  770. $transactionID = uniqid(rand(), true);
  771. // AUDIT TABKEL
  772. $fields = $this -> db -> list_fields($object -> table);
  773. //log_message('error', 'OldObject Exists?: ' . $oldObject -> exists());
  774. //log_message('error', 'OldObject ID?: ' . $oldObject -> id);
  775. //log_message('error', 'OldObject Updated?: ' . $oldObject -> updated);
  776. // If it has never been updated before, it must be a new one (for some reason the object already exists, when using DMZ-fromarray)
  777. if (!$newCreation)
  778. {
  779. foreach ($fields as $field)
  780. {
  781. //log_message('error', $object -> $field . '<Object oldObject->' . $oldObject -> $field . ' Field->' . $field);
  782. if ($object -> $field != $oldObject -> $field)
  783. {
  784. // We have a change!
  785. $t = new Timeline();
  786. $t -> oldValue = (string)$oldObject -> $field;
  787. $t -> newValue = $object -> $field;
  788. $t -> field = $field;
  789. $t -> type = 'update';
  790. $t -> transactionID = $transactionID;
  791. $t -> model = $modelName;
  792. $t -> model_id = $object -> id;
  793. if ($data)
  794. {
  795. $t -> related_model = get_class($data);
  796. $t -> related_model_id = $data -> id;
  797. }
  798. $t -> updated_by = $userID;
  799. $t -> updated = $timestamp;
  800. $t -> created = $timestamp;
  801. $t -> created_by = $userID;
  802. $t -> save();
  803. $ti = new Timeline();
  804. $newTimelineMarkup = $ti -> createTimelineTransactionMarkup($t);
  805. }
  806. }
  807. }
  808. else
  809. {
  810. // This is new creation
  811. $t = new Timeline();
  812. $t -> type = 'creation';
  813. $t -> transactionID = $transactionID;
  814. $t -> model = $modelName;
  815. $t -> model_id = $object -> id;
  816. $t -> updated_by = $userID;
  817. $t -> updated = $timestamp;
  818. $t -> created = $timestamp;
  819. $t -> created_by = $userID;
  820. $t -> save();
  821. $ti = new Timeline();
  822. $newTimelineMarkup = $ti -> createTimelineTransactionMarkup($t);
  823. }
  824. }
  825. }
  826. /**
  827. * Sorta overload, so we can add some custom save fields
  828. *
  829. * @param unknown_type $data
  830. */
  831. function save($data = null)
  832. {
  833. //print_r($data);
  834. $this -> CI = get_instance();
  835. $this -> user = $this -> CI -> user;
  836. $timestamp = $this -> dm_timestamp();
  837. //(DataMapper::$config['local_time']) ? date(DataMapper::$config['timestamp_format']) : gmdate(DataMapper::$config['timestamp_format']);
  838. if ($this -> id)
  839. {
  840. $this -> updated_by = $this -> user -> id;
  841. $this -> updated = $timestamp;
  842. }
  843. else
  844. {
  845. $this -> created = $timestamp;
  846. $this -> created_by = $this -> user -> id;
  847. }
  848. // Check table fields are all there.......
  849. if (ENVIRONMENT == 'development')
  850. {
  851. $fields = array();
  852. $ok = true;
  853. if (!$this -> db -> field_exists('updated', $this -> table))
  854. {
  855. $ok = false;
  856. log_message('error', 'updated field did not exist.');
  857. $fields['updated'] = array('type' => 'datetime', );
  858. }
  859. if (!$this -> db -> field_exists('updated_by', $this -> table))
  860. {
  861. $ok = false;
  862. log_message('error', 'updated by field did not exist.');
  863. $fields['updated_by'] = array(
  864. 'type' => 'INT',
  865. 'constraint' => 6,
  866. );
  867. }
  868. if (!$this -> db -> field_exists('created', $this -> table))
  869. {
  870. $ok = false;
  871. log_message('error', 'created field did not exist.');
  872. $fields['created'] = array('type' => 'datetime', );
  873. }
  874. if (!$this -> db -> field_exists('created_by', $this -> table))
  875. {
  876. $ok = false;
  877. log_message('error', 'created by field did not exist.');
  878. $fields['created_by'] = array(
  879. 'type' => 'INT',
  880. 'constraint' => 6,
  881. );
  882. }
  883. if (!$ok)
  884. {
  885. log_message('error', 'Updating Table: ' . $this -> table . ' Adding fields: ' . print_r($fields, 1));
  886. $this -> CI -> load -> dbforge();
  887. $this -> CI -> dbforge -> add_column($this -> table, $fields);
  888. }
  889. }
  890. // End developement check
  891. // Get Changes
  892. if ($this -> table != 'timelines')
  893. {
  894. if ($this -> created && $this -> updated)
  895. {
  896. $this -> timeline($this, $this -> user -> id, $timestamp, $data);
  897. $res = parent::save($data);
  898. }
  899. else
  900. {
  901. // This is a new object, and thus we need to save it first, so we can get its ID
  902. $res = parent::save($data);
  903. $this -> timeline($this, $this -> user -> id, $timestamp, $data, true);
  904. }
  905. }
  906. else
  907. {
  908. // do not timeline a timelines save! Recursive!
  909. $res = parent::save($data);
  910. }
  911. return $res;
  912. }
  913. public function search(array $data, DataMapper $dmObject, $where = '')
  914. {
  915. $search = $data['search'];
  916. $dmObject -> or_like('title', $search);
  917. $dmObject -> or_like('description', $search);
  918. if ($where)
  919. {
  920. if (is_array($where))
  921. {
  922. foreach ($where as $k => $w)
  923. {
  924. $sql[] = $k . ' = "' . $w . '"';
  925. }
  926. $where = '( ' . implode(' AND ', $sql) . ')';
  927. }
  928. $dmObject -> having($where);
  929. }
  930. $dmObject -> get();
  931. return $dmObject;
  932. }
  933. public function getItems($type, $model, $sort, $where = null, $include_related = null, $returnArray = false)
  934. {
  935. // if its a post, we should search the DB
  936. if (isset($_GET['search']))
  937. {
  938. // check if user is sorting
  939. if ($sort)
  940. {
  941. //add sort clause
  942. $model -> order_by("created", $sort);
  943. }
  944. $data['search'] = $_GET['search'];
  945. if ($returnArray)
  946. {
  947. return $model -> search($data, $model, $where, $include_related) -> all_to_array();
  948. }
  949. return $model -> search($data, $model, $where, $include_related);
  950. // search using parent function, generic across the board
  951. }
  952. else// just return either cached or sorted results
  953. {
  954. if ($returnArray)
  955. {
  956. return $model -> getObject($type, null, null, true, $where, $include_related, $sort) -> dm_object -> all_to_array();
  957. }
  958. return $model -> getObject($type, null, null, true, $where, $include_related, $sort) -> dm_object;
  959. // get the default cached list of item, return as array
  960. }
  961. }
  962. public function more($id, $model)
  963. {
  964. return $model -> where('id >' . $id) -> order_by("created", 'asc') -> limit(3) -> get();
  965. }
  966. public function getCachedActivity()
  967. {
  968. $cacheID = 'activity_cache';
  969. // set the cache id
  970. return $this -> cache -> file -> get($cacheID);
  971. }
  972. public function commentCacheActivity($type, $arr, array $comment)
  973. {
  974. $cacheID = 'activity_cache';
  975. // set the cache id
  976. if (!$arr['id'])
  977. {
  978. throw new Exception('No id found in the datamapper object, there must be an ID');
  979. // if there was no id throw an exception as, it should never passs here with out having an id
  980. }
  981. if (!$arr['created_by'])
  982. {
  983. throw new Exception('No created_by int found in the datamapper object, there must be an created by value');
  984. // if there was no id throw an exception as, it should never passs here with out having an id
  985. }
  986. // check the datamapper object
  987. $activityCache = $this -> cache -> file -> get($cacheID);
  988. // get the activity cache
  989. $activityCache[$type . '_' . $arr['id']][$arr['created_by']][$type][$arr['id']]['comments'][] = $comment;
  990. // instead of just having a numerical index combine both type and id so it will always be unque, iner lvels keys also contain this info
  991. // time used as a way of making it possible to sort
  992. $activityCache[$type . '_' . $arr['id']][$arr['created_by']][$type][$arr['id']][time()] = $arr;
  993. // ^ ^ ^ ^
  994. //createdby// //type// //item_id// //order//
  995. // set up updated cached object
  996. $this -> cache -> file -> save($cacheID, $activityCache, 1000000);
  997. // save the cache
  998. return true;
  999. }
  1000. public function updateCacheActivity($type, array $arr)
  1001. {
  1002. $cacheID = 'activity_cache';
  1003. // set the cache id
  1004. if (!$arr['id'])
  1005. {
  1006. throw new Exception('No id found in the datamapper object, there must be an ID');
  1007. // if there was no id throw an exception as, it should never passs here with out having an id
  1008. }
  1009. if (!$arr['created_by'])
  1010. {
  1011. throw new Exception('No created_by int found in the datamapper object, there must be an created by value');
  1012. // if there was no id throw an exception as, it should never passs here with out having an id
  1013. }
  1014. // check the datamapper object
  1015. $activityCache = $this -> cache -> file -> get($cacheID);
  1016. // get the activity cache
  1017. $arr['likes'] = 0;
  1018. $arr['comments'] = array();
  1019. // instead of just having a numerical index combine both type and id so it will always be unque, iner lvels keys also contain this info
  1020. // time used as a way of making it possible to sort
  1021. unset($activityCache[$type . '_' . $arr['id']][$arr['created_by']][$type][$arr['id']]);
  1022. $activityCache[$type . '_' . $arr['id']][$arr['created_by']][$type][$arr['id']][time()] = $arr;
  1023. // ^ ^ ^ ^
  1024. //createdby// //type// //item_id// //order//
  1025. // set up updated cached object
  1026. $this -> cache -> file -> save($cacheID, $activityCache, 1000000);
  1027. // save the cache
  1028. return true;
  1029. }
  1030. public function removeCacheActivity($type, $id)
  1031. {
  1032. $activityCache = $this -> cache -> file -> get($cacheID);
  1033. // get the activity cache
  1034. unset($activityCache[$type . '_' . $id]);
  1035. $this -> cache -> file -> save($cacheID, $activityCache);
  1036. }
  1037. public function createCacheActivity($type, array $arr)
  1038. {
  1039. $cacheID = 'activity_cache';
  1040. // set the cache id
  1041. if (!$arr['id'])
  1042. {
  1043. throw new Exception('No id found in the datamapper object, there must be an ID');
  1044. // if there was no id throw an exception as, it should never passs here with out having an id
  1045. }
  1046. if (!$arr['created_by'])
  1047. {
  1048. throw new Exception('No created_by int found in the datamapper object, there must be an created by value');
  1049. // if there was no id throw an exception as, it should never passs here with out having an id
  1050. }
  1051. // check the datamapper object
  1052. $activityCache = $this -> cache -> file -> get($cacheID);
  1053. // get the activity cache
  1054. $arr['likes'] = 0;
  1055. $arr['comments'] = array();
  1056. // instead of just having a numerical index combine both type and id so it will always be unque, iner lvels keys also contain this info
  1057. $activityCache[$type . '_' . $arr['id']][$arr['created_by']][$type][$arr['id']][time()] = $arr;
  1058. // ^ ^ ^ ^
  1059. //createdby// //type// //item_id// //order//
  1060. // set up updated cached object
  1061. $this -> cache -> file -> save($cacheID, $activityCache, 1000000);
  1062. // save the cache
  1063. return true;
  1064. }
  1065. }
  1066. /**
  1067. * So we dont have to rename all the classes
  1068. *
  1069. */
  1070. class Base_model extends MY_Model
  1071. {
  1072. function __construct($id = null)
  1073. {
  1074. parent::__construct($id);
  1075. }
  1076. function __destruct()
  1077. {
  1078. }
  1079. }