PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/halogy/libraries/Template.php

https://gitlab.com/intelij/Halogy
PHP | 1129 lines | 832 code | 152 blank | 145 comment | 119 complexity | 1705bc6deef7a94403eabdd30a57a397 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Halogy
  4. *
  5. * A user friendly, modular content management system for PHP 5.0
  6. * Built on CodeIgniter - http://codeigniter.com
  7. *
  8. * @package Halogy
  9. * @author Haloweb Ltd
  10. * @copyright Copyright (c) 2012, Haloweb Ltd
  11. * @license http://halogy.com/license
  12. * @link http://halogy.com/
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. class Template {
  18. // set defaults
  19. var $CI; // CI instance
  20. var $base_path = ''; // default base path
  21. var $moduleTemplates = array();
  22. var $template = array();
  23. function Template()
  24. {
  25. $this->CI =& get_instance();
  26. // get siteID, if available
  27. if (defined('SITEID'))
  28. {
  29. $this->siteID = SITEID;
  30. }
  31. $this->uploadsPath = $this->CI->config->item('uploadsPath');
  32. // populate module templates array
  33. $this->moduleTemplates = array(
  34. 'blog',
  35. 'blog_single',
  36. 'blog_search',
  37. 'community_account',
  38. 'community_create_account',
  39. 'community_forgotten',
  40. 'community_home',
  41. 'community_login',
  42. 'community_members',
  43. 'community_messages',
  44. 'community_messages_form',
  45. 'community_messages_popup',
  46. 'community_messages_read',
  47. 'community_reset',
  48. 'community_view_profile',
  49. 'community_view_profile_private',
  50. 'events',
  51. 'events_single',
  52. 'events_search',
  53. 'forums',
  54. 'forums_delete',
  55. 'forums_edit_post',
  56. 'forums_edit_topic',
  57. 'forums_forum',
  58. 'forums_post_reply',
  59. 'forums_post_topic',
  60. 'forums_search',
  61. 'forums_topic',
  62. 'shop_account',
  63. 'shop_browse',
  64. 'shop_cancel',
  65. 'shop_cart',
  66. 'shop_checkout',
  67. 'shop_create_account',
  68. 'shop_donation',
  69. 'shop_featured',
  70. 'shop_forgotten',
  71. 'shop_login',
  72. 'shop_orders',
  73. 'shop_prelogin',
  74. 'shop_product',
  75. 'shop_recommend',
  76. 'shop_recommend_popup',
  77. 'shop_reset',
  78. 'shop_review',
  79. 'shop_review_popup',
  80. 'shop_success',
  81. 'shop_view_order',
  82. 'wiki',
  83. 'wiki_form',
  84. 'wiki_page',
  85. 'wiki_search'
  86. );
  87. }
  88. function generate_template($pagedata, $file = false)
  89. {
  90. // page data
  91. @$this->template['page:title'] = (isset($pagedata['title'])) ? htmlentities($pagedata['title']) : htmlentities($this->CI->site->config['siteName']);
  92. @$this->template['page:keywords'] = (isset($pagedata['keywords'])) ? $pagedata['keywords'] : '';
  93. @$this->template['page:description'] = (isset($pagedata['description'])) ? $pagedata['description'] : '';
  94. @$this->template['page:date'] = (isset($pagedata['dateCreated'])) ? dateFmt($pagedata['dateCreated']) : '';
  95. @$this->template['page:date-modified'] = (isset($pagedata['dateModified'])) ? dateFmt($pagedata['dateModified']) : '';
  96. @$this->template['page:uri'] = site_url($this->CI->uri->uri_string());
  97. @$this->template['page:uri-encoded'] = $this->CI->core->encode($this->CI->uri->uri_string());
  98. @$this->template['page:uri:segment(1)'] = $this->CI->uri->segment(1);
  99. @$this->template['page:uri:segment(2)'] = $this->CI->uri->segment(2);
  100. @$this->template['page:uri:segment(3)'] = $this->CI->uri->segment(3);
  101. @$this->template['page:template'] = ($this->template['page:template']) ? $this->template['page:template'] : '';
  102. // find out if logged in
  103. $this->template['logged-in'] = ($this->CI->session->userdata('session_user')) ? TRUE : FALSE;
  104. // find out if subscribed
  105. $this->template['subscribed'] = ($this->CI->session->userdata('subscribed')) ? TRUE : FALSE;
  106. // find out if admin
  107. $this->template['admin'] = ($this->CI->session->userdata('session_admin')) ? TRUE : FALSE;
  108. // find out if this is ajax
  109. $this->template['ajax'] = ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'))) ? TRUE : FALSE;
  110. // find out if browser is iphone
  111. $this->template['mobile'] = (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strpos($_SERVER['HTTP_USER_AGENT'], 'Android')) ? TRUE : FALSE;
  112. // permissions
  113. if ($this->CI->session->userdata('session_admin'))
  114. {
  115. if ($permissions = $this->CI->permission->get_group_permissions($this->CI->session->userdata('groupID')))
  116. {
  117. foreach($permissions as $permission)
  118. {
  119. @$this->template['permission:'.$permission] = TRUE;
  120. }
  121. }
  122. }
  123. // feed (if it exists for the module)
  124. @$this->template['page:feed'] = (isset($pagedata['feed'])) ? $pagedata['feed'] : '';
  125. // either build template from a file or from db
  126. if ($file)
  127. {
  128. $templateBody = $this->parse_template($file, FALSE, NULL, FALSE);
  129. }
  130. else
  131. {
  132. $templateData = $this->CI->core->get_template($pagedata['templateID']);
  133. $templateBody = $templateData['body'];
  134. }
  135. // parse it for everything else
  136. $this->template['body'] = $this->parse_template($templateBody, FALSE, NULL, FALSE);
  137. // get navigation and build menu
  138. if (preg_match_all('/{navigation(\:([a-z0-9\.-]+))?}/i', $this->template['body'], $matches))
  139. {
  140. $this->template = $this->parse_navigation('navigation', $this->template);
  141. }
  142. return $this->template;
  143. }
  144. function parse_includes($body)
  145. {
  146. // get includes
  147. preg_match_all('/include\:([a-z0-9\.-]+)/i', $body, $includes);
  148. if ($includes)
  149. {
  150. $includeBody = '';
  151. foreach($includes[1] as $include => $value)
  152. {
  153. $includeRow = $this->CI->core->get_include($value);
  154. $includeBody = $this->parse_body($includeRow['body'], FALSE, NULL, FALSE);
  155. $includeBody = $this->CI->parser->conditionals($includeBody, $this->template, TRUE);
  156. $body = str_replace('{include:'.$value.'}', $includeBody, $body);
  157. }
  158. }
  159. return $body;
  160. }
  161. function parse_navigation($navTag, $template)
  162. {
  163. // get all navigation
  164. $template[$navTag] = $this->parse_nav();
  165. // get parents
  166. $template[$navTag.':parents'] = $this->parse_nav(0, FALSE);
  167. // get uri
  168. $uri = (!$this->CI->uri->segment(1)) ? 'home' : $this->CI->uri->segment(1);
  169. // get children of active nav item
  170. if ($parent = $this->CI->core->get_page(FALSE, $uri))
  171. {
  172. $template[$navTag.':children'] = $this->parse_nav($parent['pageID']);
  173. }
  174. else
  175. {
  176. $template[$navTag.':children'] = '';
  177. }
  178. return $template;
  179. }
  180. function parse_nav($parentID = 0, $showChildren = TRUE)
  181. {
  182. $output = '';
  183. if ($navigation = $this->get_nav_parents($parentID))
  184. {
  185. $i = 1;
  186. foreach($navigation as $nav)
  187. {
  188. // set first and last state on menu
  189. $class = '';
  190. $class .= ($i == 1) ? 'first ' : '';
  191. $class .= (sizeof($navigation) == $i) ? 'last ' : '';
  192. // look for children
  193. $children = ($showChildren) ? $this->get_nav_children($nav['navID']) : FALSE;
  194. // parse the nav item for the link
  195. $output .= $this->parse_nav_item($nav['uri'], $nav['navName'], $children, $class);
  196. // parse for children
  197. if ($children)
  198. {
  199. $x = 1;
  200. $output .= '<ul class="subnav">';
  201. foreach($children as $child)
  202. {
  203. // set first and last state on menu
  204. $class = '';
  205. $class .= ($x == 1) ? 'first ' : '';
  206. $class .= (sizeof($children) == $x) ? 'last ' : '';
  207. // look for sub children
  208. $subChildren = $this->get_nav_children($child['navID']);
  209. // parse nav item
  210. $navItem = $this->parse_nav_item($child['uri'], $child['navName'], $subChildren, $class);
  211. $output .= $navItem;
  212. // parse for children
  213. if ($subChildren)
  214. {
  215. $y = 1;
  216. $output .= '<ul class="subnav">';
  217. foreach($subChildren as $subchild)
  218. {
  219. // set first and last state on menu
  220. $class = '';
  221. $class .= ($y == 1) ? 'first ' : '';
  222. $class .= (sizeof($subChildren) == $y) ? 'last ' : '';
  223. $navItem = $this->parse_nav_item($subchild['uri'], $subchild['navName'], '', $class).'</li>';
  224. $output .= $navItem;
  225. $y++;
  226. }
  227. $output .= '</ul>';
  228. }
  229. $output .= '</li>';
  230. $x++;
  231. }
  232. $output .= '</ul>';
  233. }
  234. $output .= '</li>';
  235. $i++;
  236. }
  237. }
  238. return $output;
  239. }
  240. function parse_nav_item($uri, $name, $children = FALSE, $class = '')
  241. {
  242. // init stuff
  243. $output = '';
  244. $childs = array();
  245. // tidy children array
  246. if ($children)
  247. {
  248. foreach($children as $child)
  249. {
  250. $childs[] = $child['uri'];
  251. }
  252. }
  253. // set active state on menu
  254. $currentNav = $uri;
  255. $output .= '<li class="';
  256. if (($currentNav != '/' && $currentNav == $this->CI->uri->uri_string()) ||
  257. $currentNav == $this->CI->uri->segment(1) ||
  258. (($currentNav == '' || $currentNav == 'home' || $currentNav == '/') &&
  259. ($this->CI->uri->uri_string() == '' || $this->CI->uri->uri_string() == '/home' || $this->CI->uri->uri_string() == '/')) ||
  260. @in_array(substr($this->CI->uri->uri_string(),1), $childs)
  261. )
  262. {
  263. $class .= 'active selected ';
  264. }
  265. if ($children)
  266. {
  267. $class .= 'expanded ';
  268. }
  269. // filter uri to make sure it's cool
  270. if (substr($uri,0,1) == '/')
  271. {
  272. $href = $uri;
  273. }
  274. elseif (stristr($uri,'http://'))
  275. {
  276. $href = $uri;
  277. }
  278. elseif (stristr($uri,'www.'))
  279. {
  280. $href = 'http://'.$uri;
  281. }
  282. elseif (stristr($uri,'mailto:'))
  283. {
  284. $href = $uri;
  285. }
  286. elseif ($uri == 'home')
  287. {
  288. $href = '/';
  289. }
  290. else
  291. {
  292. $href = '/'.$uri;
  293. }
  294. // output anchor with span in case of additional styling
  295. $output .= trim($class).'" id="nav-'.trim($uri).'"><a href="'.site_url($href).'" class="'.trim($class).'"><span>'.htmlentities($name).'</span></a>';
  296. return $output;
  297. }
  298. function get_nav($navID = '')
  299. {
  300. // default where
  301. $this->CI->db->where(array('siteID' => $this->siteID, 'deleted' => 0));
  302. // where parent is set
  303. $this->CI->db->where('parentID', 0);
  304. // get navigation from pages
  305. $this->CI->db->select('uri, pageID as navID, pageName as navName');
  306. $this->CI->db->order_by('pageOrder', 'asc');
  307. $query = $this->CI->db->get('pages');
  308. if ($query->num_rows())
  309. {
  310. return $query->result_array();
  311. }
  312. else
  313. {
  314. return false;
  315. }
  316. }
  317. function get_nav_parents($parentID = 0)
  318. {
  319. // default where
  320. $this->CI->db->where(array('siteID' => $this->siteID, 'deleted' => 0));
  321. // where parent is set
  322. $this->CI->db->where('parentID', $parentID);
  323. // where parent is set
  324. $this->CI->db->where('active', 1);
  325. // get navigation from pages
  326. $this->CI->db->select('uri, pageID as navID, pageName as navName');
  327. // nav has to be active because its parents
  328. $this->CI->db->where('navigation', 1);
  329. $this->CI->db->order_by('pageOrder', 'asc');
  330. $query = $this->CI->db->get('pages');
  331. if ($query->num_rows())
  332. {
  333. return $query->result_array();
  334. }
  335. else
  336. {
  337. return false;
  338. }
  339. }
  340. function get_nav_children($navID = '')
  341. {
  342. // default where
  343. $this->CI->db->where(array('siteID' => $this->siteID, 'deleted' => 0));
  344. // get nav by ID
  345. $this->CI->db->where('parentID', $navID);
  346. // where parent is set
  347. $this->CI->db->where('active', 1);
  348. // select
  349. $this->CI->db->select('uri, pageID as navID, pageName as navName');
  350. // where viewable
  351. $this->CI->db->where('navigation', 1);
  352. // page order
  353. $this->CI->db->order_by('pageOrder', 'asc');
  354. // grab
  355. $query = $this->CI->db->get('pages');
  356. if ($query->num_rows())
  357. {
  358. return $query->result_array();
  359. }
  360. else
  361. {
  362. return FALSE;
  363. }
  364. }
  365. function parse_template($body, $condense = FALSE, $link = '', $mkdn = TRUE)
  366. {
  367. $body = $this->parse_body($body, $condense, $link, $mkdn);
  368. return $body;
  369. }
  370. function parse_body($body, $condense = FALSE, $link = '', $mkdn = TRUE)
  371. {
  372. // parse for images
  373. $body = $this->parse_images($body);
  374. // parse for files
  375. $body = $this->parse_files($body);
  376. // parse for files
  377. $body = $this->parse_includes($body);
  378. // parse for modules
  379. $this->template = $this->parse_modules($body, $this->template);
  380. // site globals
  381. $body = str_replace('{site:name}', $this->CI->site->config['siteName'], $body);
  382. $body = str_replace('{site:domain}', $this->CI->site->config['siteDomain'], $body);
  383. $body = str_replace('{site:url}', $this->CI->site->config['siteURL'], $body);
  384. $body = str_replace('{site:email}', $this->CI->site->config['siteEmail'], $body);
  385. $body = str_replace('{site:tel}', $this->CI->site->config['siteTel'], $body);
  386. $body = str_replace('{site:currency}', $this->CI->site->config['currency'], $body);
  387. $body = str_replace('{site:currency-symbol}', currency_symbol(), $body);
  388. // logged in userdata
  389. $body = str_replace('{userdata:id}', ($this->CI->session->userdata('userID')) ? $this->CI->session->userdata('userID') : '', $body);
  390. $body = str_replace('{userdata:email}', ($this->CI->session->userdata('email')) ? $this->CI->session->userdata('email') : '', $body);
  391. $body = str_replace('{userdata:username}', ($this->CI->session->userdata('username')) ? $this->CI->session->userdata('username') : '', $body);
  392. $body = str_replace('{userdata:name}', ($this->CI->session->userdata('firstName') && $this->CI->session->userdata('lastName')) ? $this->CI->session->userdata('firstName').' '.$this->CI->session->userdata('lastName') : '', $body);
  393. $body = str_replace('{userdata:first-name}', ($this->CI->session->userdata('firstName')) ? $this->CI->session->userdata('firstName') : '', $body);
  394. $body = str_replace('{userdata:last-name}', ($this->CI->session->userdata('lastName')) ? $this->CI->session->userdata('lastName') : '', $body);
  395. // other useful stuff
  396. $body = str_replace('{date}', dateFmt(date("Y-m-d H:i:s"), ($this->CI->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'), $body);
  397. $body = str_replace('{date:unixtime}', time(), $body);
  398. // condense
  399. if ($condense)
  400. {
  401. if ($endchr = strpos($body, '{more}'))
  402. {
  403. $body = substr($body, 0, ($endchr + 6));
  404. $body = str_replace('{more}', '<p class="more"><a href="'.$link.'" class="button more">Read more</a></p>', $body);
  405. }
  406. }
  407. else
  408. {
  409. $body = str_replace('{more}', '', $body);
  410. }
  411. // parse for clears
  412. $body = str_replace('{clear}', '<div style="clear:both;"/></div>', $body);
  413. // parse for pads
  414. $body = str_replace('{pad}', '<div style="padding-bottom:10px;width:10px;clear:both;"/></div>', $body);
  415. // parse body for markdown and images
  416. if ($mkdn === TRUE)
  417. {
  418. // parse for mkdn
  419. $body = mkdn($body);
  420. }
  421. return $body;
  422. }
  423. function parse_modules($body, $template)
  424. {
  425. // get web forms
  426. if (preg_match_all('/{webform:([A-Za-z0-9_\-]+)}/i', $body, $matches))
  427. {
  428. // filter matches
  429. $webformID = preg_replace('/{|}/', '', $matches[0][0]);
  430. $webform = $this->CI->core->get_web_form_by_ref($matches[1][0]);
  431. $template[$webformID] = '';
  432. $required = array();
  433. // get web form
  434. if ($webform)
  435. {
  436. // set fields
  437. if ($webform['fieldSet'] == 1)
  438. {
  439. $required[] = 'fullName';
  440. $required[] = 'subject';
  441. $required[] = 'message';
  442. // populate template
  443. $template[$webformID] .= '
  444. <div class="formrow field-fullName">
  445. <label for="fullName">Full Name</label>
  446. <input type="text" id="fullName" name="fullName" value="'.$this->CI->input->post('fullName').'" class="formelement" />
  447. </div>
  448. <div class="formrow field-email">
  449. <label for="email">Email</label>
  450. <input type="text" id="email" name="email" value="'.$this->CI->input->post('email').'" class="formelement" />
  451. </div>
  452. <div class="formrow field-subject">
  453. <label for="subject">Subject</label>
  454. <input type="text" id="subject" name="subject" value="'.$this->CI->input->post('subject').'" class="formelement" />
  455. </div>
  456. <div class="formrow field-message">
  457. <label for="message">Message</label>
  458. <textarea id="message" name="message" class="formelement small">'.$this->CI->input->post('message').'</textarea>
  459. </div>
  460. ';
  461. }
  462. // set fields
  463. if ($webform['fieldSet'] == 2)
  464. {
  465. $required[] = 'fullName';
  466. // populate template
  467. $template[$webformID] .= '
  468. <div class="formrow field-fullName">
  469. <label for="fullName">Full Name</label>
  470. <input type="text" id="fullName" name="fullName" value="'.$this->CI->input->post('fullName').'" class="formelement" />
  471. </div>
  472. <div class="formrow field-email">
  473. <label for="email">Email</label>
  474. <input type="text" id="email" name="email" value="'.$this->CI->input->post('email').'" class="formelement" />
  475. </div>
  476. <input type="hidden" name="subject" value="'.$webform['formName'].'" />
  477. ';
  478. }
  479. // set fields
  480. if ($webform['fieldSet'] == 0)
  481. {
  482. // populate template
  483. $template[$webformID] .= '
  484. <input type="hidden" name="subject" value="'.$webform['formName'].'" />
  485. ';
  486. }
  487. // set account
  488. if ($webform['account'] == 1)
  489. {
  490. // populate template
  491. $template[$webformID] .= '
  492. <input type="hidden" name="subject" value="'.$webform['formName'].'" />
  493. <input type="hidden" name="message" value="'.$webform['outcomeMessage'].'" />
  494. <input type="hidden" name="groupID" value="'.$webform['groupID'].'" />
  495. ';
  496. }
  497. // set required
  498. if ($required)
  499. {
  500. $template[$webformID] .= '
  501. <input type="hidden" name="required" value="'.implode('|', $required).'" />
  502. ';
  503. }
  504. // output encoded webform ID
  505. $template[$webformID] .= '
  506. <input type="hidden" name="formID" value="'.$this->CI->core->encode($matches[1][0]).'" />
  507. ';
  508. }
  509. else
  510. {
  511. $template[$webformID] = '';
  512. }
  513. }
  514. // get blog headlines
  515. if (preg_match_all('/{headlines:blog(:category(\(([A-Za-z0-9_-]+)\))?)?(:limit(\(([0-9]+)\))?)?}/i', $body, $matches))
  516. {
  517. // load blog model
  518. $this->CI->load->model('blog/blog_model', 'blog');
  519. // filter through matches
  520. for ($x = 0; $x < sizeof($matches[0]); $x++)
  521. {
  522. // filter matches
  523. $headlineID = preg_replace('/{|}/', '', $matches[0][$x]);
  524. $limit = ($matches[6][$x]) ? $matches[6][$x] : $this->CI->site->config['headlines'];
  525. $headlines = ($matches[3][$x]) ? $this->CI->blog->get_posts_by_category($matches[3][$x], $limit) : $this->CI->blog->get_posts($limit);
  526. // get latest posts
  527. if ($headlines)
  528. {
  529. // fill up template array
  530. $i = 0;
  531. foreach ($headlines as $headline)
  532. {
  533. // get rid of any template tags
  534. $headlineBody = $this->parse_body($headline['body'], TRUE, site_url('blog/'.dateFmt($headline['dateCreated'], 'Y/m').'/'.$headline['uri']));
  535. $headlineExcerpt = $this->parse_body($headline['excerpt'], TRUE, site_url('blog/'.dateFmt($headline['dateCreated'], 'Y/m').'/'.$headline['uri']));
  536. // populate loop
  537. $template[$headlineID][$i] = array(
  538. 'headline:link' => site_url('blog/'.dateFmt($headline['dateCreated'], 'Y/m').'/'.$headline['uri']),
  539. 'headline:title' => $headline['postTitle'],
  540. 'headline:date' => dateFmt($headline['dateCreated'], ($this->CI->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
  541. 'headline:day' => dateFmt($headline['dateCreated'], 'd'),
  542. 'headline:month' => dateFmt($headline['dateCreated'], 'M'),
  543. 'headline:year' => dateFmt($headline['dateCreated'], 'y'),
  544. 'headline:body' => $headlineBody,
  545. 'headline:excerpt' => $headlineExcerpt,
  546. 'headline:comments-count' => $headline['numComments'],
  547. 'headline:author' => $this->CI->blog->lookup_user($headline['userID'], TRUE),
  548. 'headline:author-id' => $headline['userID'],
  549. 'headline:class' => ($i % 2) ? ' alt ' : ''
  550. );
  551. $i++;
  552. }
  553. }
  554. else
  555. {
  556. $template[$headlineID] = array();
  557. }
  558. }
  559. }
  560. // get events headlines
  561. if (preg_match_all('/{headlines:events(:limit(\(([0-9]+)\))?)?}/i', $body, $matches))
  562. {
  563. // load events model
  564. $this->CI->load->model('events/events_model', 'events');
  565. // filter matches
  566. $headlineID = preg_replace('/{|}/', '', $matches[0][0]);
  567. $limit = ($matches[3][0]) ? $matches[3][0] : $this->CI->site->config['headlines'];
  568. // get latest posts
  569. if ($headlines = $this->CI->events->get_events($limit))
  570. {
  571. // fill up template array
  572. $i = 0;
  573. foreach ($headlines as $headline)
  574. {
  575. $headlineBody = $this->parse_body($headline['description'], TRUE, site_url('events/viewevent/'.$headline['eventID']));
  576. $headlineExcerpt = $this->parse_body($headline['excerpt'], TRUE, site_url('events/viewevent/'.$headline['eventID']));
  577. $template[$headlineID][$i] = array(
  578. 'headline:link' => site_url('events/viewevent/'.$headline['eventID']),
  579. 'headline:title' => $headline['eventTitle'],
  580. 'headline:date' => dateFmt($headline['eventDate'], ($this->CI->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
  581. 'headline:day' => dateFmt($headline['eventDate'], 'd'),
  582. 'headline:month' => dateFmt($headline['eventDate'], 'M'),
  583. 'headline:year' => dateFmt($headline['eventDate'], 'y'),
  584. 'headline:body' => $headlineBody,
  585. 'headline:excerpt' => $headlineExcerpt,
  586. 'headline:author' => $this->CI->events->lookup_user($headline['userID'], TRUE),
  587. 'headline:author-id' => $headline['userID'],
  588. 'headline:class' => ($i % 2) ? ' alt ' : ''
  589. );
  590. $i++;
  591. }
  592. }
  593. else
  594. {
  595. $template[$headlineID] = array();
  596. }
  597. }
  598. // get wiki headlines
  599. if (preg_match_all('/{headlines:wiki(:category(\(([A-Za-z0-9_-]+)\))?)?(:limit(\(([0-9]+)\))?)?}/i', $body, $matches))
  600. {
  601. // load wiki model
  602. $this->CI->load->model('wiki/wiki_model', 'wiki');
  603. // filter matches
  604. $headlineID = preg_replace('/{|}/', '', $matches[0][0]);
  605. $limit = ($matches[3][0]) ? $matches[3][0] : $this->CI->site->config['headlines'];
  606. // get latest posts
  607. if ($headlines = $this->CI->wiki->get_pages($limit))
  608. {
  609. // fill up template array
  610. $i = 0;
  611. foreach ($headlines as $headline)
  612. {
  613. $template[$headlineID][$i] = array(
  614. 'headline:link' => site_url('wiki/' .$headline['uri']),
  615. 'headline:title' => $headline['pageName'],
  616. );
  617. $i++;
  618. }
  619. }
  620. else
  621. {
  622. $template[$headlineID] = array();
  623. }
  624. }
  625. // get gallery
  626. if (preg_match_all('/{gallery:([A-Za-z0-9_-]+)(:limit\(([0-9]+)\))?}/i', $body, $matches))
  627. {
  628. // load libs etc
  629. $this->CI->load->model('images/images_model', 'images');
  630. // filter through matches
  631. for ($x = 0; $x < sizeof($matches[0]); $x++)
  632. {
  633. // filter matches
  634. $headlineID = preg_replace('/{|}/', '', $matches[0][0]);
  635. $limit = ($matches[3][$x]) ? $matches[3][$x] : 9;
  636. // get latest posts
  637. if ($gallery = $this->CI->images->get_images_by_folder_ref($matches[1][$x], $limit))
  638. {
  639. // fill up template array
  640. $i = 0;
  641. foreach ($gallery as $galleryimage)
  642. {
  643. if ($imageData = $this->get_image($galleryimage['imageRef']))
  644. {
  645. $imageHTML = display_image($imageData['src'], $imageData['imageName']);
  646. $imageHTML = preg_replace('/src=("[^"]*")/i', 'src="'.site_url('/images/'.$imageData['imageRef'].strtolower($imageData['ext'])).'"', $imageHTML);
  647. $thumbTML = display_image($imageData['src'], $imageData['imageName']);
  648. $thumbHTML = preg_replace('/src=("[^"]*")/i', 'src="'.site_url('/thumbs/'.$imageData['imageRef'].strtolower($imageData['ext'])).'"', $imageHTML);
  649. $template[$headlineID][$i] = array(
  650. 'galleryimage:link' => site_url('images/'.$imageData['imageRef'].$imageData['ext']),
  651. 'galleryimage:title' => $imageData['imageName'],
  652. 'galleryimage:image' => $imageHTML,
  653. 'galleryimage:thumb' => $thumbHTML,
  654. 'galleryimage:filename' => $imageData['imageRef'].$imageData['ext'],
  655. 'galleryimage:date' => dateFmt($imageData['dateCreated'], ($this->CI->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
  656. 'galleryimage:author' => $this->CI->images->lookup_user($imageData['userID'], TRUE),
  657. 'galleryimage:author-id' => $imageData['userID'],
  658. 'galleryimage:class' => $imageData['class']
  659. );
  660. $i++;
  661. }
  662. }
  663. }
  664. else
  665. {
  666. $template[$headlineID] = array();
  667. }
  668. }
  669. }
  670. // get shop gateway
  671. if (preg_match('/{shop:(.+)}|{headlines:shop/i', $body))
  672. {
  673. // load messages model
  674. $this->CI->load->model('shop/shop_model', 'shop');
  675. // shop globals
  676. $template['shop:email'] = $this->CI->site->config['shopEmail'];
  677. $template['shop:paypal'] = $this->CI->shop->paypal_url;
  678. $template['shop:gateway'] = ($this->CI->site->config['shopGateway'] == 'sagepay' || $this->CI->site->config['shopGateway'] == 'authorize') ? site_url('/shop/checkout') : $this->CI->shop->gateway_url;
  679. // get shop headlines
  680. if (preg_match_all('/{headlines:shop(:category\(([A-Za-z0-9_-]+)\))?(:limit\(([0-9]+)\))?}/i', $body, $matches))
  681. {
  682. // filter matches
  683. $headlineID = preg_replace('/{|}/', '', $matches[0][0]);
  684. $limit = ($matches[4][0]) ? $matches[4][0] : $this->CI->site->config['headlines'];
  685. $catSafe = $matches[2][0];
  686. // get latest posts
  687. if ($headlines = $this->CI->shop->get_latest_products($catSafe, $limit))
  688. {
  689. // fill up template array
  690. $i = 0;
  691. foreach ($headlines as $headline)
  692. {
  693. // get body and excerpt
  694. $headlineBody = (strlen($headline['description']) > 100) ? substr($headline['description'], 0, 100).'...' : $headline['description'];
  695. $headlineExcerpt = nl2br($headline['excerpt']);
  696. // get images
  697. if (!$headlineImage = $this->CI->uploads->load_image($headline['productID'], false, true))
  698. {
  699. $headlineImage['src'] = $this->CI->config->item('staticPath').'/images/nopicture.jpg';
  700. }
  701. // get images
  702. if (!$headlineThumb = $this->CI->uploads->load_image($headline['productID'], true, true))
  703. {
  704. $headlineThumb['src'] = $this->CI->config->item('staticPath').'/images/nopicture.jpg';
  705. }
  706. // populate template
  707. $template[$headlineID][$i] = array(
  708. 'headline:id' => $headline['productID'],
  709. 'headline:link' => site_url('shop/'.$headline['productID'].'/'.strtolower(url_title($headline['productName']))),
  710. 'headline:title' => $headline['productName'],
  711. 'headline:subtitle' => $headline['subtitle'],
  712. 'headline:date' => dateFmt($headline['dateCreated'], ($this->CI->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
  713. 'headline:body' => $headlineBody,
  714. 'headline:excerpt' => $headlineExcerpt,
  715. 'headline:price' => currency_symbol().number_format($headline['price'],2),
  716. 'headline:image-path' => $headlineImage['src'],
  717. 'headline:thumb-path' => $headlineThumb['src'],
  718. 'headline:cell-width' => floor(( 1 / $limit) * 100),
  719. 'headline:price' => currency_symbol().number_format($headline['price'],2),
  720. 'headline:stock' => $headline['stock'],
  721. 'headline:class' => ($i % 2) ? ' alt ' : ''
  722. );
  723. $i++;
  724. }
  725. }
  726. else
  727. {
  728. $template[$headlineID] = array();
  729. }
  730. }
  731. // get shop headlines
  732. if (preg_match_all('/{headlines:shop:featured(:limit(\(([0-9]+)\))?)?}/i', $body, $matches))
  733. {
  734. // filter matches
  735. $headlineID = preg_replace('/{|}/', '', $matches[0][0]);
  736. $limit = ($matches[3][0]) ? $matches[3][0] : $this->CI->site->config['headlines'];
  737. // get latest posts
  738. if ($headlines = $this->CI->shop->get_latest_featured_products($limit))
  739. {
  740. // fill up template array
  741. $i = 0;
  742. foreach ($headlines as $headline)
  743. {
  744. // get body and excerpt
  745. $headlineBody = (strlen($headline['description']) > 100) ? substr($headline['description'], 0, 100).'...' : $headline['description'];
  746. $headlineExcerpt = nl2br($headline['excerpt']);
  747. // get images
  748. if (!$headlineImage = $this->CI->uploads->load_image($headline['productID'], false, true))
  749. {
  750. $headlineImage['src'] = $this->CI->config->item('staticPath').'/images/nopicture.jpg';
  751. }
  752. // get thumb
  753. if (!$headlineThumb = $this->CI->uploads->load_image($headline['productID'], true, true))
  754. {
  755. $headlineThumb['src'] = $this->CI->config->item('staticPath').'/images/nopicture.jpg';
  756. }
  757. $template[$headlineID][$i] = array(
  758. 'headline:id' => $headline['productID'],
  759. 'headline:link' => site_url('shop/'.$headline['productID'].'/'.strtolower(url_title($headline['productName']))),
  760. 'headline:title' => $headline['productName'],
  761. 'headline:subtitle' => $headline['subtitle'],
  762. 'headline:date' => dateFmt($headline['dateCreated'], ($this->CI->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
  763. 'headline:body' => $headlineBody,
  764. 'headline:excerpt' => $headlineExcerpt,
  765. 'headline:price' => currency_symbol().number_format($headline['price'],2),
  766. 'headline:image-path' => $headlineImage['src'],
  767. 'headline:thumb-path' => $headlineThumb['src'],
  768. 'headline:cell-width' => floor(( 1 / $limit) * 100),
  769. 'headline:price' => currency_symbol().number_format($headline['price'],2),
  770. 'headline:stock' => $headline['stock'],
  771. 'headline:class' => ($i % 2) ? ' alt ' : ''
  772. );
  773. $i++;
  774. }
  775. }
  776. else
  777. {
  778. $template[$headlineID] = array();
  779. }
  780. }
  781. // get shop cart headlines
  782. if (preg_match('/({headlines:shop:((.+)?)})+/i', $body))
  783. {
  784. // get shopping cart
  785. $cart = $this->CI->shop->load_cart();
  786. // get latest posts
  787. if ($headlines = $cart['cart'])
  788. {
  789. // fill up template array
  790. $i = 0;
  791. foreach ($headlines as $headline)
  792. {
  793. $template['headlines:shop:cartitems'][$i] = array(
  794. 'headline:link' => site_url('shop/'.$headline['productID'].'/'.strtolower(url_title($headline['productName']))),
  795. 'headline:title' => $headline['productName'],
  796. 'headline:quantity' => $headline['quantity'],
  797. 'headline:price' => currency_symbol().(number_format($headline['price'] * $headline['quantity'], 2)),
  798. 'headline:class' => ($i % 2) ? ' alt ' : ''
  799. );
  800. $i++;
  801. }
  802. $template['headlines:shop:numitems'] = count($headlines);
  803. $template['headlines:shop:subtotal'] = currency_symbol().number_format($cart['subtotal'], 2);
  804. }
  805. else
  806. {
  807. $template['headlines:shop:numitems'] = 0;
  808. $template['headlines:shop:subtotal'] = currency_symbol().number_format(0, 2);
  809. $template['headlines:shop:cartitems'] = array();
  810. }
  811. }
  812. // get shop navigation
  813. if (preg_match('/({shop:categories((.+)?)})+/i', $body))
  814. {
  815. $template['shop:categories'] = '';
  816. if ($categories = $this->CI->shop->get_category_parents())
  817. {
  818. $i = 1;
  819. foreach($categories as $nav)
  820. {
  821. // get subnav
  822. if ($children = $this->CI->shop->get_category_children($nav['catID']))
  823. {
  824. $template['shop:categories'] .= '<li class="expanded ';
  825. if ($i == 1)
  826. {
  827. $template['shop:categories'] .= 'first ';
  828. }
  829. if ($i == sizeof($categories))
  830. {
  831. $template['shop:categories'] .= 'last ';
  832. }
  833. $template['shop:categories'] .= '"><a href="/shop/'.$nav['catSafe'].'">'.htmlentities($nav['catName'], NULL, 'UTF-8').'</a><ul class="subnav">';
  834. foreach($children as $child)
  835. {
  836. $template['shop:categories'] .= '<li class="';
  837. if ($child['catID'] == $this->CI->uri->segment(3) || $nav['catSafe'] == $this->CI->uri->segment(2))
  838. {
  839. $template['shop:categories'] .= 'active selected';
  840. }
  841. $template['shop:categories'] .= '"><a href="/shop/'.$nav['catSafe'].'/'.$child['catSafe'].'">'.htmlentities($child['catName'], NULL, 'UTF-8').'</a></li>';
  842. }
  843. $template['shop:categories'] .= '</ul>';
  844. }
  845. else
  846. {
  847. $template['shop:categories'] .= '<li class="';
  848. if ($nav['catID'] == $this->CI->uri->segment(3) || $nav['catSafe'] == $this->CI->uri->segment(2))
  849. {
  850. $template['shop:categories'] .= 'active selected ';
  851. }
  852. if ($i == 1)
  853. {
  854. $template['shop:categories'] .= 'first ';
  855. }
  856. if ($i == sizeof($categories))
  857. {
  858. $template['shop:categories'] .= 'last ';
  859. }
  860. $template['shop:categories'] .= '"><a href="/shop/'.$nav['catSafe'].'">'.htmlentities($nav['catName'], NULL, 'UTF-8').'</a>';
  861. }
  862. $template['shop:categories'] .= '</li>';
  863. $i++;
  864. }
  865. }
  866. }
  867. }
  868. // message centre stuff
  869. if (preg_match('/({((.+)?)messages:unread((.+)?)})+/i', $body))
  870. {
  871. // load messages model
  872. $this->CI->load->model('community/messages_model', 'messages');
  873. // get message count
  874. @$template['messages:unread'] = ($messageCount = $this->CI->messages->get_unread_message_count()) ? $messageCount : 0;
  875. }
  876. return $template;
  877. }
  878. function parse_images($body)
  879. {
  880. // parse for images
  881. preg_match_all('/image\:([a-z0-9\-_]+)/i', $body, $images);
  882. if ($images)
  883. {
  884. foreach($images[1] as $image => $value)
  885. {
  886. $imageHTML = '';
  887. if ($imageData = $this->get_image($value))
  888. {
  889. $imageHTML = display_image($imageData['src'], $imageData['imageName'], $imageData['maxsize'], 'id="'.$this->CI->core->encode($this->CI->session->userdata('lastPage').'|'.$imageData['imageID']).'" class="pic '.$imageData['class'].'"');
  890. $imageHTML = preg_replace('/src=("[^"]*")/i', 'src="'.site_url('/images/'.$imageData['imageRef'].strtolower($imageData['ext'])).'"', $imageHTML);
  891. }
  892. elseif ($this->CI->session->userdata('session_admin'))
  893. {
  894. $imageHTML = '<a href="'.site_url('/admin/images').'" target="_parent"><img src="'.$this->CI->config->item('staticPath').'/images/btn_upload.png" alt="Upload Image" /></a>';
  895. }
  896. $body = str_replace('{image:'.$value.'}', $imageHTML, $body);
  897. }
  898. }
  899. // parse for thumbs
  900. preg_match_all('/thumb\:([a-z0-9\-_]+)/i', $body, $images);
  901. if ($images)
  902. {
  903. foreach($images[1] as $image => $value)
  904. {
  905. $imageHTML = '';
  906. if ($imageData = $this->get_image($value))
  907. {
  908. $imageHTML = display_image($imageData['thumbnail'], $imageData['imageName'], $imageData['maxsize'], 'id="'.$this->CI->core->encode($this->CI->session->userdata('lastPage').'|'.$imageData['imageID']).'" class="pic thumb '.$imageData['class'].'"');
  909. $imageHTML = preg_replace('/src=("[^"]*")/i', 'src="/thumbs/'.$imageData['imageRef'].strtolower($imageData['ext']).'"', $imageHTML);
  910. }
  911. elseif ($this->CI->session->userdata('session_admin'))
  912. {
  913. $imageHTML = '<a href="'.site_url('/admin/images').'" target="_parent"><img src="'.$this->CI->config->item('staticPath').'/images/btn_upload.png" alt="Upload Image" /></a>';
  914. }
  915. $body = str_replace('{thumb:'.$value.'}', $imageHTML, $body);
  916. }
  917. }
  918. return $body;
  919. }
  920. function get_image($imageRef)
  921. {
  922. $this->CI->db->where('siteID', $this->siteID);
  923. $this->CI->db->where('deleted', 0);
  924. $this->CI->db->where('imageRef', $imageRef);
  925. $query = $this->CI->db->get('images');
  926. // get data
  927. if ($query->num_rows())
  928. {
  929. // path to uploads
  930. $pathToUploads = $this->uploadsPath;
  931. $row = $query->row_array();
  932. $image = $row['filename'];
  933. $ext = substr($image,strpos($image,'.'));
  934. $imagePath = $pathToUploads.'/'.$image;
  935. $thumbPath = str_replace($ext, '', $imagePath).'_thumb'.$ext;
  936. $row['ext'] = $ext;
  937. $row['src'] = $imagePath;
  938. $row['thumbnail'] = (file_exists('.'.$thumbPath)) ? $thumbPath : $imagePath;
  939. return $row;
  940. }
  941. else
  942. {
  943. return FALSE;
  944. }
  945. }
  946. function parse_files($body)
  947. {
  948. // parse for files
  949. preg_match_all('/file\:([a-z0-9\-_]+)/i', $body, $files);
  950. if ($files)
  951. {
  952. foreach($files[1] as $file => $value)
  953. {
  954. $fileData = $this->get_file($value);
  955. $body = str_replace('{file:'.$value.'}', anchor('/files/'.$fileData['fileRef'].$fileData['extension'], 'Download', 'class="file '.str_replace('.', '', $fileData['extension']).'"'), $body);
  956. }
  957. }
  958. return $body;
  959. }
  960. function get_file($fileRef)
  961. {
  962. // get data
  963. if ($file = $this->CI->uploads->load_file($fileRef, TRUE))
  964. {
  965. return $file;
  966. }
  967. else
  968. {
  969. return false;
  970. }
  971. }
  972. }