/**
  * Creates a new model.
  * @return create view
  */
 public function actionCreate()
 {
     // check if user has permissions to createMilestones
     if (Yii::app()->user->checkAccess('createMilestones')) {
         // create Milestones object model
         $model = new Milestones();
         // find all project managers
         $Users = Projects::model()->findManagersByProject(Yii::app()->user->getState('project_selected'));
         // if Milestones form exist
         if (isset($_POST['Milestones'])) {
             // set form elements to Milestones model attributes
             $model->attributes = $_POST['Milestones'];
             $model->project_id = Yii::app()->user->getState('project_selected');
             // validate milestone model
             if ($model->validate()) {
                 // find milestones dates
                 $milestone_startdate = date("Ymd", strtotime($model->milestone_startdate));
                 $milestone_duedate = date("Ymd", strtotime($model->milestone_duedate));
                 // get project data
                 $project = Projects::model()->findByPk($model->project_id);
                 // find project dates
                 $project_startDate = date("Ymd", strtotime($project->project_startDate));
                 $project_endDate = date("Ymd", strtotime($project->project_endDate));
                 // If milestone dates are not within project dates ERROR!!
                 if ($milestone_startdate >= $project_startDate && $milestone_startdate <= $project_endDate) {
                     if ($milestone_duedate <= $project_endDate && $milestone_duedate >= $project_startDate) {
                         // validate and save
                         if ($model->save()) {
                             // save log
                             $attributes = array('log_date' => date("Y-m-d G:i:s"), 'log_activity' => 'MilestoneCreated', 'log_resourceid' => $model->primaryKey, 'log_type' => Logs::LOG_CREATED, 'user_id' => Yii::app()->user->id, 'module_id' => Yii::app()->controller->id, 'project_id' => $model->project_id);
                             Logs::model()->saveLog($attributes);
                             // notify to user that has a milestone to attend
                             Yii::import('application.extensions.phpMailer.yiiPhpMailer');
                             $mailer = new yiiPhpMailer();
                             $subject = Yii::t('email', 'MilestoneAssigned') . " :: " . $model->milestone_title;
                             // user you will be notified
                             $User = Users::model()->findByPk($model->user_id);
                             $recipientsList = array('name' => $User->CompleteName, 'email' => $User->user_email);
                             // render template
                             $str = $this->renderPartial('//templates/milestones/assigned', array('milestone' => $model, 'urlToMilestone' => Yii::app()->createAbsoluteUrl('milestones/view', array('id' => $model->milestone_id)), 'applicationName' => Yii::app()->name, 'applicationUrl' => "http://" . $_SERVER['SERVER_NAME'] . Yii::app()->request->baseUrl), true);
                             $mailer->pushMail($subject, $str, $recipientsList, Emails::PRIORITY_NORMAL);
                             // to prevent F5 keypress, redirect to create page
                             $this->redirect(array('view', 'id' => $model->milestone_id));
                         }
                     } else {
                         $model->addError('milestone_duedate', Yii::t('milestones', 'DueDateError'));
                     }
                 } else {
                     $model->addError('milestone_startdate', Yii::t('milestones', 'StartDateError'));
                 }
             }
         }
         $this->render('create', array('model' => $model, 'users' => $Users));
     } else {
         throw new CHttpException(403, Yii::t('site', '403_Error'));
     }
 }