PageRenderTime 78ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/manage/controllers/store/products.php

https://bitbucket.org/pooshonk/esw
PHP | 729 lines | 575 code | 98 blank | 56 comment | 61 complexity | e499dbc989dd404536b557da60ec0f0d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class Productscontroller extends Contentelementcontroller{
  3. public function __construct( PeacockCarterFrameworkRegistry $registry, $directCall )
  4. {
  5. $this->registry = $registry;
  6. $this->registry->getObject('adminmenubuilder')->buildMenu( 'store.products' );
  7. if( $this->registry->getObject('authenticate')->isAuthorised('store_products') == true )
  8. {
  9. if( $this->registry->getURLBit(2) != '' )
  10. {
  11. switch( $this->registry->getURLBit(2) )
  12. {
  13. case 'toggle-visibility':
  14. $this->toggleCategoryActive( $this->registry->getURLBit(3), $this->registry->getURLBit(4) );
  15. break;
  16. case 'toggle-security':
  17. $this->toggleCategorySecurity( $this->registry->getURLBit(3), $this->registry->getURLBit(4) );
  18. break;
  19. case 'create':
  20. $this->createProduct();
  21. break;
  22. case 'edit':
  23. $this->editProduct( intval( $this->registry->getURLBit(3) ) );
  24. break;
  25. case 'move-down':
  26. $this->moveCategoryDown( intval( $this->registry->getURLBit(3) ) );
  27. break;
  28. case 'move-up':
  29. $this->moveCategoryUp( intval( $this->registry->getURLBit(3) ) );
  30. break;
  31. case 'move-right':
  32. $this->moveCategoryRight( intval( $this->registry->getURLBit(3) ) );
  33. break;
  34. case 'move-left':
  35. $this->moveCategoryLeft( intval( $this->registry->getURLBit(3) ) );
  36. break;
  37. case 'delete':
  38. $this->deleteCategory( intval( $this->registry->getURLBit(3) ) );
  39. break;
  40. case 'by-category':
  41. $this->productsByCategory( intval( $this->registry->getURLBit(3) ) );
  42. break;
  43. case 'view-revision':
  44. $this->viewRevision( intval( $this->registry->getURLBit(3) ) );
  45. break;
  46. default:
  47. $this->listContent();
  48. break;
  49. }
  50. }
  51. else
  52. {
  53. $this->listContent();
  54. }
  55. }
  56. else
  57. {
  58. $this->registry->getObject('authenticate')->redirectToAuthorised('store');
  59. }
  60. }
  61. private function viewRevision( $revision )
  62. {
  63. $sql = "SELECT f.ID as fid, v.*, c.price, c.weight, c.sku, c.stock, c.image, IF( c.featured > 0, 'checked=\"checked\"', '' ) as featured, IF( c.allow_upload > 0, 'checked=\"checked\"', '' ) as recfiles FROM content_versions v, content f, content_versions_store_products c WHERE f.current_revision = v.ID AND v.ID = c.version_id AND v.ID = {$revision} LIMIT 1";
  64. $this->registry->getObject('db')->executeQuery( $sql );
  65. if( $this->registry->getObject('db')->numRows() > 0 )
  66. {
  67. //version data
  68. $data = $this->registry->getObject('db')->getRows();
  69. $fid = $data['fid'];
  70. $this->registry->getObject('template')->dataToTags( $data, 'pd_' );
  71. $this->registry->getObject('template')->getPage()->addTag( 'uploads_url', $this->registry->getSetting('uploads_url') );
  72. //categories
  73. $sql = "SELECT v.name as category_name, c.ID as category_id FROM content_versions v, content c, content_types t WHERE v.ID = c.current_revision AND c.type = t.ID and t.reference = 'product-cat' AND c.deleted = 0 AND c.approved = 1";
  74. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  75. $this->registry->getObject('template')->getPage()->addTag( 'categories', array( 'SQL', $cache ) );
  76. // SELECTED CATEGORIES
  77. $cats = array();
  78. $sql = "SELECT category_id FROM store_product_category_associations WHERE product_id=" . $fid;
  79. $this->registry->getObject('db')->executeQuery($sql);
  80. while( $row = $this->registry->getObject('db')->getRows() )
  81. {
  82. $cats[] = $row;
  83. }
  84. if( ! empty($cats) )
  85. {
  86. foreach( $cats as $cat )
  87. {
  88. $this->registry->getObject('template')->getPage()->addPPTag($cat['category_id'].'_cat', "checked='checked'");
  89. }
  90. }
  91. // variation groupings
  92. $sql = "SELECT ID as attribute_id, name as attribute_name FROM store_product_variations";
  93. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  94. $this->registry->getObject('template')->getPage()->addTag( 'variations', array( 'SQL', $cache ) );
  95. $sql = "SELECT ID, name as attribute_value_name, attribute FROM store_product_variation_values ORDER BY attribute, `order`";
  96. $this->registry->getObject('db')->executeQuery( $sql );
  97. $attributeValues = array();
  98. while( $row = $this->registry->getObject('db')->getRows() )
  99. {
  100. $attributeValues[ $row['attribute'] ][] = $row;
  101. }
  102. // variation values
  103. foreach( $attributeValues as $attribute => $data )
  104. {
  105. $cache = $this->registry->getObject('db')->cacheData( $data );
  106. $this->registry->getObject('template')->getPage()->addTag( 'variation_values_' . $attribute , array( 'DATA', $cache ) );
  107. }
  108. // selected variations
  109. $sql = "SELECT v.ID, a.cost_difference FROM store_product_variation_values v, store_product_variation_associations a WHERE a.variation_value = v.ID AND a.product_id =" . $fid;
  110. $this->registry->getObject('db')->executeQuery( $sql );
  111. $p = array();
  112. while ( $row = $this->registry->getObject('db')->getRows() )
  113. {
  114. $p[$row['ID']] = $row['cost_difference'];
  115. }
  116. if( ! empty( $p ) )
  117. {
  118. foreach( $p as $v => $c )
  119. {
  120. $this->registry->getObject('template')->getPage()->addPPTag( $v.'_v', "checked='checked'");
  121. $this->registry->getObject('template')->getPage()->addPPTag( 'form_' . $v.'_v_c', $c );
  122. }
  123. }
  124. // product attributes
  125. $sql = "SELECT ID as attribute_id, name as attribute_name FROM store_product_filter_attribute_types WHERE productContainedAttribute=0";
  126. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  127. $this->registry->getObject('template')->getPage()->addTag( 'attributes', array( 'SQL', $cache ) );
  128. $sql = "SELECT ID, name as attribute_value_name, attribute FROM store_product_filter_attribute_values ORDER BY attribute, `order`";
  129. $this->registry->getObject('db')->executeQuery( $sql );
  130. $attributeValues = array();
  131. while( $row = $this->registry->getObject('db')->getRows() )
  132. {
  133. $attributeValues[ $row['attribute'] ][] = $row;
  134. }
  135. // attribute values
  136. foreach( $attributeValues as $attribute => $data )
  137. {
  138. //echo '<pre>' . print_r( $data, true ) . '</pre>';
  139. $cache = $this->registry->getObject('db')->cacheData( $data );
  140. $this->registry->getObject('template')->getPage()->addTag( 'attribute_values_' . $attribute , array( 'DATA', $cache ) );
  141. }
  142. // selected attributes
  143. $sql = "SELECT v.ID FROM store_product_filter_attribute_values v, store_product_attribute_associations a WHERE v.ID = a.attribute_value AND a.product_id=" .$fid;
  144. $this->registry->getObject('db')->executeQuery( $sql );
  145. $a = array();
  146. while( $row = $this->registry->getObject('db')->getRows() )
  147. {
  148. $a[] = $row;
  149. }
  150. if( ! empty( $a ) )
  151. {
  152. foreach( $a as $atype => $avalue )
  153. {
  154. $this->registry->getObject('template')->getPage()->addPPTag( $avalue['ID'].'_sel', "selected='selected'");
  155. }
  156. }
  157. // shipping methods:
  158. $sql = "SELECT ID as method_id, name as method_name, default_cost as method_cost FROM store_shipping_methods";
  159. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  160. $this->registry->getObject('template')->getPage()->addTag( 'shipping_methods', array( 'SQL', $cache ) );
  161. $sql = "SELECT * FROM store_product_shipping_costs WHERE product_id=" .$fid;
  162. $this->registry->getObject('db')->executeQuery( $sql );
  163. $sc = array();
  164. while( $row = $this->registry->getObject('db')->getRows() )
  165. {
  166. $sc[$row['shipping_id']] = $row['cost'];
  167. }
  168. if( ! empty($sc) )
  169. {
  170. foreach( $sc as $m => $c )
  171. {
  172. $this->registry->getObject('template')->getPage()->addPPTag( $m.'_sc', $c);
  173. }
  174. }
  175. $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'store/products/viewrevision.tpl.php', 'footer.tpl.php');
  176. }
  177. else
  178. {
  179. $this->registry->errorPage('Content not found', 'Sorry there was an error locating the content.');
  180. }
  181. }
  182. private function productsByCategory( $category )
  183. {
  184. $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'store/products/list.tpl.php', 'footer.tpl.php');
  185. $this->buildSiteStructure( $category );
  186. }
  187. public function moveProductDown( $page )
  188. {
  189. parent::setType('product');
  190. parent::moveDown( $page );
  191. header('Location: ' . $this->registry->buildURL(array('store'), '', true ) );
  192. exit();
  193. }
  194. public function moveProductUp( $page )
  195. {
  196. parent::setType('product');
  197. parent::moveUp( $page );
  198. header('Location: ' . $this->registry->buildURL(array('store'), '', true ) );
  199. exit();
  200. }
  201. public function moveProductLeft( $page )
  202. {
  203. parent::setType('product');
  204. parent::moveLeft( $page );
  205. header('Location: ' . $this->registry->buildURL(array('store'), '', true ) );
  206. exit();
  207. }
  208. public function moveProductRight( $page )
  209. {
  210. parent::setType('product');
  211. parent::moveRight( $page );
  212. header('Location: ' . $this->registry->buildURL(array('store'), '', true ) );
  213. exit();
  214. }
  215. private function editProduct( $pid )
  216. {
  217. $this->registry->getObject('template')->getPage()->addTag( 'pID', $pid );
  218. //$this->buildSiteStructure();
  219. require_once( FRAMEWORK_PATH . 'models/content.php');
  220. require_once( FRAMEWORK_PATH . 'models/store/product.php');
  221. $product = new Product( $this->registry, '', $pid );
  222. if( isset( $_POST['edit_product'] ) )
  223. {
  224. $product->setName( $this->registry->getObject('db')->sanitizeData( $_POST['page_name'] ) );
  225. $product->setTitle( $this->registry->getObject('db')->sanitizeData( $_POST['page_metatitle'] ) );
  226. $product->setHeading( $this->registry->getObject('db')->sanitizeData( $_POST['page_heading'] ) );
  227. $product->setContent( $this->registry->getObject('db')->sanitizeData( $_POST['page_content'] ) );
  228. $product->generateType();
  229. $product->setAuthor( $this->registry->getObject('authenticate')->getUserID() );
  230. $product->setMetadescription( $this->registry->getObject('db')->sanitizeData( $_POST['page_metadescription'] ) );
  231. $product->setMetakeywords( $this->registry->getObject('db')->sanitizeData( $_POST['page_metakeywords'] ) );
  232. // product specific
  233. //$product->setOverview( $this->registry->getObject('db')->sanitizeData( $_POST['page_overview'] ) );
  234. // process image upload
  235. if( isset( $_FILES['imageupload'] ) )
  236. {
  237. include( FRAMEWORK_PATH . 'lib/images/imagemanager.class.php');
  238. $manager = new Imagemanager();
  239. if ( $manager->loadFromPost( 'imageupload', $this->registry->getSetting('uploads_path') . 'store/products/original/' ))
  240. {
  241. $manager->resizeScaleHeight( $this->registry->getSetting('image.large_width') );
  242. $manager->save( $this->registry->getSetting('uploads_path') . 'store/products/large/' . $manager->getName() );
  243. $manager->resizeScaleHeight( $this->registry->getSetting('image.thumb_width') );
  244. $manager->save( $this->registry->getSetting('uploads_path') . 'store/products/small/' . $manager->getName() );
  245. $product->setImage( $this->registry->getObject('db')->sanitizeData( $manager->getName() ) );
  246. }
  247. }
  248. $product->setPrice( $this->registry->getObject('db')->sanitizeData( $_POST['price'] ) );
  249. $product->setWeight( $this->registry->getObject('db')->sanitizeData( $_POST['weight'] ) );
  250. $product->setSKU( $this->registry->getObject('db')->sanitizeData( $_POST['sku'] ) );
  251. $product->setStock( $this->registry->getObject('db')->sanitizeData( $_POST['stock'] ) );
  252. if( isset( $_POST['featured'] ) && $_POST['featured'] == 'yes' )
  253. {
  254. $product->setFeatured( 1 );
  255. }
  256. else
  257. {
  258. $product->setFeatured( 0 );
  259. }
  260. if( isset( $_POST['receives_files'] ) && $_POST['receives_files'] == 'yes' )
  261. {
  262. $product->setAllowUploads( 1 );
  263. }
  264. else
  265. {
  266. $product->setAllowUploads( 0 );
  267. }
  268. // variations
  269. $variations = ( isset( $_POST['variations'] ) && is_array( $_POST['variations'] ) ) ? $_POST['variations'] : array();
  270. $variations_new = array();
  271. foreach( $variations as $v )
  272. {
  273. $variations_new[ $v ] = $this->registry->getObject('db')->sanitizeData( $_POST['variation_cost_' . $v] );
  274. }
  275. $product->setVariations( $variations_new );
  276. // attributes
  277. $sql = "SELECT ID FROM store_product_filter_attribute_types WHERE productContainedAttribute=0";
  278. $this->registry->getObject('db')->executeQuery( $sql );
  279. $attributes = array();
  280. while( $r = $this->registry->getObject('db')->getRows() )
  281. {
  282. $attributes[ $r['ID'] ] = intval( $_POST['attribute_' . $r['ID'] ] );
  283. }
  284. $product->setAttributes( $attributes );
  285. // categories
  286. $categories = ( isset( $_POST['cats'] ) && is_array( $_POST['cats'] ) ) ? $_POST['cats'] : array();
  287. $product->setCategories( $categories );
  288. // shipping costs
  289. $sql = "SELECT ID FROM store_shipping_methods";
  290. $this->registry->getObject('db')->executeQuery( $sql );
  291. $shipping = array();
  292. while( $r = $this->registry->getObject('db')->getRows() )
  293. {
  294. $shipping[ $r['ID'] ] = $this->registry->getObject('db')->sanitizeData( $_POST['shipping_' . $r['ID'] ] );
  295. }
  296. $product->setShippingCosts( $shipping );
  297. // validity
  298. if( isset( $_POST['page_valid_from'] ) && $_POST['page_valid_from'] != '' )
  299. {
  300. $date = ( isset( $_POST['page_valid_from'] ) && $_POST['page_valid_from'] != '' ) ? $this->registry->getObject('db')->sanitizeData( $_POST['page_valid_from'] ) : date('Y-m-d');
  301. $time = ( isset( $_POST['page_valid_from_time'] ) ) ? $this->registry->getObject('db')->sanitizeData( $_POST['page_valid_from_time'] ) : '';
  302. $product->setAndProcessValidFrom( $date, $time );
  303. }
  304. if( isset( $_POST['page_valid_to'] ) && $_POST['page_valid_to'] != '' )
  305. {
  306. $date = ( isset( $_POST['page_valid_to'] ) && $_POST['page_valid_to'] != '' ) ? $this->registry->getObject('db')->sanitizeData( $_POST['page_valid_to'] ) : date('Y-m-d');
  307. $time = ( isset( $_POST['page_valid_to_time'] ) ) ? $this->registry->getObject('db')->sanitizeData( $_POST['page_valid_to_time'] ) : '';
  308. $product->setAndProcessValidTo( $date, $time );
  309. }
  310. $product->save();
  311. $this->registry->redirectUser( array('store', 'products'), 'Product saved','The product has been updated and saved in the CMS', true);
  312. //$this->registry->getObject('template')->getPage()->removeTag('productcats');
  313. //$this->registry->redirectUser( array('store'), 'Category created','The category has been created and saved in the CMS', true);
  314. }
  315. else
  316. {
  317. $data = $product->getData();
  318. //echo '<pre>' . print_r( $data, true ) . '</pre>';
  319. $data['validTo'] = ( $data['validToDate'] == '00/00/0000' ) ? '' : $data['validToDate'] ;
  320. $data['validFrom'] = $data['validFromDate'];
  321. $this->registry->getObject('template')->dataToTags( $data, 'pd_' );
  322. // product variations
  323. // variation groupings
  324. $sql = "SELECT ID as attribute_id, name as attribute_name FROM store_product_variations";
  325. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  326. $this->registry->getObject('template')->getPage()->addTag( 'variations', array( 'SQL', $cache ) );
  327. $sql = "SELECT ID, name as attribute_value_name, attribute FROM store_product_variation_values ORDER BY attribute, `order`";
  328. $this->registry->getObject('db')->executeQuery( $sql );
  329. $attributeValues = array();
  330. while( $row = $this->registry->getObject('db')->getRows() )
  331. {
  332. $attributeValues[ $row['attribute'] ][] = $row;
  333. }
  334. // variation values
  335. foreach( $attributeValues as $attribute => $data )
  336. {
  337. $cache = $this->registry->getObject('db')->cacheData( $data );
  338. $this->registry->getObject('template')->getPage()->addTag( 'variation_values_' . $attribute , array( 'DATA', $cache ) );
  339. }
  340. // selected variations
  341. $p = $product->getVariations();
  342. if( ! empty( $p ) )
  343. {
  344. $vs = $product->getVariations();
  345. foreach( $vs as $v => $c )
  346. {
  347. $this->registry->getObject('template')->getPage()->addPPTag( $v.'_v', "checked='checked'");
  348. $this->registry->getObject('template')->getPage()->addPPTag( 'form_' . $v.'_v_c', $c );
  349. }
  350. }
  351. // product attributes
  352. $sql = "SELECT ID as attribute_id, name as attribute_name FROM store_product_filter_attribute_types WHERE productContainedAttribute=0";
  353. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  354. $this->registry->getObject('template')->getPage()->addTag( 'attributes', array( 'SQL', $cache ) );
  355. $sql = "SELECT ID, name as attribute_value_name, attribute FROM store_product_filter_attribute_values ORDER BY attribute, `order`";
  356. $this->registry->getObject('db')->executeQuery( $sql );
  357. $attributeValues = array();
  358. while( $row = $this->registry->getObject('db')->getRows() )
  359. {
  360. $attributeValues[ $row['attribute'] ][] = $row;
  361. }
  362. // attribute values
  363. foreach( $attributeValues as $attribute => $data )
  364. {
  365. //echo '<pre>' . print_r( $data, true ) . '</pre>';
  366. $cache = $this->registry->getObject('db')->cacheData( $data );
  367. $this->registry->getObject('template')->getPage()->addTag( 'attribute_values_' . $attribute , array( 'DATA', $cache ) );
  368. }
  369. // selected attributes
  370. $a = $product->getAttributes();
  371. if( ! empty( $a ) )
  372. {
  373. $a = $product->getAttributes();
  374. foreach( $a as $atype => $avalue )
  375. {
  376. $this->registry->getObject('template')->getPage()->addPPTag( $avalue.'_sel', "selected='selected'");
  377. }
  378. }
  379. // categories
  380. $sql = "SELECT v.name as category_name, c.ID as category_id FROM content c, content_types t, content_versions v WHERE c.type=t.ID and t.reference='product-cat' AND v.ID=c.current_revision";
  381. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  382. $this->registry->getObject('template')->getPage()->addTag( 'categories', array( 'SQL', $cache ) );
  383. // SELECTED CATEGORIES
  384. $cats = $product->getCategories();
  385. if( ! empty($cats) )
  386. {
  387. foreach( $cats as $cat )
  388. {
  389. $this->registry->getObject('template')->getPage()->addPPTag( $cat.'_cat', "checked='checked'");
  390. }
  391. }
  392. // shipping methods:
  393. $sql = "SELECT ID as method_id, name as method_name, default_cost as method_cost FROM store_shipping_methods";
  394. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  395. $this->registry->getObject('template')->getPage()->addTag( 'shipping_methods', array( 'SQL', $cache ) );
  396. $sc = $product->getShippingCosts();
  397. if( ! empty($sc) )
  398. {
  399. foreach( $sc as $m => $c )
  400. {
  401. $this->registry->getObject('template')->getPage()->addPPTag( $m.'_sc', $c);
  402. }
  403. }
  404. // featured
  405. if( $product->isFeatured() )
  406. {
  407. $this->registry->getObject('template')->getPage()->addTag( 'featured', "checked='checked'");
  408. }
  409. else
  410. {
  411. $this->registry->getObject('template')->getPage()->addTag('featured', '');
  412. }
  413. if( $product->receivesFiles() )
  414. {
  415. $this->registry->getObject('template')->getPage()->addTag( 'recfiles', "checked='checked'");
  416. }
  417. else
  418. {
  419. $this->registry->getObject('template')->getPage()->addTag('recfiles', '');
  420. }
  421. // template
  422. $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'store/products/edit.tpl.php', 'footer.tpl.php');
  423. }
  424. }
  425. private function createProduct()
  426. {
  427. //$this->buildSiteStructure();
  428. if( isset( $_POST['new_product'] ) )
  429. {
  430. require_once( FRAMEWORK_PATH . 'models/content.php');
  431. require_once( FRAMEWORK_PATH . 'models/store/product.php');
  432. $product = new Product( $this->registry, "", 0);
  433. $product->setName( $this->registry->getObject('db')->sanitizeData( $_POST['page_name'] ) );
  434. $product->setTitle( $this->registry->getObject('db')->sanitizeData( $_POST['page_metatitle'] ) );
  435. $product->setHeading( $this->registry->getObject('db')->sanitizeData( $_POST['page_heading'] ) );
  436. $product->setContent( $this->registry->getObject('db')->sanitizeData( $_POST['page_content'] ) );
  437. $endpath = preg_replace('/[^a-zA-Z0-9\s]/', '', $_POST['page_name'] );
  438. $endpath = str_replace( ' ', '-', $endpath );
  439. $endpath = strtolower( $endpath );
  440. $path = $endpath;
  441. $path = $this->registry->getObject('db')->sanitizeData( $path );
  442. //$cat->setTemplate( $this->registry->getObject('db')->sanitizeData( $_POST['page_template'] ) );
  443. $product->setPath( $path );
  444. $product->setActive(1);
  445. $product->setSecure(0);
  446. $product->generateType();
  447. $product->setAuthor( $this->registry->getObject('authenticate')->getUserID() );
  448. $product->setMetadescription( $this->registry->getObject('db')->sanitizeData( $_POST['page_metadescription'] ) );
  449. $product->setMetakeywords( $this->registry->getObject('db')->sanitizeData( $_POST['page_metakeywords'] ) );
  450. // process image upload
  451. if( isset( $_FILES['imageupload'] ) )
  452. {
  453. include( FRAMEWORK_PATH . 'lib/images/imagemanager.class.php');
  454. $manager = new Imagemanager();
  455. if ( $manager->loadFromPost( 'imageupload', $this->registry->getSetting('uploads_path') . 'store/products/original/' ))
  456. {
  457. $manager->resizeScaleHeight( $this->registry->getSetting('image.large_width') );
  458. $manager->save( $this->registry->getSetting('uploads_path') . 'store/products/large/' . $manager->getName() );
  459. $manager->resizeScaleHeight( $this->registry->getSetting('image.thumb_width') );
  460. $manager->save( $this->registry->getSetting('uploads_path') . 'store/products/small/' . $manager->getName() );
  461. $product->setImage( $this->registry->getObject('db')->sanitizeData( $manager->getName() ) );
  462. }
  463. else
  464. {
  465. $product->setImage('default.png');
  466. }
  467. }
  468. $product->setPrice( $this->registry->getObject('db')->sanitizeData( $_POST['price'] ) );
  469. $product->setWeight( $this->registry->getObject('db')->sanitizeData( $_POST['weight'] ) );
  470. $product->setSKU( $this->registry->getObject('db')->sanitizeData( $_POST['sku'] ) );
  471. $product->setStock( $this->registry->getObject('db')->sanitizeData( $_POST['stock'] ) );
  472. if( isset( $_POST['featured'] ) && $_POST['featured'] == 'yes' )
  473. {
  474. $product->setFeatured( 1 );
  475. }
  476. else
  477. {
  478. $product->setFeatured( 0 );
  479. }
  480. if( isset( $_POST['receives_files'] ) && $_POST['receives_files'] == 'yes' )
  481. {
  482. $product->setAllowUploads( 1 );
  483. }
  484. else
  485. {
  486. $product->setAllowUploads( 0 );
  487. }
  488. // variations
  489. $variations = ( isset( $_POST['variations'] ) && is_array( $_POST['variations'] ) ) ? $_POST['variations'] : array();
  490. $variations_new = array();
  491. foreach( $variations as $v )
  492. {
  493. $variations_new[ $v ] = $this->registry->getObject('db')->sanitizeData( $_POST['variation_cost_' . $v] );
  494. }
  495. $product->setVariations( $variations_new );
  496. // attributes
  497. $sql = "SELECT ID FROM store_product_filter_attribute_types WHERE productContainedAttribute=0";
  498. $this->registry->getObject('db')->executeQuery( $sql );
  499. $attributes = array();
  500. while( $r = $this->registry->getObject('db')->getRows() )
  501. {
  502. $attributes[ $r['ID'] ] = intval( $_POST['attribute_' . $r['ID'] ] );
  503. }
  504. $product->setAttributes( $attributes );
  505. // categories
  506. $categories = ( isset( $_POST['cats'] ) && is_array( $_POST['cats'] ) ) ? $_POST['cats'] : array();
  507. $product->setCategories( $categories );
  508. // shipping costs
  509. $sql = "SELECT ID FROM store_shipping_methods";
  510. $this->registry->getObject('db')->executeQuery( $sql );
  511. $shipping = array();
  512. while( $r = $this->registry->getObject('db')->getRows() )
  513. {
  514. $shipping[ $r['ID'] ] = $this->registry->getObject('db')->sanitizeData( $_POST['shipping_' . $r['ID'] ] );
  515. }
  516. $product->setShippingCosts( $shipping );
  517. $product->overideMicrosite();
  518. // validity
  519. if( isset( $_POST['page_valid_from'] ) && $_POST['page_valid_from'] != '' )
  520. {
  521. $date = ( isset( $_POST['page_valid_from'] ) && $_POST['page_valid_from'] != '' ) ? $this->registry->getObject('db')->sanitizeData( $_POST['page_valid_from'] ) : date('Y-m-d');
  522. $time = ( isset( $_POST['page_valid_from_time'] ) ) ? $this->registry->getObject('db')->sanitizeData( $_POST['page_valid_from_time'] ) : '';
  523. $product->setAndProcessValidFrom( $date, $time );
  524. }
  525. if( isset( $_POST['page_valid_to'] ) && $_POST['page_valid_to'] != '' )
  526. {
  527. $date = ( isset( $_POST['page_valid_to'] ) && $_POST['page_valid_to'] != '' ) ? $this->registry->getObject('db')->sanitizeData( $_POST['page_valid_to'] ) : date('Y-m-d');
  528. $time = ( isset( $_POST['page_valid_to_time'] ) ) ? $this->registry->getObject('db')->sanitizeData( $_POST['page_valid_to_time'] ) : '';
  529. $product->setAndProcessValidTo( $date, $time );
  530. }
  531. $product->save();
  532. $this->registry->redirectUser( array('store', 'products'), 'Product created','The product has been created and saved in the CMS', true);
  533. //$this->registry->getObject('template')->getPage()->removeTag('productcats');
  534. //$this->registry->redirectUser( array('store'), 'Category created','The category has been created and saved in the CMS', true);
  535. }
  536. else
  537. {
  538. $this->registry->getObject('template')->getPage()->addTag( 'default_date', date('m/d/Y') );
  539. // product variations
  540. // variation groupings
  541. $sql = "SELECT ID as attribute_id, name as attribute_name FROM store_product_variations";
  542. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  543. $this->registry->getObject('template')->getPage()->addTag( 'variations', array( 'SQL', $cache ) );
  544. $sql = "SELECT ID, name as attribute_value_name, attribute FROM store_product_variation_values ORDER BY attribute, `order`";
  545. $this->registry->getObject('db')->executeQuery( $sql );
  546. $attributeValues = array();
  547. while( $row = $this->registry->getObject('db')->getRows() )
  548. {
  549. $attributeValues[ $row['attribute'] ][] = $row;
  550. }
  551. // variation values
  552. foreach( $attributeValues as $attribute => $data )
  553. {
  554. $cache = $this->registry->getObject('db')->cacheData( $data );
  555. $this->registry->getObject('template')->getPage()->addTag( 'variation_values_' . $attribute , array( 'DATA', $cache ) );
  556. }
  557. // product attributes
  558. $sql = "SELECT ID as attribute_id, name as attribute_name FROM store_product_filter_attribute_types WHERE productContainedAttribute=0";
  559. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  560. $this->registry->getObject('template')->getPage()->addTag( 'attributes', array( 'SQL', $cache ) );
  561. $sql = "SELECT ID, name as attribute_value_name, attribute FROM store_product_filter_attribute_values ORDER BY attribute, `order`";
  562. $this->registry->getObject('db')->executeQuery( $sql );
  563. $attributeValues = array();
  564. while( $row = $this->registry->getObject('db')->getRows() )
  565. {
  566. $attributeValues[ $row['attribute'] ][] = $row;
  567. }
  568. // attribute values
  569. foreach( $attributeValues as $attribute => $data )
  570. {
  571. $cache = $this->registry->getObject('db')->cacheData( $data );
  572. $this->registry->getObject('template')->getPage()->addTag( 'attribute_values_' . $attribute , array( 'DATA', $cache ) );
  573. }
  574. // categories
  575. $sql = "SELECT v.name as category_name, c.ID as category_id FROM content c, content_types t, content_versions v WHERE c.type=t.ID and t.reference='product-cat' AND v.ID=c.current_revision AND c.deleted = 0";
  576. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  577. $this->registry->getObject('template')->getPage()->addTag( 'categories', array( 'SQL', $cache ) );
  578. // shipping methods
  579. $sql = "SELECT ID as method_id, name as method_name, default_cost as method_cost FROM store_shipping_methods";
  580. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  581. $this->registry->getObject('template')->getPage()->addTag( 'shipping_methods', array( 'SQL', $cache ) );
  582. // template
  583. $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'store/products/create.tpl.php', 'footer.tpl.php');
  584. }
  585. }
  586. private function listContent()
  587. {
  588. // template
  589. $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'store/products/list.tpl.php', 'footer.tpl.php');
  590. $this->buildSiteStructure();
  591. }
  592. private function toggleCategoryActive( $cat, $newvalue )
  593. {
  594. $cat = intval($cat);
  595. $this->toggleActive( $cat );
  596. header('Location: ' . $this->registry->buildURL(array('store', 'products'), '', true ) );
  597. exit();
  598. }
  599. private function toggleCategorySecurity( $cat, $newvalue )
  600. {
  601. $cat = intval($cat);
  602. $this->toggleSecurity( $cat );
  603. header('Location: ' . $this->registry->buildURL(array('store', 'products'), '', true ) );
  604. exit();
  605. //$this->listContent();
  606. }
  607. private function buildSiteStructure( $category=0)
  608. {
  609. parent::setType('product');
  610. $typeSQL = "SELECT ID FROM content_types WHERE reference='product'";
  611. $this->registry->getObject('db')->executeQuery( $typeSQL );
  612. $data = $this->registry->getObject('db')->getRows();
  613. $extrafields = array( 'p.price', 'p.sku', 'p.stock', 'p.image' );
  614. $extratables = array( 'content_versions_store_products p');
  615. $extraconditions = array( 'p.version_id=f.current_revision');
  616. if( $category != 0 )
  617. {
  618. $extratables[] = 'store_product_category_associations a';
  619. $extraconditions[] = 'a.category_id=' . $category;
  620. $extraconditions[] = 'a.product_id=f.ID';
  621. }
  622. $this->buildStructure( $extrafields, $extratables, $extraconditions, array(), array(), $microsite = false );
  623. }
  624. private function deleteCategory( $content )
  625. {
  626. $this->delete( $content );
  627. header('Location: ' . $this->registry->buildURL(array('store'), '', true ) );
  628. exit();
  629. }
  630. }
  631. ?>