ProductController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace Apps\Controllers;
  3. use Cygnite\Common\Input\Input;
  4. use Cygnite\FormBuilder\Form;
  5. use Cygnite\Validation\Validator;
  6. use Cygnite\Common\UrlManager\Url;
  7. use Cygnite\Foundation\Application;
  8. use Cygnite\Mvc\Controller\AbstractBaseController;
  9. use Apps\Components\Form\ProductForm;
  10. use Apps\Models\Product;
  11. /**
  12. * This file is generated by Cygnite Crud Generator
  13. * You may alter code to fit your need
  14. */
  15. class ProductController extends AbstractBaseController
  16. {
  17. /**
  18. * --------------------------------------------------------------------------
  19. * The Product Controller
  20. *--------------------------------------------------------------------------
  21. * This controller respond to uri beginning with product and also
  22. * respond to root url like "product/index"
  23. *
  24. * Your GET request of "product/index" will respond like below -
  25. *
  26. * public function indexAction()
  27. * {
  28. * echo "Cygnite : Hello ! World ";
  29. * }
  30. *
  31. */
  32. // Plain layout
  33. protected $layout = 'layout.base';
  34. /**
  35. * Your constructor.
  36. * @access public
  37. */
  38. public function __construct()
  39. {
  40. parent::__construct();
  41. }
  42. /**
  43. * Default method for your controller. Render index page into browser.
  44. * @access public
  45. * @return void
  46. */
  47. public function indexAction()
  48. {
  49. $product = array();
  50. $product = Product::all(array('orderBy' => 'id desc',
  51. /*'paginate' => array(
  52. 'limit' => Url::segment(3)
  53. )*/)
  54. );
  55. $this->render('index', array(
  56. 'records' => $product,
  57. 'links' => '', //Product::createLinks(),
  58. 'title' => 'Cygnite Framework - Crud Application'
  59. ));
  60. }
  61. /**
  62. * Set Validation rules for Form
  63. * @param $input
  64. * @return mixed
  65. */
  66. private function setValidationRules($input)
  67. {
  68. //Set Form validation rules
  69. return Validator::instance($input, function ($validate)
  70. {
  71. $validate ->addRule('product_name', 'required|min:5')
  72. ->addRule('category', 'required|min:5')
  73. ->addRule('description', 'required|min:5')
  74. ->addRule('validity', 'required|min:5')
  75. ->addRule('price', 'required|min:5')
  76. ->addRule('created_at', 'required|min:5')
  77. ->addRule('updated_at', 'required|min:5')
  78. ;
  79. return $validate;
  80. });
  81. }
  82. /**
  83. * Add a new product
  84. * @return void
  85. */
  86. public function addAction()
  87. {
  88. $validator = null;
  89. $form = new ProductForm();
  90. $form->action = 'add';
  91. $input = Input::make();
  92. //Check is form posted
  93. if ($input->hasPost('btnSubmit') == true) {
  94. $validator = $this->setValidationRules($input);
  95. //Run validation
  96. if ($validator->run()) {
  97. $product = new Product();
  98. // get post array value except the submit button
  99. $postArray = $input->except('btnSubmit')->post();
  100. $product->product_name = $postArray["product_name"];
  101. $product->category = $postArray["category"];
  102. $product->description = $postArray["description"];
  103. $product->validity = $postArray["validity"];
  104. $product->price = $postArray["price"];
  105. $product->created_at = $postArray["created_at"];
  106. $product->updated_at = $postArray["updated_at"];
  107. // Save form details
  108. if ($product->save()) {
  109. $this->setFlash('success', 'Product added successfully!')
  110. ->redirectTo('product/index/'.Url::segment(3));
  111. } else {
  112. $this->setFlash('error', 'Error occured while saving Product!')
  113. ->redirectTo('product/index/'.Url::segment(3));
  114. }
  115. } else {
  116. //validation error here
  117. $form->errors = $validator->getErrors();
  118. }
  119. $form->validation = $validator;
  120. }
  121. // We can also use same view page for create and update
  122. $this->render('create', array(
  123. 'form' => $form->buildForm()->render(),
  124. 'validation_errors' => $form->errors,
  125. 'title' => 'Add a new Product'
  126. ));
  127. }
  128. /**
  129. * Update a product
  130. *
  131. * @param $id
  132. */
  133. public function editAction($id)
  134. {
  135. $validator = null; $product = array();
  136. $product = Product::find($id);
  137. $form = new ProductForm($product, Url::segment(3));
  138. $form->action = 'edit';
  139. $input = Input::make();
  140. //Check is form posted
  141. if ($input->hasPost('btnSubmit') == true) {
  142. $validator = $this->setValidationRules($input);
  143. //Run validation
  144. if ($validator->run()) {
  145. // get post array value except the submit button
  146. $postArray = $input->except('btnSubmit')->post();
  147. $product->product_name = $postArray["product_name"];
  148. $product->category = $postArray["category"];
  149. $product->description = $postArray["description"];
  150. $product->validity = $postArray["validity"];
  151. $product->price = $postArray["price"];
  152. $product->created_at = $postArray["created_at"];
  153. $product->updated_at = $postArray["updated_at"];
  154. // Save form information
  155. if ($product->save()) {
  156. $this->setFlash('success', 'Product updated successfully!')
  157. ->redirectTo('product/index/'.Url::segment(3));
  158. } else {
  159. $this->setFlash('error', 'Error occured while saving Product!')
  160. ->redirectTo('product/index/'.Url::segment(3));
  161. }
  162. } else {
  163. //validation error here
  164. $form->errors = $validator->getErrors();
  165. }
  166. $form->validation = $validator;
  167. }
  168. $this->render('update', array(
  169. 'form' => $form->buildForm()->render(),
  170. 'validation_errors' => $form->errors,
  171. 'title' => 'Update the Product'
  172. ));
  173. }
  174. /**
  175. * Display product details
  176. * @param type $id
  177. */
  178. public function showAction($id)
  179. {
  180. $product = Product::find($id);
  181. $this->render('show', array(
  182. 'record' => $product,
  183. 'title' => 'Show the Product'
  184. ));
  185. }
  186. /**
  187. * Delete product using id
  188. *
  189. * @param type $id
  190. */
  191. public function deleteAction($id)
  192. {
  193. $product = new Product();
  194. if ($product->trash($id) == true) {
  195. $this->setFlash('success', 'Product Deleted Successfully!')
  196. ->redirectTo('product/');
  197. } else {
  198. $this->setFlash('error', 'Error Occured while deleting Product!')
  199. ->redirectTo('product/');
  200. }
  201. }
  202. }//End of your Product controller