PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/model/Item.php

https://github.com/rockerest/capstone
PHP | 339 lines | 294 code | 37 blank | 8 comment | 23 complexity | 1a2562a98daf45976242778819adf849 MD5 | raw file
  1. <?php
  2. require_once('connect.php');
  3. require_once('Ingredient.php');
  4. require_once('Characteristic.php');
  5. require_once('Rating.php');
  6. require_once('View.php');
  7. require_once('Category.php');
  8. require_once('Search.php');
  9. class Item extends Base
  10. {
  11. public static function getByID($id)
  12. {
  13. global $db;
  14. //get raw item stuff (items table only)
  15. $itemSQL = "SELECT * FROM items WHERE itemid=?";
  16. $values = array($id);
  17. $item = $db->qwv($itemSQL, $values);
  18. return Item::wrap($item);
  19. }
  20. //for reporting purposes
  21. public static function getAll()
  22. {
  23. global $db;
  24. //get raw item stuff (items table only)
  25. $itemSQL = "SELECT * FROM items";
  26. $values = array();
  27. $item = $db->qwv($itemSQL, $values);
  28. return Item::wrap($item);
  29. }
  30. public static function getByName($name)
  31. {
  32. global $db;
  33. $sql = "SELECT * FROM items WHERE LOWER(name)=?";
  34. $values = array(strtolower($name));
  35. $items = $db->qwv($sql, $values);
  36. return Item::wrap($items);
  37. }
  38. public static function getByCharacteristic($cid)
  39. {
  40. global $db;
  41. $sql = "SELECT FROM items WHERE itemid IN (SELECT itemid FROM items_have_characteristics WHERE characteristicid=?)";
  42. $values = array($cid);
  43. $items = $db->qwv($sql, $values);
  44. return Item::wrap($items);
  45. }
  46. public static function getBySearch($str)
  47. {
  48. global $db;
  49. $sql = "SELECT * FROM items WHERE name LIKE ?";
  50. $values = array("%" . $str . "%");
  51. $res = $db->qwv($sql, $values);
  52. return Item::wrap($res);
  53. }
  54. public static function getByCategory($category)
  55. {
  56. global $db;
  57. $sql = "SELECT * FROM items WHERE categoryid=?";
  58. $values = array($category);
  59. $items = $db->qwv($sql, $values);
  60. return Item::wrap($items);
  61. }
  62. public static function getByCategorySearch($category)
  63. {
  64. global $db;
  65. $sql = "SELECT * FROM items WHERE categoryid IN (SELECT categoryid FROM categories WHERE number LIKE ?)";
  66. $values = array($category);
  67. $items = $db->qwv($sql, $values);
  68. return Item::wrap($items);
  69. }
  70. public static function add($name, $categoryid, $description, $image, $price, $prepTime, $cooklvl)
  71. {
  72. $item = Item::getByName($name);
  73. if( !$item )
  74. {
  75. $item = new Item(null, $name, $categoryid, $description, $image, $price, $prepTime, $cooklvl);
  76. return $item->save();
  77. }
  78. else
  79. {
  80. return $item;
  81. }
  82. }
  83. public static function wrap($items)
  84. {
  85. $itemList = array();
  86. foreach( $items as $item )
  87. {
  88. array_push($itemList, new Item($item['itemid'], $item['name'], $item['categoryid'], $item['description'], $item['image'], $item['price'], $item['prepTime'], $item['hasCookLevels']));
  89. }
  90. return Item::sendback($itemList);
  91. }
  92. private $itemid;
  93. private $name;
  94. private $categoryid;
  95. private $description;
  96. private $image;
  97. private $price;
  98. private $prepTime;
  99. private $hasCookLevels;
  100. public function __construct($itemid, $name, $categoryid, $description, $image, $price, $prepTime, $hasCookLevels)
  101. {
  102. $this->itemid = $itemid;
  103. $this->name = $name;
  104. $this->categoryid = $categoryid;
  105. $this->description = $description;
  106. $this->image = $image;
  107. $this->price = $price;
  108. $this->prepTime = $prepTime;
  109. $this->hasCookLevels = $hasCookLevels;
  110. }
  111. public function __get($var)
  112. {
  113. if( $var == 'ingredients' )
  114. {
  115. return Ingredient::getByItem($this->itemid);
  116. }
  117. elseif( $var == 'recommendations' )
  118. {
  119. return Ingredient::getRecommendedByItem($this->itemid);
  120. }
  121. elseif( $var == 'characteristics' )
  122. {
  123. return Characteristic::getByItem($this->itemid);
  124. }
  125. elseif( $var == 'category' )
  126. {
  127. return Category::getByID($this->categoryid);
  128. }
  129. elseif( $var == 'price' )
  130. {
  131. return money_format('%i', $this->price);
  132. }
  133. else
  134. {
  135. return $this->$var;
  136. }
  137. }
  138. public function __set($name, $value)
  139. {
  140. if( $name != 'itemid' )
  141. {
  142. $this->$name = $value;
  143. return $this->save();
  144. }
  145. }
  146. public function attach($type, $id = null)
  147. {
  148. if( $id == null )
  149. {
  150. if( $type instanceof Characteristic )
  151. {
  152. return $this->addChar($type->characteristicid);
  153. }
  154. elseif( $type instanceof Ingredient )
  155. {
  156. return $this->addIng($type->ingredientid);
  157. }
  158. else
  159. {
  160. return false;
  161. }
  162. }
  163. elseif( strtolower($type) == 'characteristic' )
  164. {
  165. if( $id instanceof Characteristic )
  166. {
  167. return $this->addChar( $id->characteristicid );
  168. }
  169. elseif( is_integer($id) )
  170. {
  171. return $this->addChar( $id );
  172. }
  173. else
  174. {
  175. return false;
  176. }
  177. }
  178. elseif( strtolower($type) == 'ingredient' )
  179. {
  180. if( $id instanceof Ingredient )
  181. {
  182. return $this->addIng( $id->ingredientid );
  183. }
  184. elseif( is_integer($id) )
  185. {
  186. return $this->addIng( $id );
  187. }
  188. else
  189. {
  190. return false;
  191. }
  192. }
  193. elseif( strtolower($type) == 'recommendation' )
  194. {
  195. if( $id instanceof Ingredient )
  196. {
  197. return $this->addRec( $id->ingredientid );
  198. }
  199. elseif( is_integer($id) )
  200. {
  201. return $this->addRec( $id );
  202. }
  203. else
  204. {
  205. return false;
  206. }
  207. }
  208. else
  209. {
  210. return false;
  211. }
  212. }
  213. public function save()
  214. {
  215. global $db;
  216. if( $this->itemid == null )
  217. {
  218. //if the item object is new and needs to be inserted
  219. $sql = "INSERT INTO items (name, categoryid, description, image, price, prepTime, hasCookLevels) VALUES (?, ?, ?, ?, ?, ?, ?)";
  220. $values = array( $this->name, $this->categoryid, $this->description, $this->image, $this->price, $this->prepTime, $this->hasCookLevels );
  221. $db->qwv($sql, $values);
  222. if( $db->stat() )
  223. {
  224. //set the itemid
  225. $this->itemid = $db->last();
  226. return $this;
  227. }
  228. else
  229. {
  230. return false;
  231. }
  232. }
  233. else
  234. {
  235. //if the object exists and is updated
  236. $sql = "UPDATE items SET name=?, categoryid=?, description=?, image=?, price=?, prepTime=?, hasCookLevels=? WHERE itemid=?";
  237. $values = array( $this->name, $this->categoryid, $this->description, $this->image, $this->price, $this->prepTime, $this->hasCookLevels, $this->itemid);
  238. $db->qwv($sql, $values);
  239. if( $db->stat() )
  240. {
  241. return $this;
  242. }
  243. else
  244. {
  245. return false;
  246. }
  247. }
  248. }
  249. public function delete()
  250. {
  251. global $db;
  252. $chars = is_array($this->characteristics) ? $this->characteristics : (is_bool($this->characteristics) ? array() : array($this->characteristics));
  253. $recs = is_array($this->recommendations) ? $this->recommendations : (is_bool($this->recommendations) ? array() : array($this->recommendations));
  254. $ings = is_array($this->ingredients) ? $this->ingredients : (is_bool($this->ingredients) ? array() : array($this->ingredients));
  255. foreach( $chars as $char )
  256. {
  257. $char->deleteLink($this->itemid);
  258. }
  259. foreach( $recs as $rec )
  260. {
  261. $rec->deleteRec($this->itemid);
  262. }
  263. foreach( $ings as $ing )
  264. {
  265. $ing->deleteLink($this->itemid);
  266. }
  267. //delete image
  268. if ( !@unlink("../images/" . $this->image) )
  269. {
  270. //deleting file failed. Tits.
  271. }
  272. $sql = "DELETE FROM items WHERE itemid=?";
  273. $values = array($this->itemid);
  274. $db->qwv($sql, $values);
  275. return $db->stat();
  276. }
  277. private function addChar($id)
  278. {
  279. global $db;
  280. $sql = "INSERT INTO items_have_characteristics (itemid, characteristicid) VALUES (?,?)";
  281. $values = array($this->itemid, $id);
  282. $db->qwv($sql, $values);
  283. return $db->stat();
  284. }
  285. private function addIng($id)
  286. {
  287. global $db;
  288. $sql = "INSERT INTO items_have_ingredients (itemid, ingredientid) VALUES (?,?)";
  289. $values = array($this->itemid, $id);
  290. $db->qwv($sql, $values);
  291. return $db->stat();
  292. }
  293. private function addRec($id)
  294. {
  295. global $db;
  296. $sql = "INSERT INTO items_have_recommendations (itemid, recommendationid) VALUES (?,?)";
  297. $values = array($this->itemid, $id);
  298. $db->qwv($sql, $values);
  299. return $db->stat();
  300. }
  301. }
  302. ?>