コード例 #1
0
ファイル: form.php プロジェクト: nonfiction/nterchange
 function &loadField($field, &$form)
 {
     return ControllerForm::addElement($field, $form, $this->model);
 }
コード例 #2
0
 /**
  * Displays an Edit form for the controller's default model
  *
  * Instantiates the model, fetches the form and displays it.
  * Also takes care of validation prior to passing the values to update()
  *
  * @see AppController::insert();
  * @param $parameter int The id of the record to be edited
  * @param $layout Default true. Whether to render in a layout.
  * @access public
  * @return null
  */
 function edit($parameter, $layout = true)
 {
     $this->auto_render = false;
     // Track the edit - this way we can keep track of the last edits of each person.
     $current_user_id = isset($this->_auth) && is_object($this->_auth) ? $this->_auth->currentUserID() : 0;
     /*$action_track = NModel::factory('action_track');
     		$status = $action_track->checkAssetEditStatus($this->name, $parameter);
     		if ($status == false) $track = $action_track->trackCurrentEdit($current_user_id, $this->name, $parameter);
     		unset($action_track);*/
     // load the model layer with info
     $model =& $this->getDefaultModel();
     if ($model && $model->get($parameter)) {
         $this->convertDateTimesToClient($model);
         $pk = $model->primaryKey();
         if (SITE_DRAFTS) {
             $draft_model =& $this->loadModel('cms_drafts');
             $draft_model->asset = $this->name;
             $draft_model->asset_id = $parameter;
             if ($draft_model->find()) {
                 // fill the local model with the draft info
                 $draft_model->fetch();
                 $current_user_id = isset($this->_auth) && is_object($this->_auth) ? $this->_auth->currentUserID() : 0;
                 if ($current_user_id == $draft_model->cms_modified_by_user) {
                     $content = unserialize($draft_model->draft);
                     foreach ($content as $field => $val) {
                         $model->{$field} = $val;
                     }
                     $this->flash->set('notice', 'You are currently editing your draft of this record.');
                     $this->flash->now('notice');
                 } else {
                     $user_model =& $this->loadModel('cms_auth');
                     $user_model->get($draft_model->cms_modified_by_user);
                     $this->flash->set('notice', 'This record has been saved as a draft by "' . $user_model->real_name . '".');
                     $this->flash->now('notice');
                     unset($user_model);
                 }
             }
         }
         // create the form
         $cform = new ControllerForm($this, $model);
         $form =& $cform->getForm();
         // is a page_content_id passed?
         $page_content_id = $this->getParam('page_content_id') ? (int) $this->getParam('page_content_id') : false;
         if ($page_content_id) {
             $page_content =& NController::factory('page_content');
             $page_content_model =& $page_content->getDefaultModel();
             $page_content_model->get($page_content_id);
             $page_content->convertDateTimesToClient($page_content_model);
             $page_model = $page_content_model->getLink('page_id', 'page');
         }
         // check if this content belongs to a different workflow group or is currently in process
         $owned_content = false;
         if (SITE_WORKFLOW) {
             $workflow =& NController::factory('workflow');
             $workflow_group_model = false;
             $workflow_model = false;
             $user_rights = 0;
             if ($page_content_id) {
                 $workflow_model =& $workflow->getDefaultModel();
                 $workflow_model->page_content_id = $page_content_id;
                 $workflow_model->asset = $this->name;
                 $workflow_model->asset_id = $model->{$pk};
                 $workflow_model->completed = 0;
                 if ($workflow_model->find(null, true)) {
                     $owned_content = true;
                 }
                 $workflow_group_model =& $workflow->getWorkflowGroup($page_model);
                 $user_rights = $workflow->getWorkflowUserRights($page_model);
             } else {
                 if ($workflow_group_model =& $workflow->findContentWorkflowGroup($this)) {
                     if ($workflow_model =& $workflow->findContentWorkflow($workflow_group_model->{$workflow_group_model->primaryKey()}, $this)) {
                         $page_model =& $this->loadModel('page');
                         if ($page_model->get($workflow_model->page_id)) {
                             $owned_content = true;
                         }
                         $user_rights = $workflow->getWorkflowUserRights($page_model);
                     } else {
                         $page_content =& NController::factory('page_content');
                         $page_model = $page_content->getContentPage($this);
                         if ($page_model) {
                             $owned_content = true;
                         }
                         $user_rights = $workflow->getWorkflowUserRights($page_model);
                     }
                 }
             }
             if (!$owned_content || $owned_content && $user_rights & WORKFLOW_RIGHT_EDIT) {
                 if ($workflow_model && $workflow_model->{$workflow_model->primaryKey()}) {
                     $form->removeElement('__submit_draft__');
                     $form->insertElementBefore(NQuickForm::createElement('submit', '__submit_workflow__', 'Start Workflow'), '__submit__');
                     $form->removeElement('__submit__');
                     $workflow_draft = unserialize($workflow_model->draft);
                     $form->setDefaults($workflow_draft);
                 } else {
                     if ($user_rights & WORKFLOW_RIGHT_EDIT) {
                         $form->insertElementBefore(NQuickForm::createElement('submit', '__submit_workflow__', 'Start Workflow'), '__submit__');
                         $form->removeElement('__submit__');
                     }
                 }
             } else {
                 if ($owned_content) {
                     $this->flash->set('notice', 'The record you are attempting to edit belongs to the "' . $workflow_group_model->workflow_title . '" Workflow Group');
                     $this->flash->now('notice');
                     $this->set('MAIN_CONTENT', '<p>Please go to the dashboard to continue.</p>');
                     $this->render(array('layout' => 'default'));
                     exit;
                 }
             }
         }
         // if page_content_id or (it's workflow owned and this user has editing rights)
         if ($page_content_id || $owned_content && $user_rights & WORKFLOW_RIGHT_EDIT) {
             // add timed content
             if ($owned_content && $user_rights & WORKFLOW_RIGHT_EDIT && $workflow_model) {
                 $form->setDefaults(array('timed_start' => $workflow_model->timed_start, 'timed_end' => $workflow_model->timed_end));
             } else {
                 if ($page_content_id) {
                     $form->setDefaults(array('timed_start' => $page_content_model->timed_start, 'timed_end' => $page_content_model->timed_end));
                 }
             }
             $page_content_model =& NModel::factory('page_content');
             $timed_start_el =& ControllerForm::addElement('timed_start', $form, $page_content_model);
             $timed_end_el =& ControllerForm::addElement('timed_end', $form, $page_content_model);
             if ($timed_start_el) {
                 $form->insertElementBefore($form->removeElement('timed_start'), $form->elementExists('__submit_workflow__') ? '__submit_workflow__' : '__submit__');
             }
             if ($timed_end_el) {
                 $form->insertElementBefore($form->removeElement('timed_end'), $form->elementExists('__submit_workflow__') ? '__submit_workflow__' : '__submit__');
             }
         }
         // assign the info and render
         $this->base_dir = APP_DIR;
         $assigns = array();
         $fields = $model->fields();
         if ($form->validate() && $this->update(true)) {
             // If it validates and updates, then clear out the action track.
             /*$action_track = NModel::factory('action_track');
             		$action_track->completeCurrentEdit($current_user_id, $this->name, $parameter);
             		unset($action_track);*/
             $this->flash->set('notice', 'Your record has been saved.');
             $this->redirectTo('show', $parameter);
         } else {
             if ($model) {
                 $this->set($model->toArray());
                 $this->set(array('form' => $form->toHTML(), 'asset' => $this->name, 'asset_name' => $this->page_title ? $this->page_title : Inflector::humanize($this->name)));
             }
         }
     } else {
         $this->flash->set('notice', 'The specified record could not be found.');
         $this->flash->now('notice');
     }
     if ($this->getParam('layout') == 'false') {
         $layout = false;
     }
     $this->render($layout ? array('layout' => 'default') : null);
 }