PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/Nodes.php

https://gitlab.com/diva-lab/essam-labs
PHP | 314 lines | 289 code | 14 blank | 11 comment | 41 complexity | 265bf21d84e34fe6c79148924c53e53a MD5 | raw file
  1. <?php
  2. require_once 'CRUD.php';
  3. class Nodes extends CRUD{
  4. //calss attributes
  5. public $id;
  6. public $node_type;
  7. public $status;
  8. public $inserted_by;
  9. public $inserted_date;
  10. public $enable_comments;
  11. public $enable_summary;
  12. public $front_page;
  13. public $last_update;
  14. public $update_by;
  15. public $slide_show;
  16. public $side_bar;
  17. public $start_publishing;
  18. public $end_publishing;
  19. public $hit_counter;
  20. public $cover_image;
  21. public $slider_cover;
  22. public $model;
  23. public $shortcut_link;
  24. //relation table attribute
  25. public $content_id;
  26. public $title;
  27. public $alias;
  28. public $summary;
  29. public $lang_name;
  30. public $body;
  31. public $lang_id;
  32. public $last_name;
  33. public $first_name;
  34. public $category_title;
  35. public $category_alias;
  36. public $meta_keys;
  37. public $meta_description;
  38. public $layout_name;
  39. public $model_name;
  40. public $theme_name;
  41. //push attributes for relational tables
  42. public function enable_relation(){
  43. array_push(static::$primary_fields ,'lang_name','$category_title','category_alias','content_id','title','alias','summary','body','lang_id',
  44. 'first_name','last_name','meta_keys','meta_description','model_name','layout_name','theme_name');
  45. }
  46. //define table name and fields
  47. protected static $table_name = 'nodes';
  48. protected static $primary_fields = array('id','status','enable_summary','inserted_by','inserted_date','last_update','front_page','slide_show','side_bar','update_by','enable_comments','start_publishing','end_publishing','url_redirect','cover_image','slider_cover','node_type','model','shortcut_link');
  49. //get by nodes data
  50. public function node_data($type,$id = null,$status=null,$cat_id=null,$date_from=null,$date_to=null,$lang=null,$inserted_by=null,$sort_filed=null,$order_by=null, $limit=null,$offset=null){
  51. $sql = "SELECT DISTINCT nodes.id AS id, nodes_content.id AS content_id, nodes.status AS status,nodes_content.title as title,
  52. nodes.enable_summary AS enable_summary,nodes.node_type AS node_type,
  53. nodes.inserted_date AS inserted_date,nodes.last_update AS last_update, users.user_name AS inserted_by, user2.user_name AS update_by,
  54. nodes.cover_image AS cover_image ,nodes.slider_cover AS slider_cover, nodes.enable_comments AS enable_comments,
  55. nodes_content.lang_id AS lang_id,nodes.start_publishing AS start_publishing, nodes.end_publishing AS end_publishing, nodes.slide_show AS slide_show,
  56. nodes.front_page AS front_page, localization.name AS lang_name,localization.id AS lang_id,nodes_content.meta_keys AS meta_keys,
  57. nodes_content.meta_description AS meta_description,nodes.shortcut_link AS shortcut_link ,nodes.side_bar AS side_bar
  58. FROM nodes
  59. LEFT JOIN users ON nodes.inserted_by = users.id
  60. LEFT JOIN nodes_content ON nodes.id = nodes_content.node_id
  61. LEFT JOIN users AS user2 ON nodes.update_by = user2.id
  62. LEFT JOIN nodes_selected_taxonomies ON nodes.id = nodes_selected_taxonomies.node_id
  63. LEFT JOIN localization ON nodes_content.lang_id = localization.id
  64. WHERE 1 ";
  65. if(!empty($lang)){
  66. $sql .= " AND nodes_content.lang_id = '$lang' ";
  67. }
  68. if(!empty($id)){
  69. $sql .= " AND nodes.id = $id ";
  70. $result_array = static::find_by_sql($sql);
  71. return !empty($result_array)? array_shift($result_array) : false;
  72. }else{
  73. $sql .=" AND nodes.node_type = '$type' ";
  74. if(!empty($cat_id)){
  75. $categories = implode(",",$cat_id);
  76. $sql .= " AND nodes_selected_taxonomies.taxonomy_id in ($categories)
  77. AND nodes_selected_taxonomies.node_type = '$type'
  78. AND nodes_selected_taxonomies.taxonomy_type = 'category'";
  79. }
  80. if(!empty($date_from)){
  81. $sql .=" AND CAST(nodes.inserted_date as DATE) BETWEEN '$date_from' AND '$date_to' ";
  82. }
  83. if(!empty($status)){
  84. $sql .= " AND nodes.status = '$status' ";
  85. }
  86. if(!empty($inserted_by)){
  87. $inserted_by_string = implode(",",$inserted_by);
  88. $sql .= " AND nodes.inserted_by in ($inserted_by_string) ";
  89. }
  90. if(!empty($sort_filed) && !empty($order_by)){
  91. $sql .= " ORDER BY ".$sort_filed." ".$order_by;
  92. }
  93. if(!empty($limit)){
  94. $sql .= " LIMIT {$limit}";
  95. if(!empty($offset)){
  96. $sql .= " OFFSET {$offset}";
  97. }
  98. }
  99. return self::find_by_sql($sql);
  100. }
  101. }
  102. //for front nodes
  103. public static function front_node_data($id=null, $type = null,$alias = null,$alias_exclusion = null,$lang = null,$front_page = null,$slide_show = null,$side_bar = null,$cat_id = null,$tag_id = null,$date = null, $search = null, $retrieve = null,$limit = null,$page_offset = null){
  104. global $database;
  105. $lang = $database->escape_values($lang);
  106. $alias = $database->escape_values($alias);
  107. $alias_exclusion = $database->escape_values($alias_exclusion);
  108. $sql = "SELECT DISTINCT nodes.id AS id, nodes_content.title AS title, nodes.status AS status, nodes_content.summary AS summary,
  109. nodes.enable_summary AS enable_summary, nodes_content.body AS body, nodes_content.alias AS alias,nodes.enable_comments AS enable_comments,
  110. nodes.start_publishing AS start_publishing, nodes.cover_image AS cover_image ,nodes.node_type as node_type, nodes.side_bar AS side_bar,
  111. nodes.slider_cover AS slider_cover,users.first_name AS first_name ,users.last_name AS last_name, nodes_content.meta_description AS meta_description
  112. FROM nodes
  113. LEFT JOIN nodes_content ON nodes.id = nodes_content.node_id
  114. LEFT JOIN nodes_selected_taxonomies ON nodes.id = nodes_selected_taxonomies.node_id
  115. LEFT JOIN users ON nodes.inserted_by = users.id
  116. WHERE nodes.status = 'publish'
  117. AND nodes_content.lang_id = '$lang'
  118. ";
  119. if(!empty($type)){
  120. $sql .=" AND nodes.node_type = '$type'";
  121. }
  122. if(!empty($id)){
  123. $sql .=" AND nodes.id = '$id'";
  124. }
  125. if(!empty($alias)){
  126. $sql .=" AND nodes_content.alias = '$alias' ";
  127. }
  128. if(!empty($search)){
  129. $sql .=" AND nodes_content.body like '%$search%'
  130. OR nodes_content.title like '%$search%' ";
  131. }
  132. if(!empty($alias_exclusion)){
  133. $sql .=" AND nodes_content.alias != '$alias_exclusion' ";
  134. }
  135. if(!empty($date)){
  136. $sql .=" AND nodes.start_publishing <= '$date' AND (nodes.end_publishing >= '$date' OR nodes.end_publishing = '0000-00-00 00:00:00') ";
  137. }
  138. if(!empty($front_page)){
  139. $sql .=" AND nodes.front_page = '$front_page' ";
  140. }
  141. if(!empty($side_bar)){
  142. $sql .=" AND nodes.side_bar = '$side_bar' ";
  143. }
  144. if(!empty($slide_show)){
  145. $sql .=" AND nodes.slide_show = '$slide_show' ";
  146. }
  147. if(!empty($cat_id)){
  148. $categories = $database->escape_values($cat_id);
  149. $sql .= " AND nodes_selected_taxonomies.taxonomy_id in ($categories)
  150. AND nodes_selected_taxonomies.taxonomy_type = 'category'
  151. AND nodes_selected_taxonomies.node_type = '$type' ";
  152. }
  153. if(!empty($tag_id)){
  154. $sql .= " AND nodes_selected_taxonomies.taxonomy_id in ($tag_id)
  155. AND nodes_selected_taxonomies.taxonomy_type = 'tag'
  156. AND nodes_selected_taxonomies.node_type = '$type' ";
  157. }
  158. $sql .= " ORDER BY nodes.start_publishing DESC";
  159. if(!empty($limit)){
  160. $sql .= " LIMIT ".$limit;
  161. }
  162. if(!empty($page_offset)){
  163. $sql .= " OFFSET $page_offset";
  164. }
  165. if($retrieve == 'one'){
  166. $result_array = static::find_by_sql($sql);
  167. return !empty($result_array)? array_shift($result_array) : false;
  168. }else{
  169. return self::find_by_sql($sql);
  170. }
  171. }
  172. //get node content
  173. public static function get_node_content($node_id,$lang = null){
  174. $sql = "SELECT nodes_content.id AS id, nodes_content.lang_id AS lang_id, nodes_content.title AS title, nodes_content.alias AS alias,
  175. nodes_content.summary AS summary, nodes_content.body AS body, nodes_content.meta_keys AS meta_keys, nodes_content.meta_description AS meta_description
  176. FROM nodes_content
  177. WHERE node_id = '$node_id'";
  178. if(!empty($lang)){
  179. $sql .= "AND lang_id = '$lang'" ;
  180. $result_array = static::find_by_sql($sql);
  181. return !empty($result_array)? array_shift($result_array) : false;
  182. }else{
  183. return static::find_by_sql($sql);
  184. }
  185. }
  186. //return rest languages
  187. public function return_other_languages($alias, $lang){
  188. global $database;
  189. $alias = $database->escape_values($alias);
  190. $lang = $database->escape_values($lang);
  191. $sql = "SELECT nodes_content.alias AS alias
  192. FROM nodes, nodes_content, localization
  193. WHERE nodes.id = nodes_content.node_id AND nodes_content.node_id
  194. IN (SELECT node_id FROM nodes_content WHERE alias = '{$alias}')
  195. AND nodes_content.lang_id = localization.id
  196. AND localization.label = '{$lang}'";
  197. $result_array = static::find_by_sql($sql);
  198. return !empty($result_array)? array_shift($result_array) : false;
  199. }
  200. //get node model,theme and layout
  201. public static function get_model($node_id = null) {
  202. $sql = "SELECT nodes.model AS model, theme_layout_model.name AS model_name, themes.name AS theme_name
  203. FROM nodes, theme_layout_model, themes_layouts, themes
  204. WHERE nodes.model = theme_layout_model.id AND theme_layout_model.layout_id = themes_layouts.id
  205. AND nodes.id = $node_id
  206. AND themes_layouts.theme_id = themes.id";
  207. $result_array = static::find_by_sql($sql);
  208. return !empty($result_array)? array_shift($result_array) : false;
  209. }
  210. //get front node model,theme and layout
  211. public static function front_get_model($node_id = null) {
  212. $sql = "SELECT themes_layouts.layout_name AS layout_name, nodes.model AS model
  213. FROM nodes,theme_layout_model,themes_layouts
  214. WHERE nodes.model = theme_layout_model.id
  215. AND theme_layout_model.layout_id = themes_layouts.id
  216. AND nodes.id = '$node_id' ";
  217. $result_array = static::find_by_sql($sql);
  218. return !empty($result_array)? array_shift($result_array) : false;
  219. }
  220. //for front products
  221. public static function front_product_data($alias = null,$alias_exclusion = null,$lang = null,$date = null,$front_page = null,$slide_show = null,$cat_id = null,$tag_id = null,$retrieve = null,$limit = null,$page_offset = null,$store_id = null,$search = null){
  222. global $database;
  223. $lang = $database->escape_values($lang);
  224. $alias = $database->escape_values($alias);
  225. $alias_exclusion = $database->escape_values($alias_exclusion);
  226. $sql = "SELECT DISTINCT nodes.id AS id, nodes_content.title AS title, nodes.status AS status, nodes_content.summary AS summary,
  227. nodes.enable_summary AS enable_summary, nodes_content.body AS body, nodes_content.alias AS alias,nodes.enable_comments AS enable_comments,
  228. nodes.start_publishing AS start_publishing, nodes.cover_image AS cover_image ,
  229. nodes.slider_cover AS slider_cover,users.first_name AS first_name ,users.last_name AS last_name
  230. FROM nodes
  231. LEFT JOIN nodes_content ON nodes.id = nodes_content.node_id
  232. LEFT JOIN nodes_selected_taxonomies ON nodes.id = nodes_selected_taxonomies.node_id
  233. LEFT JOIN ecom_products_selected_stores ON nodes.id = ecom_products_selected_stores.product_id
  234. LEFT JOIN users ON nodes.inserted_by = users.id
  235. WHERE nodes.status = 'publish'
  236. AND nodes_content.lang_id = '$lang'
  237. AND nodes.node_type = 'product' ";
  238. if(!empty($alias)){
  239. $sql .=" AND nodes_content.alias = '$alias' ";
  240. }
  241. if(!empty($search)){
  242. $sql .=" AND nodes_content.body like '%$search%'
  243. OR nodes_content.title like '%$search%' ";
  244. }
  245. if(!empty($alias_exclusion)){
  246. $sql .=" AND nodes_content.alias != '$alias_exclusion' ";
  247. }
  248. if(!empty($date)){
  249. $sql .=" AND nodes.start_publishing <= '$date' AND (nodes.end_publishing >= '$date' OR nodes.end_publishing = '0000-00-00 00:00:00') ";
  250. }
  251. if(!empty($front_page)){
  252. $sql .=" AND nodes.front_page = '$front_page' ";
  253. }
  254. if(!empty($slide_show)){
  255. $sql .=" AND nodes.slide_show = '$slide_show' ";
  256. }
  257. if(!empty($cat_id)){
  258. $categories = $database->escape_values($cat_id);
  259. $sql .= " AND nodes_selected_taxonomies.taxonomy_id in ($categories)
  260. AND nodes_selected_taxonomies.taxonomy_type = 'category'
  261. AND nodes_selected_taxonomies.node_type = 'product' ";
  262. }
  263. if(!empty($store_id)){
  264. $sql .= " AND ecom_products_selected_stores.branch_id = '$store_id'";
  265. }
  266. if(!empty($tag_id)){
  267. $sql .=" AND nodes_selected_taxonomies.taxonomy_id in ($tag_id)
  268. AND nodes_selected_taxonomies.taxonomy_type = 'tag'
  269. AND nodes_selected_taxonomies.node_type = 'product' ";
  270. }
  271. $sql .= " ORDER BY nodes.start_publishing DESC";
  272. if(!empty($limit)){
  273. $sql .= " LIMIT ".$limit;
  274. }
  275. if(!empty($page_offset)){
  276. $sql .= " OFFSET $page_offset";
  277. }
  278. if($retrieve == 'one'){
  279. $result_array = static::find_by_sql($sql);
  280. return !empty($result_array)? array_shift($result_array) : false;
  281. }else{
  282. return self::find_by_sql($sql);
  283. }
  284. }
  285. public static function find_cover_image($node_id){
  286. $sql = "SELECT nodes.cover_image AS cover_image
  287. FROM nodes
  288. WHERE id = '$node_id'";
  289. $result_array = static::find_by_sql($sql);
  290. return !empty($result_array)? array_shift($result_array) : false;
  291. }
  292. public static function get_image_for_node($node_id = null ){
  293. $sql = " SELECT nodes.cover_image FROM nodes left join nodes_content
  294. on nodes.id = nodes_content.node_id
  295. WHERE node_id = '$node_id' " ;
  296. $result_array = static::find_by_sql($sql);
  297. return !empty($result_array)? array_shift($result_array) : false;
  298. }
  299. }
  300. ?>