public function actionCreate() { $data = $_POST; if (!empty($data['name'])) { $data['name'] = substr(strtoupper(preg_replace("/[^a-zA-Z0-9\\-\\_]/", '', $data['name'])), 0, 12); if ($data['name'] != '') { $type = ApplicationTypes::model()->find('name=:name', array(':name' => $data['name'])); if ($type == null) { $type = new ApplicationTypes(); $type->name = $data['name']; $type->created_by = Yii::app()->user->name; $type->save(); } echo CJSON::encode(array('type' => 'success', 'data' => array('type_id' => $type->type_id, 'name' => $type->name))); } else { echo CJSON::encode(array('type' => 'error', 'data' => 'TYPE_ERROR: Invalid application type')); } } else { echo CJSON::encode(array('type' => 'error', 'data' => 'CSRF_ERROR: CSRF Token did not match')); } }
public function actionCreate() { $data = $_POST; if (!empty($data)) { $data['project_id'] = trim($data['project_id']); $data['name'] = trim($data['name']); $data['description'] = trim($data['description']); $data['accessibility'] = trim($data['accessibility']); $data['repository_url'] = trim($data['repository_url']); $data['description'] = trim($data['description']); $data['instructions'] = trim($data['instructions']); $data['production_date'] = trim($data['production_date']); $data['termination_date'] = trim($data['termination_date']); $data['uses_mobile_patterns'] = $data['uses_mobile_patterns'] == 'true' ? 1 : 0; //FORM VALIDATION HERE $errors = array(); //project id is required if (strlen($data['project_id']) == 0) { array_push($errors, 'PROJECT_ERROR: Project ID is required'); } else { if (!Projects::model()->exists('project_id=:project_id', array(':project_id' => $data['project_id']))) { array_push($errors, 'PROJECT_ERROR: Project ID does not exist'); } } //name is required if (strlen($data['name']) == 0) { array_push($errors, 'NAME_ERROR: Name is required'); } //type is required if (strlen($data['type_name']) == 0) { array_push($errors, 'TYPE_ERROR: Type is required'); //invalid type } else { $app_type = ApplicationTypes::model()->find('name=:name', array(':name' => $data['type_name'])); if ($app_type == null) { array_push($errors, 'TYPE_ERROR: Type is not in the list'); } else { $data['type_id'] = $app_type->type_id; } } //accessibility is required if (strlen($data['accessibility']) == 0) { array_push($errors, 'ACCESSIBILITY_ERROR: Accessibility is required'); //accessibility should either be PUBLIC or PRIVATE only } else { if ($data['accessibility'] != 'PUBLIC' && $data['accessibility'] != 'PRIVATE') { array_push($errors, 'ACCESSIBILITY_ERROR: Accessibility option selected is invalid'); } } //data is good if (count($errors) == 0) { $application = new Applications(); $application->project_id = (int) $data['project_id']; $application->type_id = $data['type_id']; $application->name = $data['name']; $application->description = $data['description']; $application->accessibility = $data['accessibility']; $application->repository_url = $data['repository_url']; $application->uses_mobile_patterns = $data['uses_mobile_patterns']; $application->instructions = $data['instructions']; $application->rd_point_person = $data['rd_point_person']; $application->production_date = $data['production_date']; $application->termination_date = $data['termination_date']; $application->date_created = date("Y-m-d H:i:s"); $application->date_updated = '0000-00-00 00:00:00'; $application->created_by = Yii::app()->user->name; $application->save(); // add to server if necessary if (isset($data['server_id'])) { $app_server = new ApplicationServers(); $app_server->application_id = $application->application_id; $app_server->server_id = $data['server_id']; $app_server->application_path = $data['application_path']; $app_server->application_log = $data['application_log']; $app_server->date_created = date("Y-m-d H:i:s"); $app_server->date_updated = '0000-00-00 00:00:00'; $app_server->created_by = Yii::app()->user->name; $app_server->save(); } echo CJSON::encode(array('type' => 'success', 'data' => '')); } else { echo CJSON::encode(array('type' => 'error', 'data' => implode(',', $errors))); } } else { echo CJSON::encode(array('type' => 'error', 'data' => 'CSRF_ERROR: CSRF Token did not match')); } }