Ejemplo n.º 1
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $hardware = Resources::getResourceTypes(Resources::HARDWARE_DB);
     $employees = Resources::getResourceTypes(Resources::EMPLOYEE_DB);
     $facilities = Resources::getResourceTypes(Resources::FACILITY_DB);
     $customResources = new CustomResources();
     $resourceAvailable = 1;
     $projectId = $_GET['pid'];
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['create'])) {
         $ecounter = 0;
         $hcounter = 0;
         $fcounter = 0;
         $ccounter = 0;
         foreach ($_POST as $key => $value) {
             $resource = null;
             $type = $key;
             $type = substr($type, 0, 4);
             if ($value['start_date'] != '') {
                 if ($type == 'Empl') {
                     $resource = new EmployeeSchedule();
                     $resource->attributes = $_POST[$key];
                     $resource->employee_id = EmployeeSchedule::findAvailableEmployeeResource(Resources::EMPLOYEE_DB, $resource->start_date, $resource->end_date, $value['name']);
                     $resource->pid = $projectId;
                     if ($resource->employee_id == 0) {
                         $resource = null;
                         $resourceAvailable = 0;
                     }
                 } elseif ($type == 'Hard') {
                     $resource = new HardwareSchedule();
                     $resource->attributes = $_POST[$key];
                     $resource->pid = $projectId;
                     $resourceAvailable = HardwareSchedule::findAvailableHardwareResource(Resources::HARDWARE_TYPES_DB, $resource->start_date, $resource->end_date, $value['name']);
                     if ($resourceAvailable == 0) {
                         $resource = null;
                     } else {
                         $resource->hardware_id = $resourceAvailable;
                     }
                 } elseif ($type == 'Faci') {
                     $resource = new FacilitySchedule();
                     $resource->attributes = $_POST[$key];
                     $resource->pid = $projectId;
                     $resourceAvailable = FacilitySchedule::findAvailableFacilityResource(Resources::FACILITY_TYPES_DB, $resource->start_date, $resource->end_date, $value['name']);
                     if ($resourceAvailable == 0) {
                         $resource = null;
                     } else {
                         $resource->facility_id = $resourceAvailable;
                     }
                 } elseif ($type == 'Cust') {
                     $resource = new CustomResources();
                     $resource->attributes = $_POST[$key];
                     $resource->pid = $projectId;
                     $resource->owner = Yii::app()->user->id;
                     $ccounter++;
                 }
                 if ($resource != null) {
                     $resource->save();
                 }
             }
         }
         if ($resourceAvailable > 0) {
             $this->redirect(array('project/view', 'id' => $projectId));
         }
     }
     $employeeNames = Resources::getResourceNames(Resources::EMPLOYEE_DB);
     $this->render('create', array('employees' => $employees, 'employeeNames' => $employeeNames, 'hardware' => $hardware, 'facilities' => $facilities, 'customResources' => $customResources));
 }