/system/cms/modules/widgets/models/widget_m.php

https://github.com/JamieLomas/pyrocms · PHP · 332 lines · 260 code · 61 blank · 11 comment · 14 complexity · 594ce111f9bc29af1902361e2f82b060 MD5 · raw file

  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * @package PyroCMS
  4. * @subpackage Widgets module
  5. * @author Phil Sturgeon - PyroCMS Development Team
  6. *
  7. * Model to handle widgets
  8. */
  9. class Widget_m extends MY_Model
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->load->helper('date');
  15. }
  16. public function get_instance($id)
  17. {
  18. $this->db
  19. ->select('w.id, w.slug, wi.id as instance_id, wi.title as instance_title, w.title, wi.widget_area_id, wa.slug as widget_area_slug, wi.options')
  20. ->from('widget_areas wa')
  21. ->join('widget_instances wi', 'wa.id = wi.widget_area_id')
  22. ->join('widgets w', 'wi.widget_id = w.id')
  23. ->where('wi.id', $id);
  24. $result = $this->db->get()->row();
  25. if ($result)
  26. {
  27. $this->unserialize_fields($result);
  28. }
  29. return $result;
  30. }
  31. public function get_by_area($slug)
  32. {
  33. $this->db
  34. ->select('wi.id, w.slug, wi.id as instance_id, wi.title as instance_title, w.title, wi.widget_area_id, wa.slug as widget_area_slug, wi.options')
  35. ->from('widget_areas wa')
  36. ->join('widget_instances wi', 'wa.id = wi.widget_area_id')
  37. ->join('widgets w', 'wi.widget_id = w.id')
  38. ->where('wa.slug', $slug)
  39. ->order_by('wi.order');
  40. $result = $this->db->get()->result();
  41. if ($result)
  42. {
  43. array_map(array($this, 'unserialize_fields'), $result);
  44. }
  45. return $result;
  46. }
  47. public function get_by_areas($slug = array())
  48. {
  49. if ( ! (is_array($slug) && $slug))
  50. {
  51. return array();
  52. }
  53. $this->db
  54. ->select('wi.id, w.slug, wi.id as instance_id, wi.title as instance_title, w.title, wi.widget_area_id, wa.slug as widget_area_slug, wi.options')
  55. ->from('widget_areas wa')
  56. ->join('widget_instances wi', 'wa.id = wi.widget_area_id')
  57. ->join('widgets w', 'wi.widget_id = w.id')
  58. ->where_in('wa.slug', $slug)
  59. ->order_by('wi.order');
  60. $result = $this->db->get()->result();
  61. if ($result)
  62. {
  63. array_map(array($this, 'unserialize_fields'), $result);
  64. }
  65. return $result;
  66. }
  67. public function get_areas()
  68. {
  69. return $this->db->get('widget_areas')->result();
  70. }
  71. public function get_area_by($field, $id)
  72. {
  73. return $this->db->get_where('widget_areas', array($field => $id))->row();
  74. }
  75. public function get_widget_by($field, $id)
  76. {
  77. $result = $this->db->get_where('widgets', array($field => $id))->row();
  78. if ($result)
  79. {
  80. $this->unserialize_fields($result);
  81. }
  82. return $result;
  83. }
  84. public function unserialize_fields($obj)
  85. {
  86. foreach (array('title', 'description') as $field)
  87. {
  88. if (isset($obj->{$field}))
  89. {
  90. $_field = @unserialize($obj->{$field});
  91. if ($_field === FALSE)
  92. {
  93. isset($obj->slug) && $this->widgets->reload_widget($obj->slug);
  94. }
  95. else
  96. {
  97. $obj->{$field} = is_array($_field)
  98. ? isset($_field[CURRENT_LANGUAGE])
  99. ? $_field[CURRENT_LANGUAGE] : $_field['en']
  100. : $_field;
  101. }
  102. }
  103. }
  104. return $obj;
  105. }
  106. public function get_all()
  107. {
  108. $result = parent::get_all();
  109. if ($result)
  110. {
  111. array_map(array($this, 'unserialize_fields'), $result);
  112. }
  113. return $result;
  114. }
  115. public function insert_widget($input = array())
  116. {
  117. // Merge defaults
  118. $input = array_merge(array(
  119. 'enabled' => 1
  120. ), (array) $input);
  121. $last_widget = $this->db
  122. ->select('`order`')
  123. ->order_by('`order`', 'desc')
  124. ->limit(1)
  125. ->get_where('widgets', array('enabled' => $input['enabled']))
  126. ->row();
  127. $input['order'] = isset($last_widget->order) ? $last_widget->order + 1 : 1;
  128. return $this->db->insert('widgets', array(
  129. 'title' => serialize($input['title']),
  130. 'slug' => $input['slug'],
  131. 'description' => serialize($input['description']),
  132. 'author' => $input['author'],
  133. 'website' => $input['website'],
  134. 'version' => $input['version'],
  135. 'enabled' => $input['enabled'],
  136. 'order' => $input['order'],
  137. 'updated_on' => now()
  138. ));
  139. }
  140. public function update_widget($input)
  141. {
  142. if ( ! isset($input['slug']))
  143. {
  144. return FALSE;
  145. }
  146. return $this->db
  147. ->where('slug', $input['slug'])
  148. ->update('widgets', array(
  149. 'title' => serialize($input['title']),
  150. 'slug' => $input['slug'],
  151. 'description' => serialize($input['description']),
  152. 'author' => $input['author'],
  153. 'website' => $input['website'],
  154. 'version' => $input['version'],
  155. 'updated_on' => now()
  156. ));
  157. }
  158. public function update_widget_order($id, $order)
  159. {
  160. $this->db->where('id', $id);
  161. return $this->db->update('widgets', array(
  162. 'order' => (int) $order
  163. ));
  164. }
  165. public function enable_widget($id = 0)
  166. {
  167. $this->db->where('id', $id);
  168. return $this->db->update('widgets', array(
  169. 'enabled' => 1
  170. ));
  171. }
  172. public function disable_widget($id = 0)
  173. {
  174. $this->db->where('id', $id);
  175. return $this->db->update('widgets', array(
  176. 'enabled' => 0
  177. ));
  178. }
  179. public function insert_area($input)
  180. {
  181. return $this->db->insert('widget_areas', array(
  182. 'title' => $input['title'],
  183. 'slug' => $input['slug']
  184. ));
  185. }
  186. public function update_area($input = array())
  187. {
  188. if (isset($input['id']))
  189. {
  190. $this->db->where('id', $input['id']);
  191. }
  192. else
  193. {
  194. $this->db->where('slug', $input['area_slug']);
  195. }
  196. $this->db->update('widget_areas', array(
  197. 'title' => $input['title'],
  198. 'slug' => $input['slug']
  199. ));
  200. $result = $this->db->affected_rows();
  201. return ($result > 0) ? TRUE : FALSE;
  202. }
  203. public function insert_instance($input)
  204. {
  205. $this->load->helper('date');
  206. $last_widget = $this->db
  207. ->select('`order`')
  208. ->order_by('`order`', 'desc')
  209. ->limit(1)
  210. ->get_where('widget_instances', array('widget_area_id' => $input['widget_area_id']))
  211. ->row();
  212. $order = isset($last_widget->order) ? $last_widget->order + 1 : 1;
  213. return $this->db->insert('widget_instances', array(
  214. 'title' => $input['title'],
  215. 'widget_id' => $input['widget_id'],
  216. 'widget_area_id' => $input['widget_area_id'],
  217. 'options' => $input['options'],
  218. 'order' => $order,
  219. 'created_on' => now(),
  220. 'updated_on' => now()
  221. ));
  222. }
  223. public function update_instance($instance_id, $input)
  224. {
  225. $this->db->where('id', $instance_id);
  226. return $this->db->update('widget_instances', array(
  227. 'title' => $input['title'],
  228. 'widget_area_id' => $input['widget_area_id'],
  229. 'options' => $input['options']
  230. ));
  231. }
  232. public function update_instance_order($id, $order)
  233. {
  234. $this->db->where('id', $id);
  235. return $this->db->update('widget_instances', array(
  236. 'order' => (int) $order
  237. ));
  238. }
  239. public function delete_widget($slug)
  240. {
  241. $widget = $this->db
  242. ->select('id')
  243. ->get_where('widgets', array('slug' => $slug))
  244. ->row();
  245. if (isset($widget->id))
  246. {
  247. $this->db->delete('widget_instances', array('widget_id' => $widget->id));
  248. }
  249. return $this->db->delete('widgets', array('slug' => $slug));
  250. }
  251. public function delete_area($id = 0)
  252. {
  253. if ( ! is_numeric($id))
  254. {
  255. // Get the id for this area
  256. $area = $this->db
  257. ->select('id')
  258. ->get_where('widget_areas', array('slug' => $slug))
  259. ->row();
  260. return $area ? $this->delete_area($area->id) : FALSE;
  261. }
  262. // Delete widgets in that area
  263. $this->db->delete('widget_instances', array('widget_area_id' => $id));
  264. // Delete this area
  265. return $this->db->delete('widget_areas', array('id' => $id));
  266. }
  267. public function delete_instance($id)
  268. {
  269. return $this->db->delete('widget_instances', array('id' => $id));
  270. }
  271. }