示例#1
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'show' page.
  */
 public function actionCreate()
 {
     $model = new Expense($this->action->id);
     if (isset($_POST['Expense'])) {
         // collect user input data
         $model->attributes = $_POST['Expense'];
         if (!isset($_POST['Expense']['companyId'])) {
             // set company based on the project
             if ($model->projectId >= 1) {
                 $criteria = new CDbCriteria();
                 $criteria->order = "`t`.`companyPriority` ASC, `t`.`id` ASC";
                 if (($company2Project = Company2Project::model()->findByAttributes(array('projectId' => $model->projectId), $criteria)) !== null) {
                     $model->companyId = $company2Project->companyId;
                 } else {
                     $model->companyId = 0;
                 }
             } else {
                 $model->companyId = 0;
             }
         }
         if (!isset($_POST['Expense']['managerId'])) {
             // current user is considered to be manager
             $model->managerId = Yii::app()->user->id;
         }
         // validate with the current action as scenario and save without validation
         if (($validated = $model->validate()) !== false && ($saved = $model->save(false)) !== false) {
             // set success message
             MUserFlash::setTopSuccess(Yii::t('hint', 'The new expense record number "{expenseNumber}" has been successfully created.', array('{expenseNumber}' => MHtml::wrapInTag($model->id, 'strong'))));
             // go to the 'show' page
             $this->redirect(array('show', 'id' => $model->id));
         }
     } else {
         // pre-assigned attributes (default values for a new record)
         $model->billToCompany = Expense::BILL_TO_COMPANY;
         // current user is considered to be manager
         $model->managerId = Yii::app()->user->id;
         if (isset($_GET['projectId'])) {
             // project is known
             $model->projectId = $_GET['projectId'];
         }
     }
     $this->render($this->action->id, array('model' => $model));
 }