Advertisement
fernandezekiel

Untitled

Nov 6th, 2012
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. <?php
  2.  
  3. class CustomerController extends Controller
  4. {
  5. /**
  6. * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  7. * using two-column layout. See 'protected/views/layouts/column2.php'.
  8. */
  9. public $layout='//layouts/column2';
  10.  
  11. /**
  12. * @return array action filters
  13. */
  14. public function filters()
  15. {
  16. return array(
  17. 'accessControl', // perform access control for CRUD operations
  18. 'postOnly + delete', // we only allow deletion via POST request
  19. );
  20. }
  21.  
  22. /**
  23. * Specifies the access control rules.
  24. * This method is used by the 'accessControl' filter.
  25. * @return array access control rules
  26. */
  27. public function accessRules()
  28. {
  29. return array(
  30. array('allow', // allow all users to perform 'index' and 'view' actions
  31. 'actions'=>array('index','view'),
  32. 'roles'=>array('Sales Agent'),
  33. ),
  34. array('allow', // allow authenticated user to perform 'create' and 'update' actions
  35. 'actions'=>array('create','update'),
  36. 'roles'=>array('Sales Agent'),
  37. ),
  38. array('allow', // allow admin user to perform 'admin' and 'delete' actions
  39. 'actions'=>array('admin','delete'),
  40. 'roles'=>array('Sales Agent'),
  41. ),
  42. array('deny', // deny all users
  43. 'users'=>array('*'),
  44. ),
  45. );
  46. }
  47.  
  48. /**
  49. * Displays a particular model.
  50. * @param integer $id the ID of the model to be displayed
  51. */
  52. public function actionView($id)
  53. {
  54. $contact = new Contact('search');
  55. $contact->unsetAttributes();
  56.  
  57. if(isset($_GET['Contact']))
  58. $contact->attributes=$_GET['Contact'];
  59. $contact->customer_id = $id;
  60. $this->render('view',array(
  61. 'model'=>$this->loadModel($id),
  62. 'contact'=>$contact,
  63. ));
  64. }
  65.  
  66. /**
  67. * Creates a new model.
  68. * If creation is successful, the browser will be redirected to the 'view' page.
  69. */
  70. public function actionCreate()
  71. {
  72.  
  73. $customer=new Customer;
  74. $customer->owner_id = Yii::app()->user->id;
  75. $address['billing'] =new Address;
  76. $address['shipping'] =new Address;
  77. // Uncomment the following line if AJAX validation is needed
  78. // $this->performAjaxValidation($model);
  79.  
  80. if(isset($_POST['Customer']))
  81. {
  82. if(isset($_POST['Address']))
  83. {
  84.  
  85. $address['billing']->attributes=$_POST['Address']['billing'];
  86. $address['shipping']->attributes=$_POST['Address']['shipping'];
  87.  
  88. if($address['billing']->save() && $address['shipping']->save())
  89. {
  90. $customer->attributes=$_POST['Customer'];
  91. $customer->billing_address_id = $address['billing']->id;
  92. $customer->shipping_address_id = $address['shipping']->id;
  93. if($customer->save())
  94. $this->redirect(array('view','id'=>$customer->id));
  95. }
  96.  
  97. }
  98.  
  99. }
  100.  
  101. $this->render('create',array(
  102. 'customer'=>$customer,
  103. 'address' => $address,
  104. ));
  105. }
  106.  
  107. /**
  108. * Updates a particular model.
  109. * If update is successful, the browser will be redirected to the 'view' page.
  110. * @param integer $id the ID of the model to be updated
  111. */
  112. public function actionUpdate($id)
  113. {
  114. $model=$this->loadModel($id);
  115.  
  116. // Uncomment the following line if AJAX validation is needed
  117. // $this->performAjaxValidation($model);
  118.  
  119. if(isset($_POST['Customer']))
  120. {
  121. $model->billingAddress->attributes = $_POST['Address']['billing'];
  122. $model->billingAddress->save();
  123. $model->shippingAddress->attributes = $_POST['Address']['shipping'];
  124. $model->shippingAddress->save();
  125. $model->attributes=$_POST['Customer'];
  126. if($model->save())
  127. $this->redirect(array('view','id'=>$model->id));
  128. }
  129.  
  130. $this->render('update',array(
  131. 'model'=>$model,
  132. ));
  133. }
  134.  
  135. /**
  136. * Deletes a particular model.
  137. * If deletion is successful, the browser will be redirected to the 'admin' page.
  138. * @param integer $id the ID of the model to be deleted
  139. */
  140. public function actionDelete($id)
  141. {
  142. $this->loadModel($id)->delete();
  143.  
  144. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  145. if(!isset($_GET['ajax']))
  146. $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
  147. }
  148.  
  149. /**
  150. * Lists all models.
  151. */
  152. public function actionIndex()
  153. {
  154.  
  155. $dataProvider=User::model()->findAll();
  156. $dataProvider=new CActiveDataProvider(Customer::model()->ownedByMe());
  157. $this->render('index',array(
  158. 'dataProvider'=>$dataProvider,
  159. ));
  160. }
  161.  
  162. /**
  163. * Manages all models.
  164. */
  165. public function actionAdmin()
  166. {
  167. $model=new Customer('search');
  168. $model->unsetAttributes(); // clear any default values
  169. if(isset($_GET['Customer']))
  170. $model->attributes=$_GET['Customer'];
  171.  
  172. $this->render('admin',array(
  173. 'model'=>$model,
  174. ));
  175. }
  176.  
  177. /**
  178. * Returns the data model based on the primary key given in the GET variable.
  179. * If the data model is not found, an HTTP exception will be raised.
  180. * @param integer the ID of the model to be loaded
  181. */
  182. public function loadModel($id)
  183. {
  184. $model=Customer::model()->findByPk($id);
  185. if($model===null)
  186. throw new CHttpException(404,'The requested page does not exist.');
  187. return $model;
  188. }
  189.  
  190. /**
  191. * Performs the AJAX validation.
  192. * @param CModel the model to be validated
  193. */
  194. protected function performAjaxValidation($model)
  195. {
  196. if(isset($_POST['ajax']) && $_POST['ajax']==='customer-form')
  197. {
  198. echo CActiveForm::validate($model);
  199. Yii::app()->end();
  200. }
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement