/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Topics(); $fileCreation = false; if (isset($_FILES['upload']) || isset($_POST['Topics'])) { $data = array(); $topicText = null; if (isset($_FILES['upload'])) { $fileCreation = true; $data = array('name' => $_POST['topicName']); $topicText = $_POST['topicText']; } else { if (isset($_POST['Topics'])) { $data = $_POST['Topics']; $topicText = $_POST['TopicReplies']['text']; } } $data['text'] = $topicText; $model->setX2Fields($data, false, true); if (isset($_POST['x2ajax'])) { $ajaxErrors = $this->quickCreate($model); } else { if ($model->save()) { if ($fileCreation) { $media = new Media(); $username = Yii::app()->user->getName(); // file uploaded through form $temp = CUploadedFile::getInstanceByName('upload'); if ($temp && ($tempName = $temp->getTempName()) && !empty($tempName)) { $name = $temp->getName(); $name = str_replace(' ', '_', $name); $check = Media::model()->findAllByAttributes(array('fileName' => $name)); // rename file if there name conflicts by suffixing "(n)" if (count($check) != 0) { $count = 1; $newName = $name; $arr = explode('.', $name); $name = $arr[0]; while (count($check) != 0) { $newName = $name . '(' . $count . ').' . $temp->getExtensionName(); $check = Media::model()->findAllByAttributes(array('fileName' => $newName)); $count++; } $name = $newName; } } if (FileUtil::ccopy($tempName, "uploads/protected/media/{$username}/{$name}")) { $media->associationType = 'topicReply'; $media->associationId = $model->originalPost->id; $media->uploadedBy = $username; $media->createDate = time(); $media->lastUpdated = time(); $media->fileName = $name; $media->mimetype = $temp->type; if ($media->save()) { echo $model->id; Yii::app()->end(); } } } else { $this->redirect(array('view', 'id' => $model->id)); } } elseif ($model->hasErrors('text')) { Yii::app()->user->setFlash('error', 'Original post text cannot be blank.'); } } } if (isset($_POST['x2ajax']) || isset($_FILES['upload'])) { $this->renderInlineForm($model); } else { $this->render('create', array('model' => $model)); } }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Topics(); $fileCreation = false; if (isset($_FILES['upload']) || isset($_POST['Topics'])) { $data = array(); $topicText = null; if (isset($_FILES['upload'])) { $fileCreation = true; $data = array('name' => $_POST['topicName']); $topicText = $_POST['topicText']; } else { if (isset($_POST['Topics'])) { $data = $_POST['Topics']; $topicText = $_POST['TopicReplies']['text']; } } $data['text'] = $topicText; $model->setX2Fields($data, false, true); if (isset($_POST['x2ajax'])) { $ajaxErrors = $this->quickCreate($model); } else { if ($fileCreation) { // file uploaded through form $temp = CUploadedFile::getInstanceByName('upload'); $model->upload = $temp; } if ($model->save()) { if ($fileCreation && count($model->originalPost->attachments)) { echo $model->id; Yii::app()->end(); } else { $this->redirect(array('view', 'id' => $model->id)); } } elseif ($model->hasErrors('text')) { Yii::app()->user->setFlash('error', 'Original post text cannot be blank.'); } } } if (isset($_POST['x2ajax']) || isset($_FILES['upload'])) { $this->renderInlineForm($model); } else { $this->render('create', array('model' => $model)); } }