PageRenderTime 128ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 2ms

/halogy/libraries/Template.php

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