PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/halogy/application/libraries/Template.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 1094 lines | 818 code | 144 blank | 132 comment | 124 complexity | dc09db849d48da51f56e14aed10666af 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'] = $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, $group = '')
  163. {
  164. $template[$navTag] = '';
  165. $template[$navTag.':parents'] = '';
  166. if ($navigation = $this->get_nav_parents($group))
  167. {
  168. $i = 1;
  169. foreach($navigation as $nav)
  170. {
  171. // get subnav
  172. $children = $this->get_nav_children($nav['navID']);
  173. // set last state on menu
  174. $class = '';
  175. if ($i == 1)
  176. {
  177. $class .= 'first ';
  178. }
  179. // set last state on menu
  180. if (sizeof($navigation) == $i)
  181. {
  182. $class .= 'last ';
  183. }
  184. // parse the nav item for the link
  185. $template[$navTag] .= $this->parse_nav_item($nav['uri'], $nav['navName'], $children, $class);
  186. $template[$navTag.':parents'] .= $this->parse_nav_item($nav['uri'], $nav['navName'], $children, $class);
  187. // output subnav
  188. if ($children)
  189. {
  190. $template[$navTag] .= '<ul class="subnav">';
  191. foreach($children as $child)
  192. {
  193. $navItem = $this->parse_nav_item($child['uri'], $child['navName']).'</li>';
  194. $template[$navTag] .= $navItem;
  195. }
  196. $template[$navTag] .= '</ul>';
  197. }
  198. $template[$navTag] .= '</li>';
  199. $template[$navTag.':parents'] .= '</li>';
  200. $i++;
  201. }
  202. }
  203. // get children for this segment
  204. $template[$navTag.':children'] = $this->parse_navigation_children();
  205. return $template;
  206. }
  207. function parse_navigation_children()
  208. {
  209. $output = '';
  210. // get children for this uri
  211. if ($navigation = $this->get_nav())
  212. {
  213. foreach($navigation as $nav)
  214. {
  215. // get subnav
  216. if ($children = $this->get_nav_children($nav['navID']))
  217. {
  218. foreach($children as $child)
  219. {
  220. $navItem = $this->parse_nav_item($child['uri'], $child['navName']).'</li>';
  221. if ($this->CI->uri->segment(1) == $nav['uri'] ||
  222. (
  223. ($this->CI->uri->uri_string() == '' || $this->CI->uri->segment(1) == 'home') &&
  224. ($nav['uri'] == '/' || $nav['uri'] == 'home')
  225. )
  226. )
  227. {
  228. $output .= $navItem;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. return $output;
  235. }
  236. function parse_nav_item($uri, $name, $children = FALSE, $class = '')
  237. {
  238. // init stuff
  239. $output = '';
  240. $childs = array();
  241. // tidy children array
  242. if ($children)
  243. {
  244. foreach($children as $child)
  245. {
  246. $childs[] = $child['uri'];
  247. }
  248. }
  249. // set active state on menu
  250. $currentNav = $uri;
  251. $output .= '<li class="';
  252. if (($currentNav != '/' && preg_match('/^'.str_replace('/','\/',$currentNav).'/i', substr($this->CI->uri->uri_string(), 1))) ||
  253. $currentNav == $this->CI->uri->segment(1) ||
  254. (($currentNav == '' || $currentNav == 'home' || $currentNav == '/') &&
  255. ($this->CI->uri->uri_string() == '' || $this->CI->uri->uri_string() == '/home' || $this->CI->uri->uri_string() == '/')) ||
  256. @in_array(substr($this->CI->uri->uri_string(),1), $childs)
  257. )
  258. {
  259. $class .= 'active selected ';
  260. }
  261. if ($children)
  262. {
  263. $class .= 'expanded ';
  264. }
  265. // filter uri to make sure it's cool
  266. if (substr($uri,0,1) == '/')
  267. {
  268. $href = $uri;
  269. }
  270. elseif (stristr($uri,'http://'))
  271. {
  272. $href = $uri;
  273. }
  274. elseif (stristr($uri,'www.'))
  275. {
  276. $href = 'http://'.$uri;
  277. }
  278. elseif (stristr($uri,'mailto:'))
  279. {
  280. $href = $uri;
  281. }
  282. elseif ($uri == 'home')
  283. {
  284. $href = '/';
  285. }
  286. else
  287. {
  288. $href = '/'.$uri;
  289. }
  290. $output .= trim($class).'"><a href="'.$href.'" class="'.trim($class).'"><span>'.htmlentities($name).'</span></a>';
  291. return $output;
  292. }
  293. function get_nav($navID = '')
  294. {
  295. // default where
  296. $this->CI->db->where(array('siteID' => $this->siteID, 'deleted' => 0));
  297. // where parent is set
  298. $this->CI->db->where('parentID', 0);
  299. // get navigation from pages
  300. $this->CI->db->select('uri, pageID as navID, pageName as navName');
  301. $this->CI->db->order_by('pageOrder', 'asc');
  302. $query = $this->CI->db->get('pages');
  303. if ($query->num_rows())
  304. {
  305. return $query->result_array();
  306. }
  307. else
  308. {
  309. return false;
  310. }
  311. }
  312. function get_nav_parents()
  313. {
  314. // default where
  315. $this->CI->db->where(array('siteID' => $this->siteID, 'deleted' => 0));
  316. // where parent is set
  317. $this->CI->db->where('parentID', 0);
  318. // get navigation from pages
  319. $this->CI->db->select('uri, pageID as navID, pageName as navName');
  320. // nav has to be active because its parents
  321. $this->CI->db->where('navigation', 1);
  322. $this->CI->db->order_by('pageOrder', 'asc');
  323. $query = $this->CI->db->get('pages');
  324. if ($query->num_rows())
  325. {
  326. return $query->result_array();
  327. }
  328. else
  329. {
  330. return false;
  331. }
  332. }
  333. function get_nav_children($navID = '', $group = '')
  334. {
  335. // default where
  336. $this->CI->db->where(array('siteID' => $this->siteID, 'deleted' => 0));
  337. // get nav by ID
  338. $this->CI->db->where('parentID', $navID);
  339. // get navigation from navigation table
  340. if ($group)
  341. {
  342. // nav has to be active
  343. $this->CI->db->where('active', 1);
  344. $this->CI->db->order_by('navOrder', 'asc');
  345. $query = $this->CI->db->get('navigation');
  346. }
  347. // just select from pages
  348. else
  349. {
  350. // nav has to be active
  351. $this->CI->db->select('uri, pageID as navID, pageName as navName');
  352. $this->CI->db->where('navigation', 1);
  353. $this->CI->db->order_by('pageOrder', 'asc');
  354. $query = $this->CI->db->get('pages');
  355. }
  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 gallery
  599. if (preg_match_all('/{gallery:([A-Za-z0-9_-]+)(:limit\(([0-9]+)\))?}/i', $body, $matches))
  600. {
  601. // load libs etc
  602. $this->CI->load->model('images/images_model', 'images');
  603. // filter through matches
  604. for ($x = 0; $x < sizeof($matches[0]); $x++)
  605. {
  606. // filter matches
  607. $headlineID = preg_replace('/{|}/', '', $matches[0][0]);
  608. $limit = ($matches[3][$x]) ? $matches[3][$x] : 9;
  609. // get latest posts
  610. if ($gallery = $this->CI->images->get_images_by_folder_ref($matches[1][$x], $limit))
  611. {
  612. // fill up template array
  613. $i = 0;
  614. foreach ($gallery as $galleryimage)
  615. {
  616. if ($imageData = $this->get_image($galleryimage['imageRef']))
  617. {
  618. $imageHTML = display_image($imageData['src'], $imageData['imageName']);
  619. $imageHTML = preg_replace('/src=("[^"]*")/i', 'src="/images/'.$imageData['imageRef'].strtolower($imageData['ext']).'"', $imageHTML);
  620. $thumbTML = display_image($imageData['src'], $imageData['imageName']);
  621. $thumbHTML = preg_replace('/src=("[^"]*")/i', 'src="/thumbs/'.$imageData['imageRef'].strtolower($imageData['ext']).'"', $imageHTML);
  622. $template[$headlineID][$i] = array(
  623. 'galleryimage:link' => site_url('images/'.$imageData['imageRef'].$imageData['ext']),
  624. 'galleryimage:title' => $imageData['imageName'],
  625. 'galleryimage:image' => $imageHTML,
  626. 'galleryimage:thumb' => $thumbHTML,
  627. 'galleryimage:filename' => $imageData['imageRef'].'.'.$imageData['ext'],
  628. 'galleryimage:date' => dateFmt($imageData['dateCreated'], ($this->CI->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
  629. 'galleryimage:author' => $this->CI->images->lookup_user($imageData['userID'], TRUE),
  630. 'galleryimage:author-id' => $imageData['userID'],
  631. 'galleryimage:class' => $imageData['class']
  632. );
  633. $i++;
  634. }
  635. }
  636. }
  637. else
  638. {
  639. $template[$headlineID] = array();
  640. }
  641. }
  642. }
  643. // get shop gateway
  644. if (preg_match('/{shop:(.+)}|{headlines:shop/i', $body))
  645. {
  646. // load messages model
  647. $this->CI->load->model('shop/shop_model', 'shop');
  648. // shop globals
  649. $template['shop:email'] = $this->CI->site->config['shopEmail'];
  650. $template['shop:paypal'] = $this->CI->shop->paypal_url;
  651. $template['shop:gateway'] = ($this->CI->site->config['shopGateway'] == 'sagepay' || $this->CI->site->config['shopGateway'] == 'authorize') ? site_url('/shop/checkout') : $this->CI->shop->gateway_url;
  652. // get shop headlines
  653. if (preg_match_all('/{headlines:shop(:category\(([A-Za-z0-9_-]+)\))?(:limit\(([0-9]+)\))?}/i', $body, $matches))
  654. {
  655. // filter matches
  656. $headlineID = preg_replace('/{|}/', '', $matches[0][0]);
  657. $limit = ($matches[4][0]) ? $matches[4][0] : $this->CI->site->config['headlines'];
  658. $catSafe = $matches[2][0];
  659. // get latest posts
  660. if ($headlines = $this->CI->shop->get_latest_products($catSafe, $limit))
  661. {
  662. // fill up template array
  663. $i = 0;
  664. foreach ($headlines as $headline)
  665. {
  666. // get body and excerpt
  667. $headlineBody = (strlen($headline['description']) > 100) ? substr($headline['description'], 0, 100).'...' : $headline['description'];
  668. $headlineExcerpt = nl2br($headline['excerpt']);
  669. // get images
  670. if (!$headlineImage = $this->CI->uploads->load_image($headline['productID'], false, true))
  671. {
  672. $headlineImage['src'] = $this->CI->config->item('staticPath').'/images/nopicture.jpg';
  673. }
  674. // get images
  675. if (!$headlineThumb = $this->CI->uploads->load_image($headline['productID'], true, true))
  676. {
  677. $headlineThumb['src'] = $this->CI->config->item('staticPath').'/images/nopicture.jpg';
  678. }
  679. // populate template
  680. $template[$headlineID][$i] = array(
  681. 'headline:id' => $headline['productID'],
  682. 'headline:link' => site_url('shop/'.$headline['productID'].'/'.strtolower(url_title($headline['productName']))),
  683. 'headline:title' => $headline['productName'],
  684. 'headline:subtitle' => $headline['subtitle'],
  685. 'headline:date' => dateFmt($headline['dateCreated'], ($this->CI->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
  686. 'headline:body' => $headlineBody,
  687. 'headline:excerpt' => $headlineExcerpt,
  688. 'headline:price' => currency_symbol().number_format($headline['price'],2),
  689. 'headline:image-path' => $headlineImage['src'],
  690. 'headline:thumb-path' => $headlineThumb['src'],
  691. 'headline:cell-width' => floor(( 1 / $limit) * 100),
  692. 'headline:price' => currency_symbol().number_format($headline['price'],2),
  693. 'headline:stock' => $headline['stock'],
  694. 'headline:class' => ($i % 2) ? ' alt ' : ''
  695. );
  696. $i++;
  697. }
  698. }
  699. else
  700. {
  701. $template[$headlineID] = array();
  702. }
  703. }
  704. // get shop headlines
  705. if (preg_match_all('/{headlines:shop:featured(:limit(\(([0-9]+)\))?)?}/i', $body, $matches))
  706. {
  707. // filter matches
  708. $headlineID = preg_replace('/{|}/', '', $matches[0][0]);
  709. $limit = ($matches[3][0]) ? $matches[3][0] : $this->CI->site->config['headlines'];
  710. // get latest posts
  711. if ($headlines = $this->CI->shop->get_latest_featured_products($limit))
  712. {
  713. // fill up template array
  714. $i = 0;
  715. foreach ($headlines as $headline)
  716. {
  717. // get body and excerpt
  718. $headlineBody = (strlen($headline['description']) > 100) ? substr($headline['description'], 0, 100).'...' : $headline['description'];
  719. $headlineExcerpt = nl2br($headline['excerpt']);
  720. // get images
  721. if (!$headlineImage = $this->CI->uploads->load_image($headline['productID'], false, true))
  722. {
  723. $headlineImage['src'] = $this->CI->config->item('staticPath').'/images/nopicture.jpg';
  724. }
  725. // get thumb
  726. if (!$headlineThumb = $this->CI->uploads->load_image($headline['productID'], true, true))
  727. {
  728. $headlineThumb['src'] = $this->CI->config->item('staticPath').'/images/nopicture.jpg';
  729. }
  730. $template[$headlineID][$i] = array(
  731. 'headline:id' => $headline['productID'],
  732. 'headline:link' => site_url('shop/'.$headline['productID'].'/'.strtolower(url_title($headline['productName']))),
  733. 'headline:title' => $headline['productName'],
  734. 'headline:subtitle' => $headline['subtitle'],
  735. 'headline:date' => dateFmt($headline['dateCreated'], ($this->CI->site->config['dateOrder'] == 'MD') ? 'M jS Y' : 'jS M Y'),
  736. 'headline:body' => $headlineBody,
  737. 'headline:excerpt' => $headlineExcerpt,
  738. 'headline:price' => currency_symbol().number_format($headline['price'],2),
  739. 'headline:image-path' => $headlineImage['src'],
  740. 'headline:thumb-path' => $headlineThumb['src'],
  741. 'headline:cell-width' => floor(( 1 / $limit) * 100),
  742. 'headline:price' => currency_symbol().number_format($headline['price'],2),
  743. 'headline:stock' => $headline['stock'],
  744. 'headline:class' => ($i % 2) ? ' alt ' : ''
  745. );
  746. $i++;
  747. }
  748. }
  749. else
  750. {
  751. $template[$headlineID] = array();
  752. }
  753. }
  754. // get shop cart headlines
  755. if (preg_match('/({((.+)?)shop:cartitems((.+)?)})+|{shop:subtotal}/i', $body))
  756. {
  757. // get shopping cart
  758. $cart = $this->CI->shop->load_cart();
  759. // get latest posts
  760. if ($headlines = $cart['cart'])
  761. {
  762. // fill up template array
  763. $i = 0;
  764. foreach ($headlines as $headline)
  765. {
  766. $template['shop:cartitems'][$i] = array(
  767. 'cartitem:link' => site_url('shop/'.$headline['productID'].'/'.strtolower(url_title($headline['productName']))),
  768. 'cartitem:title' => $headline['productName'],
  769. 'cartitem:quantity' => $headline['quantity'],
  770. 'cartitem:price' => currency_symbol().(number_format($headline['price'] * $headline['quantity'], 2)),
  771. 'cartitem:class' => ($i % 2) ? ' alt ' : ''
  772. );
  773. $i++;
  774. }
  775. $template['shop:numitems'] = count($headlines);
  776. $template['shop:subtotal'] = currency_symbol().number_format($cart['subtotal'], 2);
  777. }
  778. else
  779. {
  780. $template['shop:numitems'] = 0;
  781. $template['shop:subtotal'] = currency_symbol().number_format(0, 2);
  782. $template['shop:cartitems'] = array();
  783. }
  784. }
  785. // get shop navigation
  786. if (preg_match('/({((.+)?)shop:categories((.+)?)})+/i', $body))
  787. {
  788. $template['shop:categories'] = '';
  789. if ($categories = $this->CI->shop->get_category_parents())
  790. {
  791. $i = 1;
  792. foreach($categories as $nav)
  793. {
  794. // get subnav
  795. if ($children = $this->CI->shop->get_category_children($nav['catID']))
  796. {
  797. $template['shop:categories'] .= '<li class="expanded ';
  798. if ($i == 1)
  799. {
  800. $template['shop:categories'] .= 'first ';
  801. }
  802. if ($i == sizeof($categories))
  803. {
  804. $template['shop:categories'] .= 'last ';
  805. }
  806. $template['shop:categories'] .= '"><a href="/shop/'.$nav['catSafe'].'">'.htmlentities($nav['catName'], NULL, 'UTF-8').'</a><ul class="subnav">';
  807. foreach($children as $child)
  808. {
  809. $template['shop:categories'] .= '<li class="';
  810. if ($child['catID'] == $this->CI->uri->segment(3) || $nav['catSafe'] == $this->CI->uri->segment(2))
  811. {
  812. $template['shop:categories'] .= 'active selected';
  813. }
  814. $template['shop:categories'] .= '"><a href="/shop/'.$nav['catSafe'].'/'.$child['catSafe'].'">'.htmlentities($child['catName'], NULL, 'UTF-8').'</a></li>';
  815. }
  816. $template['shop:categories'] .= '</ul>';
  817. }
  818. else
  819. {
  820. $template['shop:categories'] .= '<li class="';
  821. if ($nav['catID'] == $this->CI->uri->segment(3) || $nav['catSafe'] == $this->CI->uri->segment(2))
  822. {
  823. $template['shop:categories'] .= 'active selected ';
  824. }
  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>';
  834. }
  835. $template['shop:categories'] .= '</li>';
  836. $i++;
  837. }
  838. }
  839. }
  840. }
  841. // message centre stuff
  842. if (preg_match('/({((.+)?)messages:unread((.+)?)})+/i', $body))
  843. {
  844. // load messages model
  845. $this->CI->load->model('community/messages_model', 'messages');
  846. // get message count
  847. @$template['messages:unread'] = ($messageCount = $this->CI->messages->get_unread_message_count()) ? $messageCount : 0;
  848. }
  849. return $template;
  850. }
  851. function parse_images($body)
  852. {
  853. // parse for images
  854. preg_match_all('/image\:([a-z0-9\-_]+)/i', $body, $images);
  855. if ($images)
  856. {
  857. foreach($images[1] as $image => $value)
  858. {
  859. $imageHTML = '';
  860. if ($imageData = $this->get_image($value))
  861. {
  862. $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'].'"');
  863. $imageHTML = preg_replace('/src=("[^"]*")/i', 'src="/images/'.$imageData['imageRef'].strtolower($imageData['ext']).'"', $imageHTML);
  864. }
  865. elseif ($this->CI->session->userdata('session_admin'))
  866. {
  867. $imageHTML = '<a href="/admin/images" target="_parent"><img src="'.$this->CI->config->item('staticPath').'/images/btn_upload.png" alt="Upload Image" /></a>';
  868. }
  869. $body = str_replace('{image:'.$value.'}', $imageHTML, $body);
  870. }
  871. }
  872. // parse for thumbs
  873. preg_match_all('/thumb\:([a-z0-9\-_]+)/i', $body, $images);
  874. if ($images)
  875. {
  876. foreach($images[1] as $image => $value)
  877. {
  878. $imageHTML = '';
  879. if ($imageData = $this->get_image($value))
  880. {
  881. $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'].'"');
  882. $imageHTML = preg_replace('/src=("[^"]*")/i', 'src="/thumbs/'.$imageData['imageRef'].strtolower($imageData['ext']).'"', $imageHTML);
  883. }
  884. elseif ($this->CI->session->userdata('session_admin'))
  885. {
  886. $imageHTML = '<a href="/admin/images" target="_parent"><img src="'.$this->CI->config->item('staticPath').'/images/btn_upload.png" alt="Upload Image" /></a>';
  887. }
  888. $body = str_replace('{thumb:'.$value.'}', $imageHTML, $body);
  889. }
  890. }
  891. return $body;
  892. }
  893. function get_image($imageRef)
  894. {
  895. $this->CI->db->where('siteID', $this->siteID);
  896. $this->CI->db->where('deleted', 0);
  897. $this->CI->db->where('imageRef', $imageRef);
  898. $query = $this->CI->db->get('images');
  899. // get data
  900. if ($query->num_rows())
  901. {
  902. // path to uploads
  903. $pathToUploads = $this->uploadsPath;
  904. $row = $query->row_array();
  905. $image = $row['filename'];
  906. $ext = substr($image,strpos($image,'.'));
  907. $imagePath = $pathToUploads.'/'.$image;
  908. $thumbPath = str_replace($ext, '', $imagePath).'_thumb'.$ext;
  909. $row['ext'] = $ext;
  910. $row['src'] = $imagePath;
  911. $row['thumbnail'] = (file_exists('.'.$thumbPath)) ? $thumbPath : $imagePath;
  912. return $row;
  913. }
  914. else
  915. {
  916. return FALSE;
  917. }
  918. }
  919. function parse_files($body)
  920. {
  921. // parse for files
  922. preg_match_all('/file\:([a-z0-9\-_]+)/i', $body, $files);
  923. if ($files)
  924. {
  925. foreach($files[1] as $file => $value)
  926. {
  927. $fileData = $this->get_file($value);
  928. $body = str_replace('{file:'.$value.'}', anchor(site_url('/files/'.$fileData['fileRef'].$fileData['extension']), 'Download', 'class="file '.str_replace('.', '', $fileData['extension']).'"'), $body);
  929. }
  930. }
  931. return $body;
  932. }
  933. function get_file($fileRef)
  934. {
  935. // get data
  936. if ($file = $this->CI->uploads->load_file($fileRef, TRUE))
  937. {
  938. return $file;
  939. }
  940. else
  941. {
  942. return false;
  943. }
  944. }
  945. }