actions.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * kataoProduct actions.
  4. *
  5. * @package www.katao.fr
  6. * @subpackage kataoProduct
  7. * @author Your name here
  8. * @version SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
  9. */
  10. class kataoProductActions extends wpActions {
  11. public function executeIndex() {
  12. $criteria = new Criteria();
  13. $criteria->addJoin(KataoProductPeer::KATAO_SUPPLIER_ID, KataoSupplierPeer::ID);
  14. $criteria->addJoin(KataoProductPeer::KATAO_PRODUCT_FAMILY_ID, KataoProductFamilyPeer::ID);
  15. $criteria->addJoin(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
  16. $sf_user = $this->getUser();
  17. if ($sf_user->isJustSupplier()) {
  18. $criteria->add(KataoProductPeer::KATAO_SUPPLIER_ID, $sf_user->getInstance()->getKataoSupplierId());
  19. } elseif ($sf_user->isJustReferer()) {
  20. $criteria->add(KataoSupplierPeer::REFERER_ID, $sf_user->getInstance()->getKataoMemberId());
  21. }
  22. $sort_method = $this->getCriteriaSortMethod();
  23. switch ($this->sort = $this->getRequestParameter('sort', 'name')) {
  24. case 'price':
  25. $criteria->$sort_method(KataoProductPeer::UNIT_PRICE_EURO);
  26. break;
  27. case 'category':
  28. $criteria->$sort_method(KataoProductCategoryPeer::NAME);
  29. break;
  30. case 'family':
  31. $criteria->$sort_method(KataoProductFamilyPeer::NAME);
  32. break;
  33. case 'supplier':
  34. $criteria->$sort_method(KataoSupplierPeer::NAME);
  35. break;
  36. default:
  37. $criteria->$sort_method(KataoProductPeer::NAME);
  38. }
  39. if ('' != $this->filter_supplier = $this->getUser()->getAttribute('katao_product_filter_supplier')) {
  40. $criteria->add(KataoProductPeer::KATAO_SUPPLIER_ID, $this->filter_supplier);
  41. }
  42. if ('' != $this->filter_product_category = $this->getUser()->getAttribute('katao_product_filter_product_category')) {
  43. $criteria->add(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, $this->filter_product_category);
  44. }
  45. if ('' != $this->filter_product_family = $this->getUser()->getAttribute('katao_product_filter_product_family')) {
  46. $criteria->add(KataoProductPeer::KATAO_PRODUCT_FAMILY_ID, $this->filter_product_family);
  47. }
  48. if ('' != $this->filter_product_archived = $this->getUser()->getAttribute('katao_product_filter_archived')) {
  49. $criteria->add(KataoProductPeer::IS_ARCHIVED, (CONST_YES == $this->filter_product_archived)?true:false);
  50. }
  51. if ('' != $this->search = $this->getUser()->getAttribute('katao_product_search')) {
  52. $search_like = sprintf('%%%s%%', $this->search);
  53. $criterion = $criteria->getNewCriterion(KataoProductPeer::REFERENCE, $search_like, Criteria::LIKE);
  54. $criterion->addOr($criteria->getNewCriterion(KataoProductPeer::NAME, $search_like, Criteria::LIKE));
  55. $criterion->addOr($criteria->getNewCriterion(KataoProductPeer::DESCRIPTION, $search_like, Criteria::LIKE));
  56. $criteria->addAnd($criterion);
  57. }
  58. $pager = new sfPropelPager('KataoProduct', sfConfig::get('app_pager'));
  59. $pager->setPeerMethod('doSelectJoinAll');
  60. $pager->setCriteria($criteria);
  61. $pager->setPage($this->getRequestParameter('page', 1));
  62. $pager->init();
  63. $this->pager = $pager;
  64. $this->katao_productList = $pager->getResults();
  65. }
  66. public function executeFilter($request) {
  67. $this->getUser()->setAttribute('katao_product_filter_supplier', $request->getParameter('filter_supplier'));
  68. $this->getUser()->setAttribute('katao_product_filter_product_category', $request->getParameter('filter_product_category'));
  69. $this->getUser()->setAttribute('katao_product_filter_product_family', $request->getParameter('filter_product_family'));
  70. $this->getUser()->setAttribute('katao_product_filter_archived', $request->getParameter('filter_product_archived'));
  71. return $this->redirect('kataoProduct/index');
  72. }
  73. public function executeFilterReset($request) {
  74. $this->getUser()->setAttribute('katao_product_filter_supplier', '');
  75. $this->getUser()->setAttribute('katao_product_filter_product_category', '');
  76. $this->getUser()->setAttribute('katao_product_filter_product_family', '');
  77. $this->getUser()->setAttribute('katao_product_filter_archived', '');
  78. return $this->redirect('kataoProduct/index');
  79. }
  80. public function executeSearch($request) {
  81. $this->getUser()->setAttribute('katao_product_search', $request->getParameter('search'));
  82. return $this->redirect('kataoProduct/index');
  83. }
  84. public function executeSearchReset($request) {
  85. $this->getUser()->setAttribute('katao_product_search', '');
  86. return $this->redirect('kataoProduct/index');
  87. }
  88. public function executeShow($request) {
  89. $this->katao_product = KataoProductPeer::retrieveByPk($request->getParameter('id'));
  90. $this->redirectWithErrorUnless($this->katao_product, 'Produit #' . $request->getParameter('id') . ' inconnu.');
  91. }
  92. public function executeCreate() {
  93. $this->form = new KataoProductForm();
  94. $this->setTemplate('edit');
  95. }
  96. public function executeEdit($request) {
  97. $katao_product = KataoProductPeer::retrieveByPk($request->getParameter('id'));
  98. if (!is_null($katao_product)) {
  99. $is_just_supplier = $this->getUser()->isJustSupplier();
  100. $is_just_referer = $this->getUser()->isJustReferer();
  101. $is_just_delegate = $this->getUser()->isJustDelegate();
  102. $local_node_suppliers = $is_just_delegate?KataoSupplierPeer::getAllLocalToNode($this->getUser()->getInstance()->getKataoMember()->getKataoNodeId()):array();
  103. $this->redirectWithErrorIf($is_just_supplier && $this->getUser()->getInstance()->getKataoSupplierId() != $katao_product->getKataoSupplierId());
  104. $this->redirectWithErrorIf($is_just_referer && $this->getUser()->getInstance()->getKataoMemberId() != $katao_product->getKataoSupplier()->getRefererId());
  105. $this->redirectWithErrorIf($is_just_delegate && empty($local_node_suppliers[$katao_product->getKataoSupplierId()]));
  106. }
  107. $this->form = new KataoProductForm($katao_product);
  108. }
  109. public function executeUpdate($request) {
  110. $this->redirectWithErrorUnless($request->isMethod('post'));
  111. $katao_product = KataoProductPeer::retrieveByPk($request->getParameter('id'));
  112. if (!is_null($katao_product)) {
  113. $is_just_supplier = $this->getUser()->isJustSupplier();
  114. $is_just_referer = $this->getUser()->isJustReferer();
  115. $is_just_delegate = $this->getUser()->isJustDelegate();
  116. $local_node_suppliers = $is_just_delegate?KataoSupplierPeer::getAllLocalToNode($this->getUser()->getInstance()->getKataoMember()->getKataoNodeId()):array();
  117. $this->redirectWithErrorIf($is_just_supplier && $this->getUser()->getInstance()->getKataoSupplierId() != $katao_product->getKataoSupplierId());
  118. $this->redirectWithErrorIf($is_just_referer && $this->getUser()->getInstance()->getKataoMemberId() != $katao_product->getKataoSupplier()->getRefererId());
  119. $this->redirectWithErrorIf($is_just_delegate && empty($local_node_suppliers[$katao_product->getKataoSupplierId()]));
  120. }
  121. $this->form = new KataoProductForm($katao_product);
  122. $this->form->bind($request->getParameter('katao_product'), $request->getFiles('katao_product'));
  123. if ($this->form->isValid()) {
  124. $katao_product = $this->form->save();
  125. wpFlashMessages::addConfirmation('Produit "' . $katao_product->getName() . '" sauvé avec succès.');
  126. $this->redirect('kataoProduct/index');
  127. }
  128. $this->setTemplate('edit');
  129. }
  130. public function executeEditPresentation($request) {
  131. $this->katao_product = KataoProductPeer::retrieveByPk($request->getParameter('id'));
  132. $this->redirectWithErrorUnless($this->katao_product, 'Produit #' . $request->getParameter('id') . ' inconnu.');
  133. $is_just_supplier = $this->getUser()->isJustSupplier();
  134. $is_just_referer = $this->getUser()->isJustReferer();
  135. $is_just_delegate = $this->getUser()->isJustDelegate();
  136. $local_node_suppliers = $is_just_delegate?KataoSupplierPeer::getAllLocalToNode($this->getUser()->getInstance()->getKataoMember()->getKataoNodeId()):array();
  137. $this->redirectWithErrorIf($is_just_supplier && $this->getUser()->getInstance()->getKataoSupplierId() != $this->katao_product->getKataoSupplierId());
  138. $this->redirectWithErrorIf($is_just_referer && $this->getUser()->getInstance()->getKataoMemberId() != $this->katao_product->getKataoSupplier()->getRefererId());
  139. $this->redirectWithErrorIf($is_just_delegate && empty($local_node_suppliers[$this->katao_product->getKataoSupplierId()]));
  140. if ($request->isMethod('post')) {
  141. $this->katao_product->setDescription($request->getParameter('description'));
  142. $this->katao_product->save();
  143. wpFlashMessages::addConfirmation('Présentation du produit "' . $this->katao_product->getName() . '" sauvée avec succès.');
  144. $this->redirect('kataoProduct/index');
  145. }
  146. }
  147. public function executeDelete($request) {
  148. $this->redirectWithErrorUnless($katao_product = KataoProductPeer::retrieveByPk($request->getParameter('id')));
  149. $is_just_supplier = $this->getUser()->isJustSupplier();
  150. $is_just_referer = $this->getUser()->isJustReferer();
  151. $is_just_delegate = $this->getUser()->isJustDelegate();
  152. $local_node_suppliers = $is_just_delegate?KataoSupplierPeer::getAllLocalToNode($this->getUser()->getInstance()->getKataoMember()->getKataoNodeId()):array();
  153. $this->redirectWithErrorIf($is_just_supplier && $this->getUser()->getInstance()->getKataoSupplierId() != $katao_product->getKataoSupplierId());
  154. $this->redirectWithErrorIf($is_just_referer && $this->getUser()->getInstance()->getKataoMemberId() != $katao_product->getKataoSupplier()->getRefererId());
  155. $this->redirectWithErrorIf($is_just_delegate && empty($local_node_suppliers[$katao_product->getKataoSupplierId()]));
  156. $this->redirectWithErrorIf(!$katao_product->isDeletable());
  157. $katao_product->delete();
  158. wpFlashMessages::addConfirmation('Produit "' . $katao_product->getName() . '" supprimé avec succès.');
  159. $this->redirect('kataoProduct/index');
  160. }
  161. public function executeDeleteMultiple($request) {
  162. if (!wpPersistenceManager::isEmpty('katao_product')) {
  163. foreach (KataoProductPeer::retrieveByPKs(array_keys(wpPersistenceManager::getSelectedItems('katao_product'))) as/*(KataoProduct)*/ $katao_product) {
  164. if ($katao_product->isDeletable()) {
  165. $katao_product->delete();
  166. }
  167. }
  168. wpPersistenceManager::cleanSelection('katao_product');
  169. wpFlashMessages::addConfirmation('Les produits sélectionnés ont été supprimés avec succès.');
  170. } else {
  171. wpFlashMessages::addWarning('Merci de sélectionner au moins un produit.');
  172. }
  173. $this->redirect('kataoProduct/index');
  174. }
  175. public function executeArchiveMultiple($request) {
  176. if (!wpPersistenceManager::isEmpty('katao_product')) {
  177. $done = 0;
  178. foreach (KataoProductPeer::retrieveByPKs(array_keys(wpPersistenceManager::getSelectedItems('katao_product'))) as/*(KataoProduct)*/ $katao_product) {
  179. if (!$katao_product->getIsArchived()) {
  180. $katao_product->setIsArchived(true);
  181. $katao_product->save();
  182. $done++;
  183. }
  184. }
  185. if (0 < $done) {
  186. wpFlashMessages::addConfirmation($done . ' produits sélectionnés sur ' . wpPersistenceManager::getSelectedItemCount('katao_product') . ' ont été archivés avec succès.');
  187. } else {
  188. wpFlashMessages::addWarning('Aucun produit sélectionné n\'a été archivé.');
  189. }
  190. wpPersistenceManager::cleanSelection('katao_product');
  191. } else {
  192. wpFlashMessages::addWarning('Merci de sélectionner au moins un produit.');
  193. }
  194. $this->redirect('kataoProduct/index');
  195. }
  196. public function executeAdjustPrices($request) {
  197. $count = 0;
  198. foreach (KataoProductPeer::getAll() as/*(KataoProduct)*/ $katao_product) {
  199. $price_ht_1 = round($katao_product->getUnitPriceEuro(), 3);
  200. $price_ht_2 = round($katao_product->getUnitPriceEuroCalculated(), 3);
  201. if ($price_ht_1 != $price_ht_2) {
  202. $katao_product->setUnitPriceEuro($price_ht_2);
  203. $katao_product->save();
  204. $count++;
  205. }
  206. }
  207. if (0 < $count) {
  208. wpFlashMessages::addConfirmation('Les prix HT de ces ' . $count . ' produits ont été ajustés automatiquement avec succès.');
  209. } else {
  210. wpFlashMessages::addWarning('Aucun prix HT n\'a été ajusté automatiquement.');
  211. }
  212. $this->redirect('@homepage');
  213. }
  214. }