PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/models/datamodel.php

http://three-cms.googlecode.com/
PHP | 729 lines | 480 code | 56 blank | 193 comment | 77 complexity | 74984b1cce6695d9696cfc2e8d3874b5 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * DataModel
  4. * ---------------------------------------------------------------------------
  5. * The DataModel is used by the website to load and format data that can be
  6. * used by the website.
  7. * ---------------------------------------------------------------------------
  8. * Author: Giel Berkers
  9. * E-mail: giel.berkers@gmail.com
  10. * Revision: 1
  11. * ---------------------------------------------------------------------------
  12. * Changelog:
  13. *
  14. *
  15. */
  16. // TODO: Make queries with Active Record Class
  17. class DataModel extends Model
  18. {
  19. var $options; // An array holding the options of this data object and it's values
  20. var $idContent; // The ID of the content
  21. var $idLanguage; // The ID of the language
  22. var $templateFile; // The template file
  23. var $settings; // Settings object
  24. var $parameters; // An array with parameters in the URL
  25. var $contentObjects; // An array with content objects
  26. // The following parameters don't get set until a certain first function call
  27. // This makes the dataModel load faster in case the parameter is not used.
  28. // TODO: Is this still done? can it be deleted?
  29. var $parentsArray = array(); // A 2-dimensional array holding the parents
  30. var $childrenArray = array(); // A 2-dimensional array holding the children
  31. function DataModel()
  32. {
  33. parent::Model();
  34. // Load the URL helper:
  35. $this->load->helper('url');
  36. // Load the session library:
  37. $this->load->library('session');
  38. // Assign Addons:
  39. // Load the addonBaseModel:
  40. $this->load->model('AddonBaseModel', '', true);
  41. // Load the addonModel:
  42. $this->load->model('AddonModel', '', true);
  43. // Default settings:
  44. $this->parameters = false;
  45. $this->contentObjects = false;
  46. }
  47. function load($idContent, $idLanguage)
  48. {
  49. // Default settings:
  50. $this->idContent = $idContent;
  51. $this->idLanguage = $idLanguage;
  52. $this->settings = $this->getSettings();
  53. // Caching:
  54. // Caching is done by checking if there is a datafile: cache/data.idcontent.idlanguage.php
  55. // The datafile is nothing more than the options-array and some other variables. If the file
  56. // does not exist, execute the queries needed and create the datafile, otherwise just
  57. // include the file.
  58. // If the content gets edited, the datafile gets deleted
  59. $cacheFile = 'system/cache/data.'.$idContent.'.'.$idLanguage.'.php';
  60. if(file_exists($cacheFile)) {
  61. include($cacheFile);
  62. $this->options = $options;
  63. $this->templateFile = $templateFile;
  64. } else {
  65. $this->options = array();
  66. // Default settings:
  67. $this->db->select('name,alias,order,id_content,id_template');
  68. $this->db->where('id', $idContent);
  69. $query = $this->db->get('content');
  70. $info = $query->result_array();
  71. // Some default options:
  72. $this->options['idContent'] = $idContent;
  73. $this->options['idLanguage'] = $idLanguage;
  74. $this->options['idParent'] = $info[0]['id_content'];
  75. $this->options['idTemplate'] = $info[0]['id_template'];
  76. $this->options['alias'] = $info[0]['alias'];
  77. $this->options['contentName'] = $info[0]['name'];
  78. $this->options['order'] = $info[0]['order'];
  79. // Killer Query to do the magic:
  80. // What it does: It selects the options that belong to the object of this content,
  81. // and it retrieves the objects correct values according to the given language, or
  82. // if the object isn't multilanguage, it returns it's defaults language value.
  83. $pf = $this->db->dbprefix;
  84. $sql = 'SELECT C.`type`, C.`name`, D.`value` FROM
  85. `'.$pf.'content` A,
  86. `'.$pf.'dataobjects_options` B,
  87. `'.$pf.'options` C,
  88. `'.$pf.'values` D,
  89. `'.$pf.'templates` E
  90. WHERE
  91. A.`id` = '.$idContent.' AND
  92. A.`id_template` = E.`id` AND
  93. E.`id_dataobject` = B.`id_dataobject` AND
  94. C.`id` = B.`id_option` AND
  95. D.`id_content` = '.$idContent.' AND
  96. D.`id_option` = B.`id_option` AND
  97. D.`id_language` = IF(C.`multilanguage` = 1, '.$idLanguage.', '.$this->settings['default_language'].');
  98. ';
  99. $query = $this->db->query($sql);
  100. // Fill the dataObject with the values:
  101. foreach($query->result() as $result) {
  102. // Execute Hook to allow to modify the data:
  103. // Note that here the result-parameter is a pointer to the $result-object. This is because the executeHook()-function cannot return values, only true or false.
  104. $this->AddonModel->executeHook('ModifyOptionValue', array('result'=>&$result, 'dataObject'=>$this));
  105. // If type is rich_text, replace id:-links with the correct URL:
  106. if($result->type=='rich_text') {
  107. $value = $result->value;
  108. preg_match_all('/href="id:(.*)"/', $value, $matches);
  109. for($i=0; $i<count($matches[0]); $i++) {
  110. $value = str_replace($matches[0][$i], 'href="'.$this->getUrl($matches[1][$i]).'"', $value);
  111. }
  112. $this->options[$result->name] = $value;
  113. } else {
  114. $this->options[$result->name] = $result->value;
  115. }
  116. }
  117. // Retrieve the template file:
  118. $sql = 'SELECT B.`templatefile` FROM
  119. `'.$pf.'content` A,
  120. `'.$pf.'templates` B
  121. WHERE
  122. A.`id_template` = B.`id` AND
  123. A.`id` = '.$idContent.';
  124. ';
  125. $query = $this->db->query($sql);
  126. $this->templateFile = $query->row()->templatefile;
  127. // Save the cache file:
  128. $cacheStr = '<?php'."\n";
  129. // The options:
  130. $cacheStr.= "\t".'$options = array('."\n";
  131. $first = true;
  132. foreach($this->options as $key=>$value) {
  133. if(!$first) { $cacheStr.=','."\n"; }
  134. $first = false;
  135. $cacheStr.= "\t\t".'\''.$key.'\'=>\''.str_replace('\'', '\\\'', $value).'\'';
  136. }
  137. $cacheStr.= "\n\t".');'."\n";
  138. $cacheStr.= "\t".'$templateFile = \''.$this->templateFile.'\';'."\n";
  139. $cacheStr.= '?>';
  140. $handle = fopen($cacheFile, 'w');
  141. fwrite($handle, $cacheStr);
  142. fclose($handle);
  143. }
  144. }
  145. /**
  146. * Get the children of this dataModel
  147. * @param $idContent int The ID of the parent to get the children from. If ID is set to null (default), the current dataobjects' ID is used
  148. * @param $options array An array with options to use as filter
  149. * @param $limit array An array with one ore two values for the limit-options
  150. * @param $orderby string A string to order by: 'optionName asc/desc'. example: 'myDate asc'
  151. * @return array An array with dataModels
  152. */
  153. function children($idContent = null, $options = null, $limit = null, $orderby = null)
  154. {
  155. // TODO: Make Query Active Record Style
  156. $idContent = $idContent !== null ? $idContent : $this->idContent;
  157. $depth = 0;
  158. $parentIDs = array();
  159. if(!is_numeric($idContent)) {
  160. // $idContent is a string, now special restrictions apply:
  161. // examples:
  162. // 4> : Get the children of the CHILDREN of id=4
  163. // 4,8 : Get the children of id=4 and id=8
  164. // TODO: 4>> : Get the children of the CHILDREN of the CHILDREN of id=4
  165. // TODO: 4t2 : Get the children of id=4 with template=2
  166. // TODO: 4>t2 : Get the children of the children of id=4 with template=2
  167. // Regular expression:
  168. $a = explode(",", $idContent);
  169. if(count($a)==1) {
  170. $a = explode(">", $idContent);
  171. $idContent = $a[0];
  172. // $depth = count($a)-1;
  173. $depth = 1;
  174. $fromAlias = 'd0';
  175. } else {
  176. $fromAlias = 'a';
  177. $parentIDs = $a;
  178. $idContent = $a[0];
  179. array_shift($parentIDs);
  180. }
  181. } else {
  182. $fromAlias = 'a';
  183. }
  184. // Retrieve the children of this data object:
  185. $children = array();
  186. $pf = $this->db->dbprefix;
  187. $sql = 'SELECT DISTINCT `'.$fromAlias.'`.`id` FROM (`'.$pf.'content` a ';
  188. if($depth > 0) {
  189. for($i=0; $i<$depth; $i++) {
  190. $sql .= ', `'.$pf.'content` d'.$i;
  191. }
  192. }
  193. $firstWhere = true;
  194. $where = '';
  195. if($options != null) {
  196. // Create an associated array:
  197. if(is_string($options)) {
  198. $options = $this->stringToAssocArray($options);
  199. }
  200. // Adjust the query:
  201. $sql .= ', `'.$pf.'options` b, `'.$pf.'values` c) ';
  202. foreach($options as $key=>$value) {
  203. // See if there is an operator present:
  204. // >,<,>=,<=,!=
  205. if(preg_match('/(>=|<=|>|<|\!=|=>)/', $value)==1) {
  206. $operator = preg_replace('/(.*)(>=|<=|>|<|\!=|=>)(.*)/', '\\2', $value);
  207. } else {
  208. $operator = '=';
  209. }
  210. if($operator != '=') {
  211. $a = explode($operator, $value);
  212. $key = $a[0];
  213. $value = $a[1];
  214. }
  215. if($key == 'name' || $key == 'order') {
  216. if($firstWhere) {
  217. $firstWhere = false;
  218. $where .= 'WHERE a.`'.$key.'` = \''.$value.'\' ';
  219. } else {
  220. $where .= 'AND a.`'.$key.'` = \''.$value.'\' ';
  221. }
  222. } else {
  223. if($firstWhere) {
  224. $firstWhere = false;
  225. $where .= 'WHERE b.`name` = \''.$key.'\' ';
  226. } else {
  227. $where .= 'AND b.`name` = \''.$key.'\' ';
  228. }
  229. $where .= 'AND c.`id_option` = b.`id` ';
  230. $where .= 'AND c.`value` '.$operator.' \''.$value.'\' ';
  231. $where .= 'AND c.`id_content` = '.$fromAlias.'.`id` ';
  232. }
  233. }
  234. } else {
  235. $sql .= ') ';
  236. }
  237. if($orderby != null) {
  238. // Order by given options:
  239. $orderby = explode(' ', $orderby);
  240. $item = $orderby[0];
  241. $direction = isset($orderby[1]) ? strtolower($orderby[1]) : 'asc';
  242. if($direction != 'asc' && $direction != 'desc') {
  243. $direction = 'asc';
  244. }
  245. if($item == 'name' || $item == 'order') {
  246. $orderby = ' ORDER BY a.`'.$item.'` '.$direction.' ';
  247. } else {
  248. $sql .= 'JOIN (`'.$pf.'values`, `'.$pf.'options`) ON (`'.$pf.'values`.`id_content` = `'.$fromAlias.'`.`id` AND `'.$pf.'values`.`id_option` = `'.$pf.'options`.`id` AND `'.$pf.'options`.`name` = \''.$item.'\') ';
  249. $orderby = ' ORDER BY `'.$pf.'values`.`value` '.strtoupper($direction).' ';
  250. }
  251. } else {
  252. // Order by internal order-parameter:
  253. $orderby = ' ORDER BY a.`order` ASC ';
  254. }
  255. if($firstWhere) {
  256. $where .= 'WHERE a.`id_content` = '.$idContent.' ';
  257. $firstWhere = false;
  258. } else {
  259. $where .= 'AND a.`id_content` = '.$idContent.' ';
  260. }
  261. if(count($parentIDs) > 0) {
  262. foreach($parentIDs as $pID) {
  263. $where .= 'OR a.`id_content` = '.$pID.' ';
  264. }
  265. }
  266. if($depth > 0) {
  267. for($i=0; $i<$depth; $i++) {
  268. $where .= 'AND d'.$i.'.`id_content` = a.`id`';
  269. }
  270. }
  271. $sql .= $where.$orderby;
  272. if($limit!=null) {
  273. if(is_string($limit)) {
  274. $limit = $this->stringToAssocArray($limit);
  275. }
  276. if(count($limit)==1) {
  277. $sql .= 'LIMIT '.$limit[0];
  278. } else {
  279. $sql .= 'LIMIT '.$limit[1].', '.$limit[0];
  280. }
  281. }
  282. $sql .= ';';
  283. // echo $sql;
  284. $query = $this->db->query($sql);
  285. foreach($query->result() as $result) {
  286. $dataObject = new DataModel();
  287. $dataObject->load($result->id, $this->idLanguage);
  288. array_push($children, $dataObject);
  289. }
  290. return $children;
  291. }
  292. /**
  293. * Convert a string to an associated array
  294. * @param $str string The string to convert
  295. * @return array An associated array
  296. */
  297. function stringToAssocArray($str)
  298. {
  299. $array = array();
  300. $items = explode(',', $str);
  301. foreach($items as $item) {
  302. $itemArray = explode('=>', $item);
  303. if(count($itemArray)==2) {
  304. $array[$itemArray[0]] = $itemArray[1];
  305. } else {
  306. array_push($array, $item);
  307. }
  308. }
  309. return $array;
  310. }
  311. /**
  312. * Get a specific child of this dataModel
  313. * @param $idContent int The ID of the parent to get the children from. If ID is set to null (default), the current dataobjects' ID is used
  314. * @param $options array An array with options to use as filter
  315. * @param $num int The number of the child to retrieve (default=the first)
  316. * @return DataModel A single datamodel or false of no model is found
  317. */
  318. function child($idContent = null, $options = null, $num = 0)
  319. {
  320. $docs = $this->children($idContent, $options, array(1, $num));
  321. if(count($docs)>0) {
  322. return $docs[0];
  323. } else {
  324. return false;
  325. }
  326. }
  327. /**
  328. * Create a tree of this dataModel and all it's children (recursive)
  329. * @param $startID int The ID to see as te root parent. Set to null to use the current dataModel's ID
  330. * @param $templates array An array containing the ID's of the templates to allow in this tree. Set to null to allow all templates.
  331. * @param $optionConditions array An associated array holding the name of the options and their value to which the content should be filterd. Set to null to allow all content
  332. * @return array A multi-dimensional array of the whole tree
  333. */
  334. function getTree($startID=null, $templates=null, $optionConditions=null)
  335. {
  336. $startID = $startID !== null ? $startID : $this->idContent;
  337. $tree = array();
  338. $this->db->select('id');
  339. $this->db->where('id_content', $startID);
  340. if($templates !== null) {
  341. $first = true;
  342. foreach($templates as $id_template) {
  343. if($first) {
  344. $this->db->where('id_template', $id_template);
  345. $first = false;
  346. } else {
  347. $this->db->or_where('id_template', $id_template);
  348. }
  349. }
  350. }
  351. // TODO: Filter by optionConditions <- ???
  352. $query = $this->db->get('content');
  353. foreach($query->result() as $result) {
  354. $child = array(
  355. 'id'=>$result->id,
  356. 'children'=>$this->getTree($result->id, $templates, $optionConditions)
  357. );
  358. array_push($tree, $child);
  359. }
  360. // print_r($tree);
  361. return $tree;
  362. }
  363. /**
  364. * Create the url to this dataobject.
  365. * @param $idContent int The ID of the content to create the URL of, if left empty, the URL of the current page is returned.
  366. * @param $idLanguage int The ID of the language to use for this URL. null for current language
  367. */
  368. function getUrl($idContent = null, $idLanguage = null)
  369. {
  370. $idContent = $idContent !== null ? $idContent : $this->idContent;
  371. $parents = $this->parents($idContent);
  372. $languageCode = $this->getLanguageCode($idLanguage);
  373. $aliases = array($languageCode);
  374. foreach($parents as $parentObject)
  375. {
  376. array_push($aliases, $parentObject->get('alias'));
  377. }
  378. if($idContent != $this->idContent) {
  379. array_push($aliases, $this->getAlias($idContent));
  380. } else {
  381. array_push($aliases, $this->get('alias'));
  382. }
  383. return site_url($aliases);
  384. }
  385. /**
  386. * Get the alias of a given content ID
  387. * @param $idContent int The ID of the content to get the alias from
  388. * @return string The alias
  389. */
  390. function getAlias($idContent)
  391. {
  392. $this->db->select('alias');
  393. $this->db->where('id', $idContent);
  394. $query = $this->db->get('content');
  395. $result = $query->result();
  396. return $result[0]->alias;
  397. }
  398. /**
  399. * Get the id of a given content alias
  400. * @param $alias string The alias of the content to get the id from
  401. * @return int The id
  402. */
  403. function getId($alias)
  404. {
  405. $this->db->select('id');
  406. $this->db->where('alias', $alias);
  407. $query = $this->db->get('content');
  408. $result = $query->result();
  409. return $result[0]->id;
  410. }
  411. /**
  412. * Get the language code
  413. * @param $idLanguage int The ID of the language
  414. * @return string The code of the language
  415. */
  416. function getLanguageCode($idLanguage = null)
  417. {
  418. $idLanguage = $idLanguage !== null ? $idLanguage : $this->idLanguage;
  419. $this->db->select('code');
  420. $this->db->where('id', $idLanguage);
  421. $query = $this->db->get('languages');
  422. $result = $query->result();
  423. return $result[0]->code;
  424. }
  425. /**
  426. * Get an array with all the parents
  427. * @param $idContent int The ID of the child to get the parents from. If ID is set to null (default), the current dataObjects' ID is used
  428. * @return array An array with dataModels
  429. */
  430. function parents($idContent = null)
  431. {
  432. $idContent = $idContent !== null ? $idContent : $this->idContent;
  433. if(!isset($this->parentsArray[$idContent])) {
  434. // Create an array in which the first entry is the root parent:
  435. $parents = array();
  436. $idParent = $idContent;
  437. // Add a safety counter, so this will not become an infinite loop:
  438. $safetyCounter = 0;
  439. $infiniteLoop = false;
  440. while($safetyCounter < 100) {
  441. $this->db->select('id_content');
  442. $this->db->where('id', $idParent);
  443. $query = $this->db->get('content');
  444. if($this->db->count_all_results() > 0) {
  445. $idParent = $query->row()->id_content;
  446. if($idParent==0) {
  447. break;
  448. }
  449. $dataObject = new DataModel();
  450. $dataObject->load($idParent, $this->idLanguage);
  451. array_unshift($parents, $dataObject);
  452. }
  453. $safetyCounter++;
  454. if($safetyCounter>=100) {
  455. $infiniteLoop = true;
  456. break;
  457. }
  458. }
  459. if($infiniteLoop) {
  460. // Error in loop
  461. log_message('error', 'Infinite loop detected when determing the parent. Content ID: '.$idContent, true);
  462. return false;
  463. } else {
  464. $this->parentsArray[$idContent] = $parents;
  465. }
  466. }
  467. return $this->parentsArray[$idContent];
  468. }
  469. /**
  470. * Create an array with the different languages this website uses
  471. * @return array A 2-dimensional array
  472. */
  473. function getLanguages()
  474. {
  475. $languages = array();
  476. $query = $this->db->where('active', 1);
  477. $query = $this->db->get('languages');
  478. foreach($query->result_array() as $language) {
  479. $language['url'] = site_url($language['code']);
  480. $language['currentUrl'] = $this->getUrl($this->idContent, $language['id']);
  481. array_push($languages, $language);
  482. }
  483. return $languages;
  484. }
  485. /**
  486. * Get the first parent
  487. * @param $idContent int The ID of the child to get the first parent from. If ID is set to null (default), the current dataobjects' ID is used
  488. * @return DataModel A single datamodel
  489. */
  490. function firstParent($idContent = null)
  491. {
  492. $parents = isset($this->parentsArray[$idContent]) ? $this->parentsArray[$idContent] : $this->parents($idContent);
  493. return $parents[0]; // The first entry in the array is the first parent.
  494. }
  495. /**
  496. * Get the parent
  497. * @param $idContent int The ID of the child to get the parent from. If ID is set to null (default), the current dataobjects' ID is used
  498. * @return DataModel A single datamodel
  499. */
  500. function getParent($idContent = null)
  501. {
  502. $parents = isset($this->parentsArray[$idContent]) ? $this->parentsArray[$idContent] : $this->parents($idContent);
  503. return $parents[count($parents)-1]; // The last entry in the array is the parent.
  504. }
  505. /**
  506. * Get a specific parameter
  507. * @param $parameter string The name of the parameter
  508. * @return string The value
  509. */
  510. function get($parameter)
  511. {
  512. if(isset($this->options[$parameter])) {
  513. return $this->options[$parameter];
  514. } else {
  515. return false;
  516. }
  517. }
  518. /**
  519. * Get the settings
  520. * TODO: This is the same function as in the admin model. Is there a way that these two can be combined?
  521. * @return array Associated array with the settings
  522. */
  523. function getSettings()
  524. {
  525. // Caching:
  526. $cacheFile = 'system/cache/data.settings.php';
  527. if(file_exists($cacheFile)) {
  528. include($cacheFile);
  529. } else {
  530. $cacheStr = '<?php'."\n\t".'$settings = array('."\n"; // For caching
  531. $first = true;
  532. $settings = array();
  533. $this->db->select('name,value');
  534. $query = $this->db->get('settings');
  535. foreach($query->result() as $setting) {
  536. $settings[$setting->name] = $setting->value;
  537. if(!$first) {
  538. $cacheStr.=','."\n";
  539. }
  540. $first = false;
  541. $cacheStr.= "\t\t".'\''.$setting->name.'\'=>\''.str_replace('\'', '\\\'', $setting->value).'\'';
  542. }
  543. // Save cache file:
  544. $cacheStr.= "\n"."\t".');'."\n".'?>';
  545. $handle = fopen($cacheFile, 'w');
  546. fwrite($handle, $cacheStr);
  547. fclose($handle);
  548. }
  549. return $settings;
  550. }
  551. /**
  552. * Get the locales
  553. * @param $idLanguage int The ID of the language, leave null to get the current language
  554. * @return array
  555. */
  556. function getLocales($idLanguage = null)
  557. {
  558. $idLanguage = $idLanguage !== null ? $idLanguage : $this->idLanguage;
  559. // Caching:
  560. $cacheFile = 'system/cache/data.locales.'.$idLanguage.'.php';
  561. if(file_exists($cacheFile)) {
  562. include($cacheFile);
  563. } else {
  564. $cacheStr = '<?php'."\n\t".'$locales = array('."\n"; // For caching
  565. $first = true;
  566. $locales = array();
  567. // TODO: Make this query active-record-style:
  568. $pf = $this->db->dbprefix;
  569. $sql = 'SELECT A.`name`, B.`value` FROM
  570. `'.$pf.'locales` A,
  571. `'.$pf.'locales_values` B
  572. WHERE
  573. B.`id_language` = '.$idLanguage.' AND
  574. B.`id_locale` = A.`id`';
  575. $query = $this->db->query($sql);
  576. foreach($query->result() as $locale) {
  577. $locales[$locale->name] = $locale->value;
  578. if(!$first) {
  579. $cacheStr.=','."\n";
  580. }
  581. $first = false;
  582. $cacheStr.= "\t\t".'\''.$locale->name.'\'=>\''.str_replace('\'', '\\\'', $locale->value).'\'';
  583. }
  584. // Save cache file:
  585. $cacheStr.= "\n"."\t".');'."\n".'?>';
  586. $handle = fopen($cacheFile, 'w');
  587. fwrite($handle, $cacheStr);
  588. fclose($handle);
  589. }
  590. return $locales;
  591. }
  592. /**
  593. * Count the amount of records in a database with certain conditions
  594. * @param $where string Condition to check for (can be template, parent)
  595. * @param $value string The value to check the condition with
  596. * @return int The number of matches found
  597. */
  598. /*
  599. function count($where, $value)
  600. {
  601. if($where == 'template') { $where = 'id_template'; }
  602. if($where == 'parent') { $where = 'id_content'; }
  603. $pf = $this->db->dbprefix;
  604. $sql = 'SELECT COUNT(*) AS `total` FROM `'.$pf.'content` WHERE `'.$where.'` = \''.$value.'\';';
  605. $query = $this->db->query($sql);
  606. $result = $query->result();
  607. return $result[0]->total;
  608. }
  609. */
  610. /**
  611. * Create a new data object with the given parameters
  612. * @param $idContent int The ID of the content
  613. * @param $idLanguage int The ID of the language
  614. * @return DataModel The data object
  615. */
  616. function newObject($idContent, $idLanguage)
  617. {
  618. $object = new DataModel();
  619. $object->load($idContent, $idLanguage);
  620. return $object;
  621. }
  622. /**
  623. * Render this datamodel according to it's template
  624. * @param $display boolean Display the page (true) or return it as a string (false)
  625. * @return string Empty string on success or a string with the content if display is false
  626. */
  627. function render($display = true)
  628. {
  629. // Initialize smarty:
  630. $smarty = new Smarty();
  631. // Set the correct directories:
  632. $smarty->template_dir = SMARTY_TEMPLATE_DIR;
  633. $smarty->compile_dir = SMARTY_COMPILE_DIR;
  634. $smarty->cache_dir = SMARTY_CACHE_DIR;
  635. $smarty->config_dir = SMARTY_CONFIG_DIR;
  636. $smarty->debug_tpl = SMARTY_DEBUG_TPL;
  637. // Default settings:
  638. $smarty->compile_check = SMARTY_COMPILE;
  639. $smarty->debugging = SMARTY_DEBUG;
  640. $smarty->caching = SMARTY_CACHE;
  641. $smarty->cache_lifetime = SMARTY_CACHE_LIFETIME;
  642. // Assign the options:
  643. foreach($this->options as $key=>$value) {
  644. $smarty->assign($key, $value);
  645. }
  646. // Assign a reference to the dataObject:
  647. $smarty->assign('dataObject', $this);
  648. $smarty->assign('this', $this);
  649. // Assign a reference to the settings:
  650. $smarty->assign('settings', $this->settings);
  651. // Assign a reference to the locales:
  652. $smarty->assign('locale', $this->getLocales());
  653. // Assign the parameters:
  654. $smarty->assign('parameters', $this->parameters);
  655. // Assign the contentObjects:
  656. $smarty->assign('contentObjects', $this->contentObjects);
  657. // Assign the addons:
  658. foreach($this->AddonModel->addons as $addon) {
  659. if($addon[1]->frontEnd) {
  660. $addon[1]->dataObject = $this;
  661. $smarty->assign($addon[0], $addon[1]);
  662. }
  663. }
  664. // Render the page:
  665. if($display) {
  666. $smarty->display($this->templateFile);
  667. return '';
  668. } else {
  669. return $smarty->fetch($this->templateFile);
  670. }
  671. }
  672. }
  673. ?>