PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/public/shop/core/engine/layout.php

https://bitbucket.org/rusbal/club4causes
PHP | 432 lines | 315 code | 59 blank | 58 comment | 39 complexity | 03bfb7f03208237b1dc87a5695439c64 MD5 | raw file
  1. <?php
  2. /*------------------------------------------------------------------------------
  3. $Id$
  4. Shopping Cart
  5. http://dev.club4causes.com/shop
  6. Copyright © 2011, 2012 Club4Causes
  7. This source file is subject to Open Software License (OSL 3.0)
  8. License details is bundled with this package in the file LICENSE.txt.
  9. It is also available at this URL:
  10. <http://www.opensource.org/licenses/OSL-3.0>
  11. UPGRADE NOTE:
  12. Do not edit or add to this file if you wish to upgrade Shopping Cart to newer
  13. versions in the future. If you wish to customize Shopping Cart for your
  14. needs please refer to http://dev.club4causes.com/shop for more information.
  15. ------------------------------------------------------------------------------*/
  16. if (! defined ( 'DIR_CORE' )) {
  17. header ( 'Location: static_pages/' );
  18. }
  19. class ALayout {
  20. protected $registry;
  21. private $page = array();
  22. private $layout = array();
  23. public $blocks = array();
  24. private $tmpl_id;
  25. private $layout_id;
  26. public $page_id;
  27. public function __construct($registry, $template_id) {
  28. $this->registry = $registry;
  29. $this->tmpl_id = $template_id;
  30. $this->page_id = '';
  31. }
  32. public function __get($key) {
  33. return $this->registry->get($key);
  34. }
  35. public function __set($key, $value) {
  36. $this->registry->set($key, $value);
  37. }
  38. public function buildPageData($controller) {
  39. // Locate and set page information. This needs to be called once per page
  40. $unique_page = array();
  41. $layouts = array();
  42. // find page records for given controller
  43. $key_param = $this->getKeyParamByController($controller);
  44. $key_value = $key_param ? $this->request->get[$key_param] : null;
  45. // for nested categories
  46. if($key_param=='path' && $key_value && is_int(strpos($key_value,'_'))){
  47. $key_value = (int)substr($key_value,strrpos($key_value,'_')+1);
  48. }
  49. $key_param = is_null($key_value) ? null : $key_param;
  50. $pages = $this->getPages($controller, $key_param,$key_value);
  51. if ( empty($pages) ) {
  52. //if no specific page found try to get page for group
  53. $new_path = preg_replace('/\/\w*$/', '', $controller);
  54. $pages = $this->getPages($new_path);
  55. }
  56. if ( empty($pages) ) {
  57. //if no specific page found load generic
  58. $pages = $this->getPages('generic');
  59. }else{
  60. /* if specific pages with key_param presents...
  61. in any case first row will be that what we need (see sql "order by" in getPages method) */
  62. $unique_page = $pages[0];
  63. }
  64. // look for key_param and key_value in the request
  65. //!!!!! NEW Discovery
  66. /*
  67. Steps to perform
  68. 1. Based on rt (controller) select all rows from pages table where controller = "$controller"
  69. 2. Based on $key_param = key_param from pages table for given $controller locate this $key_param value from CGI input.
  70. You will have $key_param and $key_value pair.
  71. NOTE: key_param will be unique per controller. More optimized select can be used to get key_param from pages table.
  72. 3. Locate id from pages table based on $key_param and $key_value pair
  73. where controller = "$controller" and key_param = $key_param and key_value = $this->request->get[$key_param];
  74. NOTE: Do select only if value present.
  75. 4. If locate page id use the layout.
  76. */
  77. $this->page = !empty($unique_page) ? $unique_page : $pages[0];
  78. $this->page_id = $this->page['page_id'];
  79. //if no page found set default page id 1
  80. if (empty($this->page_id)) {
  81. $this->page_id = 1;
  82. }
  83. //Get the page layout
  84. $layouts = $this->getLayouts(1);
  85. if (sizeof($layouts) == 0) {
  86. //No page specific layout, load default layout
  87. $layouts = $this->getDefaultLayout();
  88. if (sizeof($layouts) == 0) {
  89. // ????? How to terminate ????
  90. throw new AException(AC_ERR_LOAD_LAYOUT, 'No layout found for page_id/controller '.$this->page_id.'::'.$this->page['controller'].'!');
  91. }
  92. }
  93. $this->layout = $layouts[0];
  94. $this->layout_id = $this->layout['layout_id'];
  95. // Get all blacks for the page;
  96. $blocks = $this->getlayoutBlocks($this->layout_id);
  97. $this->blocks = $blocks;
  98. return $this->page_id;
  99. }
  100. public function getPages($controller = '', $key_param = '', $key_value = '') {
  101. $cache_name = 'layout.pages'
  102. .( !empty($controller) ? '.'.$controller : '' )
  103. .( !empty($key_param) ? '.'. $key_param : '' )
  104. .( !empty($key_value) ? '.'.$key_value : '' );
  105. $cache_name = preg_replace('/[^a-zA-Z0-9\.]/', '', $cache_name);
  106. $pages = $this->cache->get( $cache_name, '', (int)$this->config->get('config_store_id') );
  107. if (isset($pages)) {
  108. // return cached pages
  109. return $pages;
  110. }
  111. $where = '';
  112. if (! empty ( $controller ) ){
  113. $where = "WHERE controller = '" . $this->db->escape ( $controller ) . "' ";
  114. if( ! empty ( $key_param ) ) {
  115. if( ! empty ( $key_value ) ){
  116. // so if we have key_param key_value pair we select pages with controller and with or without key_param
  117. $where .= " AND ( COALESCE( key_param, '' ) = ''
  118. OR
  119. ( key_param = '" . $this->db->escape ( $key_param ) . "'
  120. AND key_value = '" . $this->db->escape ( $key_value ) . "' ) )";
  121. } else { //write to log this stuff. it's abnormal situation
  122. $message = "Error: Error in data of page with controller: '".$controller."'. Please check for key_value present where key_param was set";
  123. $this->messages->saveError('Error',$message);
  124. $error = new AError ( $message );
  125. $error->toLog ()->toDebug ();
  126. }
  127. }
  128. }
  129. $sql = "SELECT page_id, controller, key_param, key_value, created, updated "
  130. . "FROM ".DB_PREFIX . "pages "
  131. . $where
  132. . "ORDER BY key_param DESC, key_value DESC, page_id ASC";
  133. $query = $this->db->query($sql);
  134. $pages = $query->rows;
  135. $this->cache->set($cache_name, $pages, '', (int)$this->config->get('config_store_id'));
  136. return $pages;
  137. }
  138. public function getKeyParamByController($controller = ''){
  139. $key = null;
  140. switch($controller){
  141. case 'pages/product/product':
  142. $key = 'product_id';
  143. break;
  144. case 'pages/product/manufacturer':
  145. $key = 'manufacturer_id';
  146. break;
  147. case 'pages/product/category':
  148. $key = 'path';
  149. break;
  150. case 'pages/content/content':
  151. $key = 'content_id';
  152. break;
  153. default:
  154. $key = null;
  155. break;
  156. }
  157. return $key;
  158. }
  159. public function getDefaultLayout() {
  160. $cache_name = 'layout.default.'. $this->tmpl_id;
  161. $cache_name = preg_replace('/[^a-zA-Z0-9\.]/', '', $cache_name);
  162. $layouts = $this->cache->get($cache_name,'', (int)$this->config->get('config_store_id'));
  163. if (isset($layouts)) {
  164. // return cached layouts
  165. return $layouts;
  166. }
  167. $where = 'WHERE template_id = "' . $this->db->escape($this->tmpl_id) . '" ';
  168. $where .= "AND layout_type = '0'";
  169. $sql = "SELECT "
  170. . "layout_id, "
  171. . "layout_type, "
  172. . "layout_name, "
  173. . "created, "
  174. . "updated "
  175. . "FROM "
  176. . DB_PREFIX . "layouts "
  177. . $where
  178. . " ORDER BY "
  179. . "layout_id Asc";
  180. $query = $this->db->query($sql);
  181. $layouts = $query->rows;
  182. $this->cache->set($cache_name, $layouts,'', (int)$this->config->get('config_store_id'));
  183. return $layouts;
  184. }
  185. public function getLayouts($layout_type = '') {
  186. //No page id, not need to be here
  187. if (empty($this->page_id)) {
  188. return;
  189. }
  190. $cache_name = 'layout.layouts.' . $this->tmpl_id . '.' . $this->page_id
  191. .( !empty($layout_type) ? '.'.$layout_type : '' );
  192. $cache_name = preg_replace('/[^a-zA-Z0-9\.]/', '', $cache_name);
  193. $layouts = $this->cache->get($cache_name,'', (int)$this->config->get('config_store_id'));
  194. if (isset($layouts)) {
  195. // return cached layouts
  196. return $layouts;
  197. }
  198. $where = 'WHERE template_id = "' . $this->db->escape($this->tmpl_id) . '" ';
  199. $join = ", " . DB_PREFIX . "pages_layouts as pl ";
  200. $where .= "AND pl.page_id = '" . (int)$this->page_id . "' AND l.layout_id = pl.layout_id ";
  201. if ( !empty($layout_type) ) {
  202. $where .= empty($layout_type) ? "" : "AND layout_type = '" . (int)$layout_type . "' ";
  203. }
  204. $sql = "SELECT "
  205. . "l.layout_id as layout_id, "
  206. . "l.layout_type as layout_type, "
  207. . "l.layout_name as layout_name, "
  208. . "l.created as created, "
  209. . "l.updated as updated "
  210. . "FROM "
  211. . DB_PREFIX . "layouts as l "
  212. . $join
  213. . $where
  214. . " ORDER BY "
  215. . "l.layout_id Asc";
  216. $query = $this->db->query($sql);
  217. $layouts = $query->rows;
  218. $this->cache->set($cache_name, $layouts,'', (int)$this->config->get('config_store_id'));
  219. return $layouts;
  220. }
  221. public function getlayoutBlocks($layout_id) {
  222. if ( empty($layout_id) ){
  223. throw new AException(AC_ERR_LOAD_LAYOUT, 'No layout specified for getlayoutBlocks!');
  224. }
  225. $cache_name = 'layout.blocks.' . $layout_id;
  226. $cache_name = preg_replace('/[^a-zA-Z0-9\.]/', '', $cache_name);
  227. $blocks = $this->cache->get($cache_name, '', (int)$this->config->get('config_store_id'));
  228. if (isset($blocks)) {
  229. // return cached blocks
  230. return $blocks;
  231. }
  232. $where = "WHERE bl.layout_id = '" . $layout_id . "' ";
  233. $where .= "AND bl.block_id = b.block_id AND bl.status = 1 ";
  234. $sql = "SELECT "
  235. . "bl.instance_id as instance_id, "
  236. . "b.block_id as block_id, "
  237. . "bl.custom_block_id, "
  238. . "bl.parent_instance_id as parent_instance_id, "
  239. . "bl.position as position, "
  240. . "b.block_txt_id as block_txt_id, "
  241. . "b.controller as controller "
  242. . "FROM "
  243. . DB_PREFIX . "blocks as b, "
  244. . DB_PREFIX . "block_layouts as bl "
  245. . $where
  246. . "ORDER BY "
  247. . "bl.parent_instance_id Asc, bl.position Asc";
  248. $query = $this->db->query($sql);
  249. $blocks = $query->rows;
  250. $this->cache->set($cache_name, $blocks,'', (int)$this->config->get('config_store_id'));
  251. return $blocks;
  252. }
  253. public function getChildren($instance_id = 0) {
  254. $children = array();
  255. // Look into all blocks and locate all children
  256. foreach ($this->blocks as $block) {
  257. if ( (string)$block['parent_instance_id'] == (string)$instance_id ){
  258. array_push( $children, $block );
  259. }
  260. }
  261. return $children;
  262. }
  263. public function getBlockDetails( $child_instance_id ) {
  264. //Select block details by controller
  265. foreach ($this->blocks as $block) {
  266. if ($block['instance_id'] == $child_instance_id ){
  267. return $block;
  268. }
  269. }
  270. }
  271. public function addChildFirst($instance_id, $newchild, $block_txt_id, $template) {
  272. $new_block = array();
  273. $new_block['parent_instance_id'] = $instance_id;
  274. $new_block['instance_id'] = $block_txt_id.$instance_id;
  275. $new_block['block_id'] = $block_txt_id;
  276. $new_block['controller'] = $newchild;
  277. $new_block['block_txt_id'] = $block_txt_id;
  278. $new_block['template'] = $template;
  279. array_unshift($this->blocks, $new_block );
  280. }
  281. public function addChild($instance_id, $newchild, $block_txt_id, $template) {
  282. $new_block = array();
  283. $new_block['parent_instance_id'] = $instance_id;
  284. $new_block['instance_id'] = $block_txt_id.$instance_id;
  285. $new_block['block_id'] = $block_txt_id;
  286. $new_block['controller'] = $newchild;
  287. $new_block['block_txt_id'] = $block_txt_id;
  288. $new_block['template'] = $template;
  289. array_push($this->blocks, $new_block );
  290. }
  291. public function getBlockTemplate ( $instance_id ) {
  292. //Select block and parent id by controller
  293. $block_id = '';
  294. $parent_block_id = '';
  295. $parent_instance_id = '';
  296. $template = '';
  297. //locate block id
  298. foreach ($this->blocks as $block) {
  299. if ($block['instance_id'] == $instance_id ){
  300. $block_id = $block['block_id'];
  301. $parent_instance_id = $block['parent_instance_id'];
  302. $template = !empty($block['template']) ? $block['template'] : '';
  303. break;
  304. }
  305. }
  306. $where = '';
  307. //Check if we do not have template set yet in the code
  308. if ($template) {
  309. return $template;
  310. }
  311. //locate true parent_block id. Not to confuse with parent_instance_id
  312. foreach ($this->blocks as $block) {
  313. if ($block['instance_id'] == $parent_instance_id ){
  314. $parent_block_id = $block['block_id'];
  315. break;
  316. }
  317. }
  318. if ( !empty($block_id) && !empty($parent_block_id) ) {
  319. $cache_name = 'layout.block.template.' . $block_id . '.' . $parent_block_id;
  320. $cache_name = preg_replace('/[^a-zA-Z0-9\.]/', '', $cache_name);
  321. $template = $this->cache->get($cache_name,'', (int)$this->config->get('config_store_id'));
  322. if (isset($template)) {
  323. // return cached $template
  324. return $template;
  325. }
  326. $where = 'WHERE bt.block_id = "' . (int)($block_id) . '" ';
  327. $where .= 'AND bt.parent_block_id = "' . (int)$parent_block_id . '" ';
  328. $sql = "SELECT "
  329. . "bt.template as template, "
  330. . "bt.created as created, "
  331. . "bt.updated as updated "
  332. . "FROM "
  333. . DB_PREFIX . "block_templates as bt "
  334. . $where
  335. . "ORDER BY "
  336. . "bt.block_id Asc";
  337. $query = $this->db->query($sql);
  338. if ( $query->num_rows ) {
  339. $this->cache->set($cache_name, $query->rows[0]['template'],'', (int)$this->config->get('config_store_id'));
  340. return $query->rows[0]['template'];
  341. } else
  342. return '';
  343. }
  344. }
  345. public function getBlockDescriptions($custom_block_id=0){
  346. if(!(int)$custom_block_id){
  347. return array();
  348. }
  349. $cache_name = 'layout.a.block.descriptions.' . $custom_block_id;
  350. $output = $this->cache->get ( $cache_name );
  351. if (isset( $output )) {
  352. // return cached blocks
  353. return $output;
  354. }
  355. $output = array();
  356. $result = $this->db->query ( "SELECT bd.*, COALESCE(bl.status,0) as status
  357. FROM " . DB_PREFIX . "block_descriptions bd
  358. LEFT JOIN " . DB_PREFIX . "block_layouts bl ON bl.custom_block_id = bd.custom_block_id
  359. WHERE bd.custom_block_id = '" . ( int ) $custom_block_id . "'");
  360. if($result->num_rows){
  361. foreach($result->rows as $row){
  362. $output[$row['language_id']] = $row;
  363. }
  364. }
  365. $this->cache->set ( $cache_name, $output );
  366. return $output;
  367. }
  368. }