PageRenderTime 84ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/packages/products/product.class.php

https://bitbucket.org/navigatecms/navigatecms
PHP | 835 lines | 662 code | 134 blank | 39 comment | 74 complexity | 9cd69e1326df9a762d86166420e0f790 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause, AGPL-3.0, Apache-2.0
  1. <?php
  2. require_once(NAVIGATE_PATH.'/lib/packages/templates/template.class.php');
  3. require_once(NAVIGATE_PATH.'/lib/packages/brands/brand.class.php');
  4. require_once(NAVIGATE_PATH.'/lib/packages/webdictionary/webdictionary.class.php');
  5. require_once(NAVIGATE_PATH.'/lib/packages/webdictionary/webdictionary_history.class.php');
  6. require_once(NAVIGATE_PATH.'/lib/packages/paths/path.class.php');
  7. require_once(NAVIGATE_PATH.'/lib/packages/webuser_votes/webuser_vote.class.php');
  8. class product
  9. {
  10. public $id;
  11. public $website;
  12. public $category;
  13. public $template;
  14. public $date_to_display;
  15. public $date_published;
  16. public $date_unpublish;
  17. public $galleries;
  18. public $date_created;
  19. public $date_modified;
  20. public $comments_enabled_to; // 0 => nobody, 1=>registered, 2=>everyone
  21. public $comments_moderator; // user_id
  22. public $access; // 0 => everyone, 1 => logged in, 2 => not logged in, 3 => selected webuser groups
  23. public $groups;
  24. public $permission; // 0 => public, 1 => private (only navigate cms users), 2 => hidden
  25. public $views;
  26. public $author;
  27. public $votes;
  28. public $score;
  29. public $position;
  30. public $brand;
  31. public $sku; // stock-keeping unit (must be unique!)
  32. public $barcode;
  33. public $type; // defaults: 1 => shippable (standard), 2 => downloadable
  34. public $width;
  35. public $height;
  36. public $depth;
  37. public $size_unit;
  38. public $weight;
  39. public $weight_unit;
  40. public $inventory;
  41. public $stock_available; // including all variants
  42. public $cost;
  43. public $cost_currency;
  44. public $base_price;
  45. public $base_price_currency;
  46. public $tax_class;
  47. public $tax_value;
  48. public $sale_begin_date;
  49. public $sale_end_date;
  50. public $sale_price;
  51. public $sale_price_currency;
  52. // reserved for future use
  53. /*
  54. public $allow_preorder; // can be purchased even when no stock is available
  55. public $hide_if_no_stock; // hide the product when no stock available
  56. public $low_stock_threshold; // when the product has that number of units, it is considered "low stock"
  57. public $product_available_date;
  58. public $variants;
  59. */
  60. public $options;
  61. public $dictionary;
  62. public $paths;
  63. public $properties;
  64. private $_comments_count;
  65. public function load($id)
  66. {
  67. global $DB;
  68. global $website;
  69. if($DB->query('SELECT * FROM nv_products
  70. WHERE id = '.intval($id).'
  71. AND website = '.$website->id))
  72. {
  73. $data = $DB->result();
  74. $this->load_from_resultset($data); // there will be as many entries as languages enabled
  75. }
  76. }
  77. public function load_from_resultset($rs)
  78. {
  79. $main = $rs[0];
  80. $this->id = $main->id;
  81. $this->website = $main->website;
  82. $this->category = $main->category;
  83. $this->template = $main->template;
  84. $this->date_to_display = (empty($main->date_to_display)? '' : $main->date_to_display);
  85. $this->date_published = (empty($main->date_published)? '' : $main->date_published);
  86. $this->date_unpublish = (empty($main->date_unpublish)? '' : $main->date_unpublish);
  87. $this->date_created = $main->date_created;
  88. $this->date_modified = $main->date_modified;
  89. $this->galleries = mb_unserialize($main->galleries);
  90. $this->comments_enabled_to = $main->comments_enabled_to;
  91. $this->comments_moderator = $main->comments_moderator;
  92. $this->access = $main->access;
  93. $this->permission = $main->permission;
  94. $this->author = $main->author;
  95. $this->views = $main->views;
  96. $this->votes = $main->votes;
  97. $this->score = $main->score;
  98. $this->position = $main->position;
  99. $this->dictionary = webdictionary::load_element_strings('product', $this->id);
  100. $this->paths = path::loadElementPaths('product', $this->id);
  101. // to get the array of groups first we remove the "g" character
  102. $this->groups = $main->groups;
  103. if(!is_array($this->groups))
  104. {
  105. // to get the array of groups first we remove the "g" character
  106. $groups = str_replace('g', '', $this->groups);
  107. $this->groups = explode(',', $groups);
  108. }
  109. if(!is_array($this->groups))
  110. $this->groups = array($this->groups);
  111. $this->brand = $main->brand;
  112. $this->sku = $main->sku; // must be unique!
  113. $this->barcode = $main->barcode;
  114. $this->width = $main->width;
  115. $this->height = $main->height;
  116. $this->depth = $main->depth;
  117. $this->size_unit = $main->size_unit;
  118. $this->weight = $main->weight;
  119. $this->weight_unit = $main->weight_unit;
  120. $this->type = $main->type;
  121. $this->inventory = $main->inventory;
  122. $this->stock_available = $main->stock_available; // including all variants
  123. $this->base_price = $main->base_price;
  124. $this->base_price_currency = $main->base_price_currency;
  125. $this->tax_class = $main->tax_class;
  126. $this->tax_value = $main->tax_value;
  127. $this->cost = $main->cost;
  128. $this->cost_currency = $main->cost_currency;
  129. /*
  130. * falten per afegir:
  131. public $sale_begin_date;
  132. public $sale_end_date;
  133. public $sale_price;
  134. public $sale_price_currency;
  135. public $options;
  136. */
  137. }
  138. public function load_from_post()
  139. {
  140. global $website;
  141. global $user;
  142. $this->category = intval($_REQUEST['category']);
  143. $this->template = $_REQUEST['template'];
  144. $this->author = intval($_REQUEST['item-author']);
  145. $this->brand = $_REQUEST['product-brand-id'];
  146. if((!empty($this->brand) && !is_numeric($this->brand)) || $this->brand === 0)
  147. {
  148. // new brand! insert new object into database
  149. $brand = new brand();
  150. $brand->name = trim($_REQUEST['product-brand']);
  151. $brand->save();
  152. $this->brand = $brand->id;
  153. }
  154. $this->date_to_display = (empty($_REQUEST['date_to_display'])? '' : core_date2ts($_REQUEST['date_to_display']));
  155. $this->date_published = (empty($_REQUEST['date_published'])? '' : core_date2ts($_REQUEST['date_published']));
  156. $this->date_unpublish = (empty($_REQUEST['date_unpublish'])? '' : core_date2ts($_REQUEST['date_unpublish']));
  157. $this->access = intval($_REQUEST['access']);
  158. $this->groups = $_REQUEST['groups'];
  159. if($this->access < 3)
  160. $this->groups = array();
  161. if($user->permission("products.publish") != 'false')
  162. $this->permission = intval($_REQUEST['permission']);
  163. // if comment settings were not visible, keep the original values
  164. if(isset($_REQUEST['product-comments_enabled_to']))
  165. {
  166. $this->comments_enabled_to = intval($_REQUEST['product-comments_enabled_to']);
  167. $this->comments_moderator = intval($_REQUEST['product-comments_moderator']);
  168. }
  169. // language strings and options
  170. $this->dictionary = array();
  171. $this->paths = array();
  172. $template = $this->load_template();
  173. $fields = array('title', 'tags');
  174. if(!is_array($template->sections))
  175. $template->sections = array('id' => 'main');
  176. foreach($template->sections as $section)
  177. {
  178. if(is_object($section))
  179. $section = (array) $section;
  180. if(is_array($section))
  181. $section = $section['id'];
  182. $fields[] = 'section-'.$section;
  183. }
  184. foreach($_REQUEST as $key => $value)
  185. {
  186. if(empty($value)) continue;
  187. foreach($fields as $field)
  188. {
  189. if(substr($key, 0, strlen($field.'-'))==$field.'-')
  190. $this->dictionary[substr($key, strlen($field.'-'))][$field] = $value;
  191. }
  192. if(substr($key, 0, strlen('path-'))=='path-')
  193. $this->paths[substr($key, strlen('path-'))] = $value;
  194. }
  195. // image galleries
  196. $this->galleries = array();
  197. $items = explode("#", $_REQUEST['products-gallery-elements-order']);
  198. if(!is_array($items)) $items = array();
  199. $gallery_items = array();
  200. $gallery_items[0] = array();
  201. if(!is_array($website->languages)) $website->languages = array();
  202. foreach($items as $item)
  203. {
  204. if(empty($item)) continue;
  205. // capture image captions
  206. $gallery_items[0][$item] = array();
  207. foreach($website->languages_list as $lang)
  208. {
  209. $gallery_items[0][$item][$lang] = $_REQUEST['products-gallery-item-'.$item.'-dictionary-'.$lang];
  210. }
  211. }
  212. $this->galleries = $gallery_items;
  213. // galleries[0] = array( [id-file] => array(es => '', ca => '',.. ), [id-file-2] => array... )
  214. $this->sku = $_REQUEST['product-sku'];
  215. $this->barcode = $_REQUEST['product-barcode'];
  216. $this->width = core_string2decimal($_REQUEST['product-width']);
  217. $this->height = core_string2decimal($_REQUEST['product-height']);
  218. $this->depth = core_string2decimal($_REQUEST['product-depth']);
  219. $this->size_unit = $_REQUEST['product-size_unit'];
  220. $this->weight = core_string2decimal($_REQUEST['product-weight']);
  221. $this->weight_unit = $_REQUEST['product-weight_unit'];
  222. $this->inventory = $_REQUEST['product-track_inventory'];
  223. $this->stock_available = $_REQUEST['product-stock_available'];
  224. $this->type = $_REQUEST['product-type'];
  225. $this->base_price = core_string2decimal($_REQUEST['product-base_price']);
  226. $this->base_price_currency = $_REQUEST['product-base_price_currency'];
  227. $this->tax_class = $_REQUEST['product-tax_class'];
  228. $this->tax_value = core_string2decimal($_REQUEST['product-tax_value']);
  229. $this->cost = core_string2decimal($_REQUEST['product-cost']);
  230. $this->cost_currency = $_REQUEST['product-cost_currency'];
  231. }
  232. public function save()
  233. {
  234. if(!empty($this->id))
  235. return $this->update();
  236. else
  237. return $this->insert();
  238. }
  239. public function delete()
  240. {
  241. global $DB;
  242. global $user;
  243. if($user->permission("products.delete") == 'false')
  244. throw new Exception(t(610, "Sorry, you are not allowed to execute this function."));
  245. if(!empty($this->id) && !empty($this->website))
  246. {
  247. // TODO: remove variants, price-listings, etc.
  248. // remove dictionary elements
  249. webdictionary::save_element_strings('product', $this->id, array(), $this->website);
  250. // remove path elements
  251. path::saveElementPaths('product', $this->id, array(), $this->website);
  252. // remove all votes assigned to element
  253. webuser_vote::remove_object_votes('product', $this->id);
  254. // remove all element properties
  255. property::remove_properties('product', $this->id, $this->website);
  256. // remove grid notes
  257. grid_notes::remove_all('product', $this->id);
  258. // finally remove the product
  259. $DB->execute('
  260. DELETE FROM nv_products
  261. WHERE id = '.intval($this->id).'
  262. AND website = '.$this->website
  263. );
  264. }
  265. return $DB->get_affected_rows();
  266. }
  267. public function insert()
  268. {
  269. global $DB;
  270. global $website;
  271. global $events;
  272. global $user;
  273. if(!empty($user->id))
  274. {
  275. if( $user->permission("products.create") == 'false' ||
  276. !structure::category_allowed($this->category)
  277. )
  278. throw new Exception(t(610, "Sorry, you are not allowed to execute this function."));
  279. }
  280. $this->date_created = core_time();
  281. $this->date_modified = core_time();
  282. if(empty($this->comments_enabled_to))
  283. {
  284. // apply default comment settings from website properties
  285. $this->comments_enabled_to = $website->comments_enabled_for;
  286. $this->comments_moderator = $website->comments_default_moderator;
  287. if($this->comments_moderator == 'c_author')
  288. $this->comments_moderator = $this->author;
  289. }
  290. $groups = '';
  291. if(is_array($this->groups))
  292. {
  293. $this->groups = array_unique($this->groups); // remove duplicates
  294. $this->groups = array_filter($this->groups); // remove empty
  295. if(!empty($this->groups))
  296. $groups = 'g'.implode(',g', $this->groups);
  297. }
  298. if($groups == 'g')
  299. $groups = '';
  300. if(empty($this->website))
  301. $this->website = $website->id;
  302. if(!empty($user->id) && $user->permission("products.publish") == 'false')
  303. {
  304. if($this->permission == 0)
  305. $this->permission = 1;
  306. }
  307. if(empty($this->position))
  308. {
  309. $last_position_in_category = $DB->query_single('MAX(position)', 'nv_products', ' category = '.value_or_default($this->category, 0));
  310. $default_position = $last_position_in_category + 1;
  311. }
  312. $ok = $DB->execute('
  313. INSERT INTO nv_products
  314. (id, website, category, type, template, author, brand,
  315. date_to_display, date_published, date_unpublish, date_created, date_modified,
  316. sku, barcode, base_price, base_price_currency, tax_class, tax_value, cost, cost_currency,
  317. sale_price, sale_price_currency, sale_begin_date, sale_end_date,
  318. width, height, depth, size_unit, weight, weight_unit,
  319. inventory, stock_available,
  320. options,
  321. galleries, comments_enabled_to, comments_moderator,
  322. access, groups, permission, views, votes, score, position)
  323. VALUES
  324. (:id, :website, :category, :type, :template, :author, :brand,
  325. :date_to_display, :date_published, :date_unpublish, :date_created, :date_modified,
  326. :sku, :barcode, :base_price, :base_price_currency, :tax_class, :tax_value, :cost, :cost_currency,
  327. :sale_price, :sale_price_currency, :sale_begin_date, :sale_end_date,
  328. :width, :height, :depth, :size_unit, :weight, :weight_unit,
  329. :inventory, :stock_available,
  330. :options,
  331. :galleries, :comments_enabled_to, :comments_moderator,
  332. :access, :groups, :permission, :views, :votes, :score, :position)
  333. ',
  334. array(
  335. ":id" => 0,
  336. ":website" => $this->website,
  337. ":category" => value_or_default($this->category, 0),
  338. ":template" => value_or_default($this->template, ''),
  339. ":type" => value_or_default($this->type, 0),
  340. ":brand" => value_or_default($this->brand, 0),
  341. ":date_to_display" => intval($this->date_to_display),
  342. ":date_published" => intval($this->date_published),
  343. ":date_unpublish" => intval($this->date_unpublish),
  344. ":date_created" => $this->date_created,
  345. ":date_modified" => $this->date_modified,
  346. ":sku" => value_or_default($this->sku, ''),
  347. ":barcode" => value_or_default($this->barcode, ''),
  348. ":base_price" => value_or_default($this->base_price, 0),
  349. ":base_price_currency" => value_or_default($this->base_price_currency, ""),
  350. ":tax_class" => value_or_default($this->tax_class, "included"),
  351. ":tax_value" => value_or_default($this->tax_value, 0),
  352. ":cost" => value_or_default($this->cost, 0),
  353. ":cost_currency" => value_or_default($this->cost_currency, ""),
  354. ":sale_price" => value_or_default($this->sale_price, 0),
  355. ":sale_price_currency" => value_or_default($this->sale_price_currency, ""),
  356. ":sale_begin_date" => intval($this->sale_begin_date),
  357. ":sale_end_date" => intval($this->sale_end_date),
  358. ":width" => value_or_default($this->width, 0),
  359. ":height" => value_or_default($this->height, 0),
  360. ":depth" => value_or_default($this->depth, 0),
  361. ":size_unit" => value_or_default($this->size_unit, 'cm'),
  362. ":weight" => value_or_default($this->weight, 0),
  363. ":weight_unit" => value_or_default($this->weight_unit, 'kg'),
  364. ":inventory" => value_or_default($this->inventory, 0),
  365. ":stock_available" => value_or_default($this->stock_available, 0),
  366. ":options" => json_encode($this->options),
  367. ":author" => value_or_default($this->author, 0),
  368. ":galleries" => serialize($this->galleries),
  369. ":comments_enabled_to" => value_or_default($this->comments_enabled_to, 0),
  370. ":comments_moderator" => value_or_default($this->comments_moderator, 0),
  371. ":access" => value_or_default($this->access, 0),
  372. ":groups" => $groups,
  373. ":permission" => value_or_default($this->permission, 0),
  374. ":views" => 0,
  375. ":votes" => 0,
  376. ":score" => 0,
  377. ":position" => value_or_default($this->position, $default_position)
  378. )
  379. );
  380. if(!$ok)
  381. throw new Exception($DB->get_last_error());
  382. $this->id = $DB->get_last_id();
  383. webdictionary::save_element_strings('product', $this->id, $this->dictionary, $this->website);
  384. webdictionary_history::save_element_strings('product', $this->id, $this->dictionary, false, $this->website);
  385. path::saveElementPaths('product', $this->id, $this->paths, $this->website);
  386. if(method_exists($events, 'trigger'))
  387. {
  388. $events->trigger(
  389. 'product',
  390. 'save',
  391. array(
  392. 'product' => $this
  393. )
  394. );
  395. }
  396. return true;
  397. }
  398. public function update()
  399. {
  400. global $DB;
  401. global $events;
  402. global $user;
  403. if(!is_null($user))
  404. {
  405. if($user->permission("products.edit") == 'false' && $this->author != $user->id)
  406. throw new Exception(t(610, "Sorry, you are not allowed to execute this function."));
  407. if( !structure::category_allowed($this->category) )
  408. throw new Exception(t(610, "Sorry, you are not allowed to execute this function."));
  409. }
  410. $this->date_modified = core_time();
  411. $groups = '';
  412. if(is_array($this->groups))
  413. {
  414. $this->groups = array_unique($this->groups); // remove duplicates
  415. $this->groups = array_filter($this->groups); // remove empty
  416. if(!empty($this->groups))
  417. $groups = 'g'.implode(',g', $this->groups);
  418. }
  419. if($groups == 'g')
  420. $groups = '';
  421. $ok = $DB->execute('
  422. UPDATE nv_products
  423. SET
  424. category = :category,
  425. type = :type,
  426. template = :template,
  427. author = :author,
  428. brand = :brand,
  429. date_to_display = :date_to_display,
  430. date_published = :date_published,
  431. date_unpublish = :date_unpublish,
  432. date_modified = :date_modified,
  433. sku = :sku,
  434. barcode = :barcode,
  435. base_price = :base_price,
  436. base_price_currency = :base_price_currency,
  437. tax_class = :tax_class,
  438. tax_value = :tax_value,
  439. cost = :cost,
  440. cost_currency = :cost_currency,
  441. sale_price = :sale_price,
  442. sale_price_currency = :sale_price_currency,
  443. sale_begin_date = :sale_begin_date,
  444. sale_end_date = :sale_end_date,
  445. width = :width,
  446. height = :height,
  447. depth = :depth,
  448. size_unit = :size_unit,
  449. weight = :weight,
  450. weight_unit = :weight_unit,
  451. inventory = :inventory,
  452. stock_available = :stock_available,
  453. options = :options,
  454. galleries = :galleries,
  455. comments_enabled_to = :comments_enabled_to,
  456. comments_moderator = :comments_moderator,
  457. access = :access,
  458. groups = :groups,
  459. permission = :permission,
  460. views = :views,
  461. votes = :votes,
  462. score = :score,
  463. position = :position
  464. WHERE id = :id
  465. AND website = :website',
  466. array(
  467. ":id" => $this->id,
  468. ":website" => $this->website,
  469. ":category" => value_or_default($this->category, 0),
  470. ":template" => value_or_default($this->template, ''),
  471. ":type" => value_or_default($this->type, 0),
  472. ":brand" => value_or_default($this->brand, 0),
  473. ":date_to_display" => intval($this->date_to_display),
  474. ":date_published" => intval($this->date_published),
  475. ":date_unpublish" => intval($this->date_unpublish),
  476. ":date_modified" => $this->date_modified,
  477. ":sku" => value_or_default($this->sku, ''),
  478. ":barcode" => value_or_default($this->barcode, ''),
  479. ":base_price" => value_or_default($this->base_price, 0),
  480. ":base_price_currency" => value_or_default($this->base_price_currency, ""),
  481. ":tax_class" => value_or_default($this->tax_class, "included"),
  482. ":tax_value" => value_or_default($this->tax_value, 0),
  483. ":cost" => value_or_default($this->cost, 0),
  484. ":cost_currency" => value_or_default($this->cost_currency, ""),
  485. ":sale_price" => value_or_default($this->sale_price, 0),
  486. ":sale_price_currency" => value_or_default($this->sale_price, ""),
  487. ":sale_begin_date" => intval($this->sale_begin_date),
  488. ":sale_end_date" => intval($this->sale_end_date),
  489. ":width" => value_or_default($this->width, 0),
  490. ":height" => value_or_default($this->height, 0),
  491. ":depth" => value_or_default($this->depth, 0),
  492. ":size_unit" => value_or_default($this->size_unit, 'cm'),
  493. ":weight" => value_or_default($this->weight, 0),
  494. ":weight_unit" => value_or_default($this->weight_unit, 'kg'),
  495. ":inventory" => value_or_default($this->inventory, 0),
  496. ":stock_available" => value_or_default($this->stock_available, 0),
  497. ":options" => json_encode($this->options),
  498. ":author" => value_or_default($this->author, 0),
  499. ":galleries" => serialize($this->galleries),
  500. ":comments_enabled_to" => value_or_default($this->comments_enabled_to, 0),
  501. ":comments_moderator" => value_or_default($this->comments_moderator, 0),
  502. ":access" => value_or_default($this->access, 0),
  503. ":groups" => $groups,
  504. ":permission" => value_or_default($this->permission, 0),
  505. ":views" => $this->views,
  506. ":votes" => $this->votes,
  507. ":score" => $this->score,
  508. ":position" => value_or_default($this->position, 0)
  509. )
  510. );
  511. if(!$ok)
  512. throw new Exception($DB->get_last_error());
  513. webdictionary::save_element_strings('product', $this->id, $this->dictionary, $this->website);
  514. webdictionary_history::save_element_strings('product', $this->id, $this->dictionary, false, $this->website);
  515. path::saveElementPaths('product', $this->id, $this->paths, $this->website);
  516. $events->trigger(
  517. 'product',
  518. 'save',
  519. array(
  520. 'product' => $this
  521. )
  522. );
  523. return true;
  524. }
  525. public function load_template()
  526. {
  527. $template = new template();
  528. $template->load($this->template);
  529. return $template;
  530. }
  531. public function property($property_name, $raw=false)
  532. {
  533. // load properties if not already done
  534. if(empty($this->properties))
  535. $this->properties = property::load_properties('product', $this->template, 'product', $this->id);
  536. for($p=0; $p < count($this->properties); $p++)
  537. {
  538. if($this->properties[$p]->name==$property_name || $this->properties[$p]->id==$property_name)
  539. {
  540. if($raw)
  541. $out = $this->properties[$p]->value;
  542. else
  543. $out = $this->properties[$p]->value;
  544. break;
  545. }
  546. }
  547. return $out;
  548. }
  549. public function property_exists($property_name)
  550. {
  551. // load properties if not already done
  552. if(empty($this->properties))
  553. $this->properties = property::load_properties('product', $this->template, 'product', $this->id);
  554. for($p=0; $p < count($this->properties); $p++)
  555. {
  556. if($this->properties[$p]->name==$property_name || $this->properties[$p]->id==$property_name)
  557. return true;
  558. }
  559. return false;
  560. }
  561. public function property_definition($property_name)
  562. {
  563. $out = "";
  564. // load properties if not already done
  565. if(empty($this->properties))
  566. $this->properties = property::load_properties('product', $this->template, 'product', $this->id);
  567. for($p=0; $p < count($this->properties); $p++)
  568. {
  569. if($this->properties[$p]->name==$property_name || $this->properties[$p]->id==$property_name)
  570. {
  571. $out = $this->properties[$p];
  572. break;
  573. }
  574. }
  575. return $out;
  576. }
  577. public function link($lang)
  578. {
  579. $url = $this->paths[$lang];
  580. if(empty($url))
  581. $url = '/product/'.$this->id;
  582. $url = nvweb_prepare_link($url);
  583. return $url;
  584. }
  585. public function comments_count()
  586. {
  587. global $DB;
  588. if(empty($this->_comments_count))
  589. {
  590. $DB->query('
  591. SELECT COUNT(*) as total
  592. FROM nv_comments
  593. WHERE website = ' . protect($this->website) . '
  594. AND object_type = "product"
  595. AND object_id = ' . protect($this->id) . '
  596. AND status = 0'
  597. );
  598. $out = $DB->result('total');
  599. $this->_comments_count = $out[0];
  600. }
  601. return $this->_comments_count;
  602. }
  603. public static function reorder($order)
  604. {
  605. global $DB;
  606. global $website;
  607. $items = explode("#", $order);
  608. for($i=0; $i < count($items); $i++)
  609. {
  610. if(empty($items[$i])) continue;
  611. $ok = $DB->execute('
  612. UPDATE nv_products
  613. SET position = '.($i+1).'
  614. WHERE id = '.$items[$i].' AND
  615. website = '.$website->id
  616. );
  617. if(!$ok)
  618. return array("error" => $DB->get_last_error());
  619. }
  620. return true;
  621. }
  622. public function quicksearch($text)
  623. {
  624. global $DB;
  625. global $website;
  626. $where = '';
  627. $search = explode(" ", $text);
  628. $search = array_filter($search);
  629. sort($search);
  630. foreach($search as $text)
  631. {
  632. $like = ' LIKE '.protect('%'.$text.'%');
  633. // we search for the IDs at the dictionary NOW (to avoid inefficient requests)
  634. $DB->query('SELECT DISTINCT (nvw.node_id)
  635. FROM nv_webdictionary nvw
  636. WHERE nvw.node_type = "product"
  637. AND nvw.website = '.$website->id.'
  638. AND nvw.text '.$like, 'array');
  639. $dict_ids = $DB->result("node_id");
  640. // all columns to look for
  641. $cols[] = 'p.id' . $like;
  642. if(!empty($dict_ids))
  643. $cols[] = 'p.id IN ('.implode(',', $dict_ids).')';
  644. $where .= ' AND ( ';
  645. $where .= implode( ' OR ', $cols);
  646. $where .= ')';
  647. }
  648. return $where;
  649. }
  650. public function backup($type='json')
  651. {
  652. global $DB;
  653. global $website;
  654. $out = array();
  655. $DB->query('SELECT * FROM nv_products WHERE website = '.protect($website->id), 'object');
  656. if($type='json')
  657. $out = json_encode($DB->result());
  658. return $out;
  659. }
  660. public static function size_units()
  661. {
  662. $size_units = array(
  663. 'cm', 'm', 'mm', 'in'
  664. );
  665. return $size_units;
  666. }
  667. public static function weight_units()
  668. {
  669. $weight_units = array(
  670. 'g', 'kg', 'lb'
  671. );
  672. return $weight_units;
  673. }
  674. public static function currencies()
  675. {
  676. $currencies = array(
  677. 'euro' => '€',
  678. 'dollar' => '$'
  679. );
  680. return $currencies;
  681. }
  682. public static function tax_classes()
  683. {
  684. $tax_classes = array(
  685. 'included' => t(679, "Included"),
  686. 'free' => t(100, "Free"),
  687. 'custom' => "(". t(680, "Custom") . ")"
  688. );
  689. return $tax_classes;
  690. }
  691. public static function __set_state(array $obj)
  692. {
  693. $tmp = new product();
  694. foreach($obj as $key => $val)
  695. $tmp->$key = $val;
  696. return $tmp;
  697. }
  698. }
  699. ?>