PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/demos/e-shop/Application/Admin/Controller/ProductsController.php

http://smart-framework.googlecode.com/
PHP | 271 lines | 243 code | 19 blank | 9 comment | 27 complexity | 4a9e99cd7e9a813211d0298c617f72ad MD5 | raw file
  1. <?php
  2. require_once 'SmartL/Zend/Controller/Action.php';
  3. require_once 'SmartL/Zend/Form.php';
  4. require_once 'Zend/Form/Element/Text.php';
  5. require_once 'Zend/Form/Element/Textarea.php';
  6. require_once 'Zend/Form/Element/Select.php';
  7. require_once 'Zend/Form/Element/Hidden.php';
  8. require_once 'Zend/Form/Element/File.php';
  9. require_once 'Zend/Form/Element/Submit.php';
  10. require_once 'Zend/Form/SubForm.php';
  11. require_once 'Db/Shop/Record/Product.php';
  12. require_once 'Db/Shop/Record/Category.php';
  13. require_once 'Db/Shop/Record/BasketItem.php';
  14. require_once 'SmartL/Zend/Paginator/Adapter/Doctrine.php';
  15. require_once 'Zend/Paginator.php';
  16. class Admin_ProductsController extends SmartL_Zend_Controller_Action
  17. {
  18. public function listAction ()
  19. {
  20. // Parametry: str??nka = 1
  21. $query = new Doctrine_Query();
  22. $query->parseDqlQuery($this->config->getAction("list")->getSelection("list")->getDql('Db_Shop_Record_Product'));
  23. $products = new Zend_Paginator( new SmartL_Zend_Paginator_Adapter_Doctrine($query, Doctrine::HYDRATE_RECORD) );
  24. $products->setCurrentPageNumber($this->_getParam('page'));
  25. $products->setItemCountPerPage(10);
  26. $this->view->products = $products;
  27. }
  28. public function addProductAction ()
  29. {
  30. //die($this->translations['form-product']->getLocale());
  31. // Parametry?: n??zev, popis, obr??zek, cena, kategorie
  32. $form = SmartL_Zend_Form::getForm(array("name" => "product" , "method" => "post", "enctype"=>"multipart/form-data"),
  33. $this->translations['form-product']);
  34. $hidden = new Zend_Form_Element_Hidden("addProduct");
  35. $submit = new Zend_Form_Element_Submit("send");
  36. $submit->setLabel("Send");
  37. $form->addElement($hidden);
  38. $form->setAction($this->_helper->url->url(array("action"=>"add-product","controller"=>"products")));
  39. $productSubForm = SmartL_Zend_Form::getForm($this->config->getAction("addProduct")->getForm("product"),
  40. null, 'Zend_Form_SubForm');
  41. $productSubForm->category->options = $this->_getCategoriesOptions();
  42. $textsSubForm = SmartL_Zend_Form::getForm( $this->config->getAction("addProduct")->getForm("product-texts"),
  43. null, 'Zend_Form_SubForm');
  44. $textsSubForm->getDecorator('Fieldset')
  45. ->setOption("legend", $this->translations['fieldsets']->translate("Texts")
  46. ." ".Zend_Registry::get('shopDefaultLanguage')->getLanguageName()
  47. ."-".Zend_Registry::get('shopDefaultLanguage')->getCultureName());
  48. $form->addSubForm($productSubForm, "product")
  49. ->addSubForm($textsSubForm, "texts")
  50. ->addElement($submit);
  51. $this->view->form = $form;
  52. if (isset($_POST['addProduct'])) {
  53. if ($form->isValid($_POST) and $form->product->image->receive()) {
  54. $product = new Db_Shop_Record_Product();
  55. //forms
  56. $values = $form->getSubForm("product")->getValues();
  57. $values = $values['product'];
  58. $texts = $form->getSubForm("texts")->getValues();
  59. $texts = $texts['texts'];
  60. //product
  61. $product->price = (float)$values['price'];
  62. $product->categoryId = $values['category'];
  63. //images
  64. if ( isset($_FILES['image']['name']) and $_FILES['image']['name'] !== "") {
  65. $this->_setProductImage($product, $_FILES['image']['tmp_name'], $_FILES['image']['name']);
  66. }
  67. //product-texts
  68. $languageId = Zend_Registry::get('shopDefaultLanguage')->getId();
  69. $product->Translation[$languageId]->name = $texts['name'];
  70. $product->Translation[$languageId]->description = $texts['description'];
  71. $product->save();
  72. $this->_helper->redirector->goto("list", "products");
  73. }
  74. }
  75. }
  76. public function editProductAction ()
  77. {
  78. // Parametry?: id produktu, n??zev, popis, obr??zek, cena, kategorie
  79. $this->view->productForm = $productForm = $this->_createEditProductForm();
  80. $this->view->textsForm = $textsForm = $this->_createEditProductTextsForm();
  81. $textsForm->setAction($this->_helper->url->url(array("action"=>"edit-product","controller"=>"products")));
  82. $productForm->setAction($this->_helper->url->url(array("action"=>"edit-product","controller"=>"products")));
  83. $this->view->languages = Zend_Registry::get("shopLanguages");
  84. $id = $this->_hasParam("id") ? $this->_getParam('id') : null;
  85. if (isset($_POST['languageId'])) {
  86. $this->_processEditProductTextsForm($textsForm);
  87. $id = $textsForm->productId->getValue();
  88. } else if (isset($_POST['productId'])) {
  89. $this->_processEditProductForm($productForm);
  90. $id = $productForm->productId->getValue();
  91. }
  92. $product = Doctrine_Manager::getInstance()->getCurrentConnection()->getTable('Db_Shop_Record_Product')->find($id);
  93. $productForm->productId->setValue($product->id);
  94. $textsForm->productId->setValue($product->id);
  95. if (isset($_POST['languageId']) or ! isset($_POST['productId'])) {
  96. $productForm->price->setValue((float)$product->price);
  97. $productForm->category->setValue($product->categoryId);
  98. }
  99. if (! isset($_POST['languageId'])) {
  100. $textsForm->languageId->setValue(Zend_Registry::get("shopDefaultLanguage")->getId());
  101. $this->_fillTextsForm($textsForm, $product, $textsForm->languageId->getValue());
  102. }
  103. $this->view->product_id = $id;
  104. }
  105. public function productTextsAjaxAction ()
  106. {
  107. if ($this->_hasParam('id') and $this->_hasParam('langId')) {
  108. $query = new Doctrine_Query();
  109. $product = $query->from('Db_Shop_Record_Product p')->select('*')->where('p.id = ?', $this->_getParam('id'))->execute()->getFirst();
  110. $languageId = $this->_getParam('langId');
  111. $this->_helper->json->direct(array("name" => $product->Translation[$languageId]->name , "description" => $product->Translation[$languageId]->description));
  112. }
  113. }
  114. public function removeProductAction ()
  115. {
  116. // Parametry: id produktu
  117. if ($this->_hasParam("id")) {
  118. $query = new Doctrine_Query();
  119. $product = Doctrine_Manager::getInstance()->getCurrentConnection()->getTable('Db_Shop_Record_Product')
  120. ->find($this->_getParam('id'));
  121. $this->_deleteProductImages($product);
  122. $query->delete()->from('Db_Shop_Record_Product p')->where('p.id = ?', $this->_getParam('id'))->execute();
  123. $this->_helper->redirector->goto("list", "products");
  124. }
  125. }
  126. private function _processEditProductForm ($form)
  127. {
  128. if ($form->isValid($_POST)) {
  129. $product = Doctrine_Manager::getInstance()->getCurrentConnection()->getTable('Db_Shop_Record_Product')
  130. ->find($form->productId->getValue());
  131. $product->price = $form->price->getValue();
  132. $product->categoryId = $form->category->getValue();
  133. if ( isset($_FILES['image']['name']) and $_FILES['image']['name'] !== "") {
  134. $filename = $form->image->getFilename();
  135. if ( !file_exists($filename) ) $filename = $_FILES['image']['tmp_name'];
  136. $this->_deleteProductImages($product);
  137. $this->_setProductImage($product, $filename, $_FILES['image']['name']);
  138. }
  139. $product->save();
  140. }
  141. }
  142. private function _processEditProductTextsForm ($form)
  143. {
  144. if ($form->isValid($_POST)) {
  145. $values = $form->getValues();
  146. $product = Doctrine_Manager::getInstance()->getCurrentConnection()->getTable('Db_Shop_Record_Product')->find($values['productId']);
  147. $product->Translation[$values['languageId']]->name = $values['name'];
  148. $product->Translation[$values['languageId']]->description = $values['description'];
  149. $product->save();
  150. }
  151. }
  152. private function _createEditProductTextsForm ()
  153. {
  154. $options = $this->config->getAction("addProduct")->getForm("product-texts");
  155. $options['elements']['languageId'] = array("type" => "hidden");
  156. $options['elements']['productId'] = array("type" => "hidden");
  157. $options['elements']['submit'] = array("type" => "submit" , "options" => array("label" => "Send"));
  158. $textsForm = SmartL_Zend_Form::getForm($options, $this->translations['form-product']);
  159. return $textsForm;
  160. }
  161. private function _createEditProductForm ()
  162. {
  163. $options = $this->config->getAction("addProduct")->getForm("product");
  164. $options['elements']['productId'] = array('type' => 'hidden');
  165. $options['elements']['submit'] = array("type" => "submit" , "options" => array("label" => "Send"));
  166. $productForm = SmartL_Zend_Form::getForm($options, $this->translations['form-product']);
  167. $productForm->category->options = $this->_getCategoriesOptions();
  168. return $productForm;
  169. }
  170. private function _fillTextsForm ($textsForm, $product, $languageId)
  171. {
  172. $textsForm->name->setValue($product->Translation[$languageId]->name);
  173. $textsForm->description->setValue($product->Translation[$languageId]->description);
  174. }
  175. private function _getCategoriesOptions ()
  176. {
  177. $query = new Doctrine_Query();
  178. $data = $query->from('Db_Shop_Record_Category c')->select('c.id')->execute();
  179. $result = array();
  180. foreach ($data as $category) {
  181. $result[$category->id] = $category->translate("name");
  182. }
  183. return $result;
  184. }
  185. private function _setProductImage($product, $tmpName, $imageName)
  186. {
  187. if ($imageName !== "" or $imageName !== null) {
  188. $ds = DIRECTORY_SEPARATOR;
  189. $dir = $this->application->getBasePath() . $ds
  190. . $this->application->getPublicDir()
  191. . $ds . "Products" . $ds;
  192. $imageName = basename($imageName);
  193. $i = "";
  194. $pathInfo = pathInfo($imageName);
  195. while( file_exists($dir . $pathInfo['filename'] . $i . '.' . $pathInfo['extension']) ) {
  196. $i++;
  197. }
  198. $imageName = $pathInfo['filename'] . $i . '.' . $pathInfo['extension'];
  199. $product->image = $imageName;
  200. move_uploaded_file($tmpName, $dir . $imageName );
  201. $thumbConfig = $this->application->getConfig()->demo->products->thumb;
  202. $this->_createImageThumbnail($dir . $imageName, $dir . "Thumbs" . $ds . $imageName,
  203. $thumbConfig->maxWidth->getValue(), $thumbConfig->maxHeight->getValue() );
  204. }
  205. }
  206. private function _createImageThumbnail($image,$thumb,$max_thumb_width, $max_thumb_height)
  207. {
  208. $info = pathinfo($image);
  209. $extension = strtolower($info['extension']);
  210. if ( $extension == "jpg" or $extension=="jpeg") {
  211. $src_img = imagecreatefromjpeg($image);
  212. }
  213. else if ( $extension == "png" ) {
  214. $src_img = imagecreatefrompng($image);
  215. }
  216. if ( !isset($src_img) or $src_img === null ) {
  217. copy($src_img, $thumb);
  218. return;
  219. }
  220. $orig_w = imagesx($src_img);
  221. $orig_h = imagesy($src_img);
  222. if ( $orig_w >= $orig_h ) {
  223. $thumb_width = $max_thumb_width;
  224. $thumb_height = $max_thumb_width * ($orig_h/$orig_w);
  225. }
  226. else {
  227. $thumb_height = $max_thumb_height;
  228. $thumb_width = $max_thumb_height * ($orig_w/$orig_h);
  229. }
  230. $dst_img = imagecreate($thumb_width,$thumb_height);
  231. imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_width,$thumb_height,imagesx($src_img),imagesy($src_img));
  232. if ( $extension == "jpg" or $extension=="jpeg") {
  233. imagejpeg($dst_img, $thumb);
  234. }
  235. else {
  236. imagepng($dst_img, $thumb);
  237. }
  238. }
  239. private function _deleteProductImages($product)
  240. {
  241. $ds = DIRECTORY_SEPARATOR;
  242. $dir = $this->application->getBasePath() . $ds
  243. . $this->application->getPublicDir()
  244. . $ds . "Products" . $ds;
  245. $files = array($dir . $product->image, $dir . "Thumbs" . $ds . $product->image);
  246. foreach ($files as $file) {
  247. if ( file_exists($file) ) {
  248. unlink($file);
  249. }
  250. }
  251. }
  252. }