/** * Adds a domain to the database * * @param string $name The name of the domain * @param string type The type of the domain. Can be:<br/> * MASTER * NATIVE * @param int $template_id An optional template ID to be loaded in the domain * @return array {result:boolean,error_code:int,error_message:string} A json encoded array containing: * result: true if the domain has been created, false otherwise * error_code: the code of the error. 0 means no error * error_message: the explanation of the error */ public function actionAddDomain($name, $type = Domain::TYPE_MASTER, $template_id = '') { $result = true; $error = array('code' => 0, 'message' => ''); $domain = new Domain(); $domain->name = $name; $domain->type = $type; try { if (!$domain->save()) { $error['code'] = self::ERROR_VALIDATION_CODE; $error['message'] = $domain->getErrors(); $result = false; } else { // If there is a template, let's load it and insert: if ($template_id != '') { $records = Yii::app()->db->createCommand()->select()->from('zone_templ_records')->where('zone_templ_id = :id', array(':id' => $template_id))->queryAll(); $record_results = array(); foreach ($records as $record) { $record['name'] = str_replace(array('[ZONE]', '[SERIAL]'), array($name, '1'), $record['name']); $record['content'] = str_replace(array('[ZONE]', '[SERIAL]'), array($name, '1'), $record['content']); $record_results[] = $this->actionAddRecord($name, $record['name'], $record['type'], $record['content'], empty($record['ttl']) ? 3600 : $record['ttl'], empty($record['prio']) ? 0 : $record['prio'], false, false); } $domain->updateSOA(); $all_ok = true; foreach ($record_results as $record_result) { if ($record_result['result'] !== true) { $all_ok = false; } } if ($all_ok) { $result = true; } else { $result = false; $error['code'] = self::ERROR_TEMPLATE_CODE; $error['message'] = $record_results; } } } } catch (CDbException $e) { if ($e->getCode() == 23000) { // Domain already existing $error['code'] = self::ERROR_DOMAIN_ALREADY_EXISTING_CODE; $error['message'] = "Domain already existing"; } else { $error['code'] = $e->getCode(); $error['message'] = $e->getMessage(); } $result = false; } $var = array('error_code' => $error['code'], 'error_message' => $error['message'], 'result' => $result); $this->renderText(CJSON::encode($var)); return $var; }
function deleteAction() { $id = AF::get($_POST, 'id', 0); $modelsID = explode(',', $id); $errors = FALSE; foreach ($modelsID as $id) { $model = new Domain(); //$model->model_uset_id = $this->user->user_id; if ($model->findByPk($id)) { // if beforeDelete() returns an error, indicate this to the user if (!$model->delete($id)) { $errors = TRUE; } } else { $errors = TRUE; } if ($model->getErrors()) { $errors = TRUE; } unset($model); } if (isset($_POST['ajax'])) { AF::setJsonHeaders('json'); if ($errors) { Message::echoJsonError(__('domain_not_deleted')); } else { Message::echoJsonSuccess(__('domain_deleted')); } } $this->redirect(); }