/models/portfolio_m.php

https://github.com/jenkoian/pyrocms-portfolio · PHP · 264 lines · 159 code · 43 blank · 62 comment · 37 complexity · 7b223b4e8c269138aec3f34793be206e MD5 · raw file

  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Portfolio_m extends MY_Model {
  4. /**
  5. * @var string
  6. */
  7. protected $_table = 'portfolio';
  8. /**
  9. * @return array
  10. */
  11. public function get_all() {
  12. $this->db->select($this->db->dbprefix('portfolio') . '.*, ' . $this->db->dbprefix('portfolio_clients') . '.title AS client_title, ' . $this->db->dbprefix('portfolio_clients') . '.slug AS client_slug, f.filename, f.extension');
  13. $this->db->join($this->db->dbprefix('portfolio_clients'), $this->db->dbprefix('portfolio') . '.client_id = ' . $this->db->dbprefix('portfolio_clients') . '.id', 'left');
  14. $this->db->join('files f', 'f.id = ' . $this->db->dbprefix('portfolio') . '.thumbnail_id', 'left');
  15. $this->db->order_by('created_on', 'DESC');
  16. return $this->db->get($this->db->dbprefix('portfolio'))->result();
  17. }
  18. /**
  19. * @param int $id
  20. * @return array
  21. */
  22. public function get($id) {
  23. $this->db->where(array('id' => $id));
  24. return $this->db->get($this->db->dbprefix('portfolio'))->row();
  25. }
  26. /**
  27. *
  28. * @param array $params
  29. * @return array
  30. */
  31. public function get_many_by($params = array()) {
  32. $this->load->helper('date');
  33. if (!empty($params['client'])) {
  34. if (is_numeric($params['client']))
  35. $this->db->where($this->db->dbprefix('portfolio_clients') . '.id', $params['client']);
  36. else
  37. $this->db->where($this->db->dbprefix('portfolio_clients') . '.slug', $params['client']);
  38. }
  39. if (!empty($params['portfolioIds']) && is_array($params['portfolioIds'])) {
  40. $this->db->where_in($this->db->dbprefix('portfolio') . '.id', $params['portfolioIds']);
  41. }
  42. if (!empty($params['month'])) {
  43. $this->db->where('MONTH(FROM_UNIXTIME(created_on))', $params['month']);
  44. }
  45. if (!empty($params['year'])) {
  46. $this->db->where('YEAR(FROM_UNIXTIME(created_on))', $params['year']);
  47. }
  48. // Is a status set?
  49. if (!empty($params['status'])) {
  50. // If it's all, then show whatever the status
  51. if ($params['status'] != 'all') {
  52. // Otherwise, show only the specific status
  53. $this->db->where('status', $params['status']);
  54. }
  55. }
  56. // Nothing mentioned, show live only (general frontend stuff)
  57. else {
  58. $this->db->where('status', 'live');
  59. }
  60. // By default, dont show future items
  61. if (!isset($params['show_future']) || (isset($params['show_future']) && $params['show_future'] == FALSE)) {
  62. $this->db->where('created_on <=', now());
  63. }
  64. // Limit the results based on 1 number or 2 (2nd is offset)
  65. if (isset($params['limit']) && is_array($params['limit']))
  66. $this->db->limit($params['limit'][0], $params['limit'][1]);
  67. elseif (isset($params['limit']))
  68. $this->db->limit($params['limit']);
  69. return $this->get_all();
  70. }
  71. /**
  72. *
  73. * @param array $params
  74. * @return int
  75. */
  76. public function count_by($params = array()) {
  77. $this->db->join($this->db->dbprefix('portfolio_clients'), $this->db->dbprefix('portfolio') . '.client_id = ' . $this->db->dbprefix('portfolio_clients') . '.id', 'left');
  78. if (!empty($params['client'])) {
  79. if (is_numeric($params['client']))
  80. $this->db->where($this->db->dbprefix('portfolio_clients') . '.id', $params['client']);
  81. else
  82. $this->db->where($this->db->dbprefix('portfolio_clients') . '.slug', $params['client']);
  83. }
  84. if (!empty($params['month'])) {
  85. $this->db->where('MONTH(FROM_UNIXTIME(created_on))', $params['month']);
  86. }
  87. if (!empty($params['year'])) {
  88. $this->db->where('YEAR(FROM_UNIXTIME(created_on))', $params['year']);
  89. }
  90. // Is a status set?
  91. if (!empty($params['status'])) {
  92. // If it's all, then show whatever the status
  93. if ($params['status'] != 'all') {
  94. // Otherwise, show only the specific status
  95. $this->db->where('status', $params['status']);
  96. }
  97. }
  98. // Nothing mentioned, show live only (general frontend stuff)
  99. else {
  100. $this->db->where('status', 'live');
  101. }
  102. return $this->db->count_all_results($this->db->dbprefix('portfolio'));
  103. }
  104. /**
  105. *
  106. * @param int $id
  107. * @param array $input
  108. * @return int
  109. */
  110. public function update($id, $input) {
  111. $input['updated_on'] = now();
  112. return parent::update($id, $input);
  113. }
  114. /**
  115. *
  116. * @param int $id
  117. * @return int
  118. */
  119. public function publish($id = 0) {
  120. return parent::update($id, array('status' => 'live'));
  121. }
  122. // -- Archive ---------------------------------------------
  123. /**
  124. * @return array
  125. */
  126. public function get_archive_months() {
  127. $this->db->select('UNIX_TIMESTAMP(DATE_FORMAT(FROM_UNIXTIME(t1.created_on), "%Y-%m-02")) AS `date`', FALSE);
  128. $this->db->distinct();
  129. $this->db->select('(SELECT count(id) FROM ' . $this->db->dbprefix('portfolio') . ' t2
  130. WHERE MONTH(FROM_UNIXTIME(t1.created_on)) = MONTH(FROM_UNIXTIME(t2.created_on))
  131. AND YEAR(FROM_UNIXTIME(t1.created_on)) = YEAR(FROM_UNIXTIME(t2.created_on))
  132. AND status = "live"
  133. AND created_on <= ' . now() . '
  134. ) as item_count');
  135. $this->db->where('status', 'live');
  136. $this->db->where('created_on <=', now());
  137. $this->db->having('item_count >', 0);
  138. $this->db->order_by('t1.created_on DESC');
  139. $query = $this->db->get($this->db->dbprefix('portfolio') . ' t1');
  140. return $query->result();
  141. }
  142. /**
  143. * @todo DIRTY frontend functions. Move to views
  144. * @param array $params
  145. * @return string
  146. */
  147. public function get_portfolio_fragment($params = array()) {
  148. $this->load->helper('date');
  149. $this->db->where('status', 'live');
  150. $this->db->where('created_on <=', now());
  151. $string = '';
  152. $this->db->order_by('created_on', 'DESC');
  153. $this->db->limit(5);
  154. $query = $this->db->get($this->db->dbprefix('portfolio'));
  155. if ($query->num_rows() > 0) {
  156. $this->load->helper('text');
  157. foreach ($query->result() as $portfolio) {
  158. $string .= '<p>' . anchor('portfolio/' . date('Y/m') . '/' . $portfolio->slug, $portfolio->title) . '<br />' . strip_tags($portfolio->intro) . '</p>';
  159. }
  160. }
  161. return $string;
  162. }
  163. /**
  164. *
  165. * @param string $field
  166. * @param string $value
  167. * @param int $id
  168. * @return boolean
  169. */
  170. public function check_exists($field, $value = '', $id = 0) {
  171. if (is_array($field)) {
  172. $params = $field;
  173. $id = $value;
  174. } else {
  175. $params[$field] = $value;
  176. }
  177. $params['id !='] = (int) $id;
  178. return parent::count_by($params) == 0;
  179. }
  180. /**
  181. * Searches portfolio items based on supplied data array
  182. * @param $data array
  183. * @return array
  184. */
  185. public function search($data = array()) {
  186. if (array_key_exists('client_id', $data)) {
  187. $this->db->where('client_id', $data['client_id']);
  188. }
  189. if (array_key_exists('status', $data)) {
  190. $this->db->where('status', $data['status']);
  191. }
  192. if (array_key_exists('keywords', $data)) {
  193. $matches = array();
  194. if (strstr($data['keywords'], '%')) {
  195. preg_match_all('/%.*?%/i', $data['keywords'], $matches);
  196. }
  197. if (!empty($matches[0])) {
  198. foreach ($matches[0] as $match) {
  199. $phrases[] = str_replace('%', '', $match);
  200. }
  201. } else {
  202. $temp_phrases = explode(' ', $data['keywords']);
  203. foreach ($temp_phrases as $phrase) {
  204. $phrases[] = str_replace('%', '', $phrase);
  205. }
  206. }
  207. $counter = 0;
  208. foreach ($phrases as $phrase) {
  209. if ($counter == 0) {
  210. $this->db->like($this->db->dbprefix('portfolio') . '.title', $phrase);
  211. } else {
  212. $this->db->or_like($this->db->dbprefix('portfolio') . '.title', $phrase);
  213. }
  214. $this->db->or_like($this->db->dbprefix('portfolio') . '.body', $phrase);
  215. $this->db->or_like($this->db->dbprefix('portfolio') . '.intro', $phrase);
  216. $counter++;
  217. }
  218. }
  219. return $this->get_all();
  220. }
  221. }