PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/model/item.php

https://github.com/lysender/simplestore
PHP | 418 lines | 272 code | 52 blank | 94 comment | 17 complexity | dc0a4e7c9f762262e860b26f27efa78f MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Item model
  4. *
  5. * @author lysender
  6. *
  7. */
  8. class Model_Item extends Sprig
  9. {
  10. const ITEMS_PER_PAGE = 50;
  11. /**
  12. * Table name
  13. *
  14. * @var string
  15. */
  16. protected $_table = 'item';
  17. /**
  18. * Count for pagination
  19. *
  20. * @var int
  21. */
  22. protected $_total_count;
  23. /**
  24. * Initialize
  25. *
  26. * (non-PHPdoc)
  27. * @see modules/sprig/classes/sprig/Sprig_Core::_init()
  28. */
  29. protected function _init()
  30. {
  31. $this->_fields = array(
  32. 'id' => new Sprig_Field_Auto,
  33. 'category' => new Sprig_Field_BelongsTo(array(
  34. 'null' => FALSE,
  35. 'empty' => FALSE,
  36. 'column' => 'category_id',
  37. 'model' => 'category',
  38. 'attributes' => array(
  39. 'id' => 'category'
  40. )
  41. )),
  42. 'name' => new Sprig_Field_Char(array(
  43. 'unique' => TRUE,
  44. 'label' => 'Name',
  45. 'min_length' => 3,
  46. 'max_length' => 64,
  47. 'attributes' => array(
  48. 'id' => 'name'
  49. )
  50. )),
  51. 'description' => new Sprig_Field_Text(array(
  52. 'empty' => TRUE,
  53. 'label' => 'Description',
  54. 'min_length' => 3,
  55. 'max_length' => 128,
  56. 'attributes' => array(
  57. 'id' => 'description'
  58. )
  59. )),
  60. 'date_created' => new Sprig_Field_Timestamp,
  61. 'created_by' => new Sprig_Field_Integer(),
  62. 'date_modified' => new Sprig_Field_Timestamp(array(
  63. 'empty' => TRUE,
  64. 'null' => TRUE
  65. )),
  66. 'modified_by' => new Sprig_Field_Integer(array(
  67. 'empty' => TRUE,
  68. 'null' => TRUE
  69. ))
  70. );
  71. }
  72. /**
  73. * Creates a new category
  74. *
  75. * (non-PHPdoc)
  76. * @see modules/sprig/classes/sprig/Sprig_Core::create()
  77. */
  78. public function create()
  79. {
  80. $this->date_created = time();
  81. $user = Dc_Auth::instance()->get_user(
  82. Sprig::factory('user'),
  83. Sprig::factory('usertoken')
  84. );
  85. if ($user)
  86. {
  87. $this->created_by = $user->id;
  88. }
  89. return parent::create();
  90. }
  91. /**
  92. * Updates category
  93. *
  94. * (non-PHPdoc)
  95. * @see modules/sprig/classes/sprig/Sprig_Core::update()
  96. */
  97. public function update()
  98. {
  99. $this->date_modified = time();
  100. $user = Dc_Auth::instance()->get_user(
  101. Sprig::factory('user'),
  102. Sprig::factory('usertoken')
  103. );
  104. if ($user)
  105. {
  106. $this->modified_by = $user->id;
  107. }
  108. return parent::update();
  109. }
  110. /**
  111. * Returns all categories
  112. *
  113. */
  114. public function get_all()
  115. {
  116. $result = DB::select(
  117. 'i.id',
  118. 'i.category_id',
  119. array('c.name', 'category_name'),
  120. 'i.name',
  121. 'i.description'
  122. )
  123. ->from(array($this->_table, 'i'))
  124. ->join(array('category', 'c'))
  125. ->on('i.category_id', '=', 'c.id')
  126. ->order_by('i.name', 'ASC')
  127. ->execute(
  128. )->as_array();
  129. if ( ! empty($result))
  130. {
  131. return $result;
  132. }
  133. return FALSE;
  134. }
  135. /**
  136. * Returns paginated item records
  137. *
  138. * @param int $category_id
  139. * @param int $page
  140. * @param string $sort
  141. * @return array
  142. */
  143. public function get_paged($category_id = NULL, $page = 1, $sort = 'ASC')
  144. {
  145. $page = (int) $page;
  146. // Pre calculate totals
  147. $total_rec = $this->get_total($category_id);
  148. $total_pages = $this->get_total_pages($total_rec);
  149. // Determine the correct page
  150. if ($page < 1)
  151. {
  152. $page = 1;
  153. }
  154. // Make sure the page does not exceed total pages
  155. if ($page > $total_pages)
  156. {
  157. $page = $total_pages;
  158. }
  159. $offset = ($page - 1) * self::ITEMS_PER_PAGE;
  160. // Set limit and offset and execute
  161. $query = DB::select(
  162. 'i.id',
  163. 'i.category_id',
  164. array('c.name', 'category_name'),
  165. 'i.name',
  166. 'i.description'
  167. )
  168. ->from(array($this->_table, 'i'))
  169. ->join(array('category', 'c'))
  170. ->on('i.category_id', '=', 'c.id');
  171. if ($category_id)
  172. {
  173. $query->where('i.category_id', '=', $category_id);
  174. }
  175. $result = $query->order_by('c.name', 'ASC')
  176. ->order_by('i.name', 'ASC')
  177. ->limit(self::ITEMS_PER_PAGE)
  178. ->offset($offset)
  179. ->execute();
  180. if ( ! empty($result))
  181. {
  182. return $result->as_array();
  183. }
  184. return FALSE;
  185. }
  186. /**
  187. * Returns the total number of shows
  188. *
  189. * @param string $cat
  190. * @return int
  191. */
  192. public function get_total($category_id = NULL)
  193. {
  194. if ($this->_total_count === NULL)
  195. {
  196. $query = DB::select('COUNT("*") AS show_total_count')
  197. ->from($this->_table);
  198. if ($category_id)
  199. {
  200. $query->where('category_id', '=', $category_id);
  201. }
  202. $this->_total_count = $query->execute()
  203. ->get('show_total_count');
  204. }
  205. return $this->_total_count;
  206. }
  207. /**
  208. * Returns the total number of pages
  209. * for a given total record count
  210. *
  211. * @param int $total_rec
  212. * @return int
  213. */
  214. public function get_total_pages($total_rec)
  215. {
  216. $ret = ceil($total_rec / self::ITEMS_PER_PAGE);
  217. if ($ret > 0)
  218. {
  219. return (int) $ret;
  220. }
  221. return 1;
  222. }
  223. /**
  224. * Returns items via keyword search
  225. *
  226. * @param string $keyword
  227. * @param int $limit
  228. * @return array
  229. */
  230. public function search($keyword, $limit = 10)
  231. {
  232. $result = DB::select(
  233. 'i.id',
  234. 'i.category_id',
  235. array('c.name', 'category_name'),
  236. 'i.name',
  237. 'i.description'
  238. )
  239. ->from(array($this->_table, 'i'))
  240. ->join(array('category', 'c'))
  241. ->on('i.category_id', '=', 'c.id')
  242. ->where('i.name', 'LIKE', '%'.$keyword.'%')
  243. ->or_where('i.description', 'LIKE', '%'.$keyword.'%')
  244. ->or_where('c.name', 'LIKE', '%'.$keyword.'%')
  245. ->order_by('c.name', 'ASC')
  246. ->order_by('i.name', 'ASC')
  247. ->limit( (int) $limit)
  248. ->execute()
  249. ->as_array();
  250. if ( ! empty($result))
  251. {
  252. return $result;
  253. }
  254. return FALSE;
  255. }
  256. /**
  257. * Searches an item including price
  258. *
  259. * @param string $keyword
  260. * @param int $limit
  261. */
  262. public function search_with_price($keyword, $limit = 10, $relative_date = NULL)
  263. {
  264. if ( ! $relative_date)
  265. {
  266. $relative_date = time();
  267. }
  268. // Inner query to latest price per item
  269. $inner_query = DB::select('id')->from('price')
  270. ->where('item_id', '=', DB::expr('i.id'))
  271. ->where('effective_date', '<=', (int) $relative_date)
  272. ->order_by('effective_date', 'DESC')
  273. ->limit(1);
  274. // Outer query
  275. $query = DB::select(
  276. 'i.id',
  277. 'i.name',
  278. 'i.description',
  279. 'i.category_id',
  280. array('c.name', 'category_name'),
  281. array('p.id', 'price_id'),
  282. 'p.price',
  283. 'p.effective_date'
  284. )
  285. ->from(array($this->_table, 'i'))
  286. ->join(array('category', 'c'))
  287. ->on('i.category_id', '=', 'c.id')
  288. ->join(array('price', 'p'), 'LEFT')
  289. ->on('p.id', '=', $inner_query)
  290. ->where('i.name', 'LIKE', '%'.$keyword.'%')
  291. ->or_where('i.description', 'LIKE', '%'.$keyword.'%')
  292. ->or_where('c.name', 'LIKE', '%'.$keyword.'%')
  293. ->order_by('c.name', 'ASC')
  294. ->order_by('i.name', 'ASC')
  295. ->limit($limit);
  296. // Kohana::$log->add(Kohana::DEBUG, (string) $query);
  297. return $query->execute()->as_array();
  298. }
  299. /**
  300. * Returns paginated items with price
  301. *
  302. * @param int $category_id
  303. * @param int $page
  304. * @param int $relative_date
  305. * @return array
  306. */
  307. public function get_paged_price($category_id = NULL, $page = 1, $relative_date = NULL)
  308. {
  309. $page = (int) $page;
  310. // Pre calculate totals
  311. $total_rec = $this->get_total($category_id);
  312. $total_pages = $this->get_total_pages($total_rec);
  313. // Determine the correct page
  314. if ($page < 1)
  315. {
  316. $page = 1;
  317. }
  318. // Make sure the page does not exceed total pages
  319. if ($page > $total_pages)
  320. {
  321. $page = $total_pages;
  322. }
  323. $offset = ($page - 1) * self::ITEMS_PER_PAGE;
  324. if ( ! $relative_date)
  325. {
  326. $relative_date = time();
  327. }
  328. if ( ! $relative_date)
  329. {
  330. $relative_date = time();
  331. }
  332. // Inner query to latest price per item
  333. $inner_query = DB::select('id')->from('price')
  334. ->where('item_id', '=', DB::expr('i.id'))
  335. ->where('effective_date', '<=', (int) $relative_date)
  336. ->order_by('effective_date', 'DESC')
  337. ->limit(1);
  338. // Outer query
  339. $query = DB::select(
  340. 'i.id',
  341. 'i.name',
  342. 'i.description',
  343. 'i.category_id',
  344. array('c.name', 'category_name'),
  345. array('p.id', 'price_id'),
  346. 'p.price',
  347. 'p.effective_date'
  348. )
  349. ->from(array($this->_table, 'i'))
  350. ->join(array('category', 'c'))
  351. ->on('i.category_id', '=', 'c.id')
  352. ->join(array('price', 'p'), 'LEFT')
  353. ->on('p.id', '=', $inner_query);
  354. // Add category filter if specified
  355. if ($category_id)
  356. {
  357. $query->where('i.category_id', '=', (int) $category_id);
  358. }
  359. // Finish query
  360. $query->order_by('c.name', 'ASC')
  361. ->order_by('i.name', 'ASC')
  362. ->limit(self::ITEMS_PER_PAGE)
  363. ->offset($offset);
  364. return $query->execute()->as_array();
  365. }
  366. }