public function editAction($id = NULL)
 {
     $this->view->title = "Редагувати студента";
     $this->view->nothing = true;
     $this->view->ind_code = $this->request->get("ind_code");
     if (!is_null($this->view->ind_code) && $this->checkINN($this->view->ind_code) || !is_null($id)) {
         $this->view->nothing = false;
         $this->view->student = is_null($id) ? \Students::findFirst("ind_code = " . $this->view->ind_code) : \Students::findFirst($id);
         $this->view->user = $this->session->get("user");
         $methodistBrands = \BrandsMethodists::find("user_id=" . $this->view->user->id)->toArray();
         $methodistActivities = \ActivitiesMethodists::find("user_id=" . $this->view->user->id)->toArray();
         if ($this->view->student != NULL) {
             $this->view->ind_code = $this->view->student->ind_code;
             $this->view->user = \Users::findFirst($this->view->student->user_id);
             $studentBrands = BrandsService::getStudentBrands($this->view->student);
             $studentActivities = ActivitiesService::getStudentActivities($this->view->student);
             $studentPostsJSON = array();
             if ($this->view->student->StudentsPosts != NULL) {
                 foreach ($this->view->student->StudentsPosts as $sp) {
                     //{"dealer":"1","post":"2","brands":["2", "5"],"activities":["1"],"rate":"1.0","appoint_date":"2015-04-01"}
                     $studentPostsJSON[$sp->id] = array();
                     $studentPostsJSON[$sp->id]["dealer"] = $sp->dealer;
                     $studentPostsJSON[$sp->id]["post"] = $sp->post;
                     $array = array();
                     foreach ($sp->StudentsPostsBrands as $spb) {
                         $array[] = $spb->Brands->id;
                     }
                     $studentPostsJSON[$sp->id]["brands"] = $array;
                     $array = array();
                     foreach ($sp->StudentsPostsActivities as $spa) {
                         $array[] = $spa->Activities->id;
                     }
                     $studentPostsJSON[$sp->id]["activities"] = $array;
                     $studentPostsJSON[$sp->id]["rate"] = $sp->rate;
                     $studentPostsJSON[$sp->id]["appoint_date"] = $sp->appoint_date;
                 }
             }
             //$this->view->student->StudentsEducation;
         } else {
             $this->view->student = new \Students();
             $this->view->user = new \Users();
             $studentPostsJSON = array();
             $studentBrands = array();
             $studentActivities = array();
         }
         $this->view->brands = BrandsService::getForChosenEntity(\Brands::find()->toArray(), $methodistBrands, $studentBrands);
         $this->view->activities = ActivitiesService::getForChosenEntity(\Activities::find()->toArray(), $methodistActivities, $studentActivities);
         $this->view->allEducations = \Educations::find("parent_id is NULL");
         $this->view->allDealers = \Dealers::find();
         $this->view->studentPostsJSON = $studentPostsJSON;
     }
 }
 public function saveAction()
 {
     $errors = array();
     $id = $this->request->get("id");
     if ($this->request->isPost()) {
         $title = $this->request->get("title");
         $address = $this->request->get("address");
         $parent_id = $this->request->get("parent_id");
         $parent_id = $parent_id ? $parent_id : NULL;
         $status = $this->request->get("status");
         $city_id = $this->request->get("city_id");
         $stafflist_group_id = $this->request->get("stafflist_group_id");
         $inspectors = $this->request->get("inspectors");
         $user = $this->session->get('user');
         $brandsForm = $this->request->get("brands");
         $activities = $this->request->get("activities");
         $brandsForm = is_array($brandsForm) ? $brandsForm : array();
         $activitiesForm = is_array($activities) ? $activities : array();
         $dealer = $id ? \Dealers::findFirst($id) : new \Dealers();
         $calculationBrands = BrandsService::getCalculationResult($user->BrandsMethodists, $dealer->DealersBrands, $brandsForm);
         $calculationActivities = ActivitiesService::getCalculationResult($user->ActivitiesMethodists, $dealer->DealersActivities, $activitiesForm);
         $dealer->DealersBrands->delete();
         // delete all brands from dealer
         $dealer->DealersActivities->delete();
         // delete all activities from dealer
         $dealer->DealersControllers->delete();
         // delete all inspectors from dealer
         $dealer->title = $title;
         $dealer->address = $address;
         $dealer->parent_id = $parent_id;
         $dealer->status = $status;
         $dealer->city_id = $city_id;
         $dealer->stafflist_group_id = $stafflist_group_id;
         if ($id = !$dealer->save()) {
             foreach ($dealer->getMessages() as $msg) {
                 $errors[] = $msg->getMessage();
             }
         } else {
             $dealerBrands = array();
             $dealerActivities = array();
             $dealersControllers = array();
             foreach ($calculationBrands as $brand) {
                 $dealerBrand = new \DealersBrands();
                 $dealerBrand->brand_id = $brand->id;
                 $dealerBrand->dealer_id = $id;
                 $dealerBrands[] = $dealerBrand;
             }
             $dealer->DealersBrands = $dealerBrands;
             foreach ($calculationActivities as $activity) {
                 $dealerActivity = new \DealersActivities();
                 $dealerActivity->activity_id = $activity->id;
                 $dealerActivity->dealer_id = $id;
                 $dealerActivities[] = $dealerActivity;
             }
             $dealer->DealersActivities = $dealerActivities;
             foreach ($inspectors as $userId) {
                 $dealerInspector = new \DealersControllers();
                 $dealerInspector->dealer_id = $dealer->id;
                 $dealerInspector->user_id = $userId;
                 $dealersControllers[] = $dealerInspector;
             }
             $dealer->DealersControllers = $dealersControllers;
             if (!$dealer->save()) {
             }
         }
         if ($errors === array()) {
             // redirect to dealer edit
             $this->response->redirect('/methodist/dealer/edit/' . $dealer->id . "?saved=true");
         }
     } else {
         $errors[] = "Немає данних для збереження";
     }
     $this->view->errors = $errors;
     $this->view->back = $id ? '/methodist/dealer/edit/' . $id : '/methodist/dealer/add';
 }