Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. // ...
  2. public function addAction()
  3. {
  4.  
  5. $categories = $this->getCategoryTable()->fetchAll();
  6. $category_options = array();
  7. foreach ($categories as $category) {
  8. $category_options[$category->id] = $category->name;
  9. }
  10. $form = new ProductForm($category_options);
  11. $form->get('submit')->setValue('Pridať inzerát');
  12.  
  13. $request = $this->getRequest();
  14.  
  15. if ($request->isPost()) {
  16.  
  17. $post = array_merge_recursive(
  18. $request->getPost()->toArray(),
  19. $request->getFiles()->toArray()
  20. );
  21. $form->setData($post);
  22. $product = new Product();
  23. $form->setInputFilter($product->getInputFilter());
  24.  
  25. if ($form->isValid())
  26. {
  27. $data = $form->getData();
  28. echo var_dump($data);
  29. $product->exchangeArray($form->getData());
  30. $this->getProductTable()->saveProduct($product);
  31. //return $this->redirect()->toRoute('product');
  32. }
  33. }
  34. return array(
  35. 'form' => $form,
  36. );
  37. }
  38.  
  39. class ProductForm extends Form
  40. {
  41. public function __construct($category_options)
  42. {
  43.  
  44. parent::__construct('product');
  45.  
  46. $this->add(array(
  47. 'name' => 'id',
  48. 'type' => 'Hidden',
  49. ));
  50.  
  51. $this->add(array(
  52. 'name' => 'title',
  53. 'type' => 'text',
  54. 'options'=> array(
  55. 'label' => 'Názov inzerátu:',
  56. ),
  57. ));
  58.  
  59. $this->add(array(
  60. 'name' => 'description',
  61. 'type' => 'text',
  62. 'options' => array(
  63. 'label' => 'Popis inzererátu:',
  64. ),
  65. ));
  66.  
  67. $this->add(array(
  68. 'name' => 'category_id',
  69. 'type' => 'ZendFormElementSelect',
  70. 'options' => array(
  71. 'label' => 'Kategória inzerátu:',
  72. 'value_options' => $category_options
  73. ),
  74. ));
  75.  
  76. $this->add(array(
  77. 'name' => 'phone',
  78. 'type' => 'text',
  79. 'options' => array(
  80. 'label' => 'Telefónne číslo:',
  81. ),
  82. ));
  83.  
  84. $this->add(array(
  85. 'name' => 'email',
  86. 'type' => 'text',
  87. 'options' => array(
  88. 'label' => 'Email:',
  89. ),
  90. ));
  91.  
  92. $this->add(array(
  93. 'name' => 'price',
  94. 'type' => 'text',
  95. 'options' => array(
  96. 'label' => 'Cena:',
  97. ),
  98. ));
  99.  
  100. $this->add(array(
  101. 'name' => 'location',
  102. 'type' => 'text',
  103. 'options' => array(
  104. 'label' => 'Mesto:',
  105. ),
  106. ));
  107.  
  108. $this->add(array(
  109. 'name' => 'shipping',
  110. 'type' => 'text',
  111. 'options' => array(
  112. 'label' => 'Odosiela do:',
  113. ),
  114. ));
  115.  
  116. $this->add(array(
  117. 'name' => 'top',
  118. 'type' => 'ZendFormElementSelect',
  119. 'options' => array(
  120. 'label' =>'Topovať inzerát?',
  121. 'value_options' => array(
  122. 'nie' => 'Nie',
  123. 'ano' => 'Áno'
  124. ),
  125. ),
  126. ));
  127.  
  128. $this->addPhotos();
  129. $this->addInputFilter();
  130.  
  131. $this->add(array(
  132. 'name' => 'submit',
  133. 'type' => 'Submit',
  134. 'attributes' => array(
  135. 'value' => 'Choď',
  136. 'id' => 'submitbutton',
  137. 'class' => 'btn btn-primary'
  138. ),
  139. ));
  140. }
  141.  
  142. public function addPhotos()
  143. {
  144. // File Input
  145. $file = new ElementFile('image-file');
  146. $file->setLabel('Foto pre inzerát')
  147. ->setAttribute('id', 'image-file')
  148. ->setAttribute('name', 'image-file')
  149. ->setAttribute('multiple', true);
  150. $this->add($file);
  151. }
  152.  
  153. public function addInputFilter()
  154. {
  155. $inputFilter = new InputFilterInputFilter();
  156.  
  157. // File Input
  158. $fileInput = new InputFilterFileInput('image-file');
  159. $fileInput->setRequired(true);
  160.  
  161. $fileInput->getValidatorChain()
  162. ->attachByName('filesize', array('max' => 204800))
  163. ->attachByName('filemimetype', array('mimeType' => 'image/jpeg, image/x-png'))
  164. ->attachByName('fileimagesize', array('maxWidth' => 100, 'maxHeight' => 100));
  165.  
  166. $fileInput->getFilterChain()->attachByName(
  167. 'filerenameupload',
  168. array(
  169. 'target' => './public/img/product.jpeg',
  170. 'randomize' => true,
  171. )
  172. );
  173. $inputFilter->add($fileInput);
  174.  
  175. $this->setInputFilter($inputFilter);
  176. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement