コード例 #1
0
 function addExistingContent($parameter)
 {
     $page_model =& $this->loadModel('page');
     $page_model->get($parameter);
     $template_container_id = isset($this->params['template_container_id']) ? $this->params['template_container_id'] : false;
     $asset = isset($this->params['asset']) ? $this->params['asset'] : false;
     // instantiate form
     include_once 'n_quickform.php';
     $form = new NQuickForm();
     $values = $form->getSubmitValues();
     $form->addElement('header', null, 'Add "' . Inflector::humanize($asset) . '" content to the "' . $page_model->title . '" page');
     $asset_controller =& NController::factory($asset);
     $asset_model =& NModel::factory($asset);
     $pk = $asset_model->primaryKey();
     $records = array();
     if ($asset_model->find()) {
         while ($asset_model->fetch()) {
             $records[$asset_model->{$pk}] = $asset_model->cms_headline;
         }
     }
     unset($asset_model);
     // add asset select
     $options = defined('SITE_WORKFLOW') && SITE_WORKFLOW ? array() : array('size' => 10, 'multiple' => 'multiple');
     $form->addElement('select', 'asset_id', Inflector::humanize($asset), $records, $options);
     // hidden fields
     $form->addElement('hidden', 'asset', $asset);
     $form->addElement('hidden', 'template_container_id', $template_container_id);
     if (isset($this->params['_referer'])) {
         $form->addElement('hidden', '_referer', urlencode($this->params['_referer']));
     }
     // finish up
     $form->addElement('submit', '__submit__', 'Add Content');
     // rules
     defined('SITE_WORKFLOW') && SITE_WORKFLOW ? $form->addRule('asset_id', 'You must select a record.', 'required') : $form->addGroupRule('asset_id', 'You must select a record.', 'required');
     $form->addRule('asset', '', 'required');
     $form->addRule('template_container_id', '', 'required');
     // check for workflow
     $user_rights = 0;
     if (SITE_WORKFLOW) {
         // get the users rights and bit compare them below
         $workflow =& NController::factory('workflow');
         $user_rights = $workflow->getWorkflowUserRights($page_model);
         if ($workflow_group_model =& $workflow->getWorkflowGroup($page_model)) {
             if (!($user_rights & WORKFLOW_RIGHT_EDIT)) {
                 // they don't belong here - go to the dashboard
                 header('Location:/' . APP_DIR . '/dashboard');
             } else {
                 if ($user_rights & WORKFLOW_RIGHT_EDIT) {
                     $form->insertElementBefore(NQuickForm::createElement('submit', '__submit_workflow__', 'Start Workflow'), '__submit__');
                     $form->removeElement('__submit__');
                 }
             }
         }
         unset($workflow);
     }
     $form->addElement('header', null, 'Make it timed content?');
     $timed_options = array('format' => 'Y-m-d H:i', 'minYear' => date('Y'), 'maxYear' => date('Y') + 4, 'addEmptyOption' => true);
     $form->addElement('date', 'timed_start', 'Timed Start', $timed_options);
     $form->addElement('date', 'timed_end', 'Timed End', $timed_options);
     if (!$user_rights) {
         $form->addElement('submit', '__submit_timed__', 'Add Scheduled Content');
     } else {
         $form->addElement('submit', '__submit_workflow__', 'Start Workflow with Scheduled Content');
     }
     if ($form->validate()) {
         $values = $form->exportValues();
         $model =& $this->loadModel($this->name);
         $workflow_active = false;
         if (SITE_WORKFLOW) {
             $workflow =& NController::factory('workflow');
             // check if this content is on any other page.
             // if it is, if either pages are part of a workflow group, we need to copy the content (go to addnewcontent with notice)
             // if neither do, then go ahead
             $asset_model =& $asset_controller->loadModel($asset_controller->name);
             $asset_model->get($values['asset_id']);
             $other_page =& $this->getContentPage($asset_controller);
             if ($other_page) {
                 $owned_content = false;
                 if ($workflow_group_model =& $workflow->getWorkflowGroup($page_model)) {
                     $owned_content = true;
                 } else {
                     if ($workflow_group_model =& $workflow->getWorkflowGroup($other_page)) {
                         $owned_content = true;
                     }
                 }
                 // if the content is already connected somewhere and one of the pages belongs to a workflow_group, then addNewContent with preloaded content
                 if ($owned_content) {
                     if (isset($values['__submit__'])) {
                         unset($values['__submit__']);
                     }
                     if (isset($values['__submit_workflow__'])) {
                         unset($values['__submit_workflow__']);
                     }
                     $this->redirectTo('copy_existing_content', $parameter, $values);
                     exit;
                 }
             }
             if (isset($values['__submit_workflow__']) && $values['__submit_workflow__']) {
                 $workflow =& NController::factory('workflow');
                 if ($workflow_group_model = $workflow->getWorkflowGroup($page_model)) {
                     $workflow_active = true;
                 }
             }
         }
         $model->page_id = $parameter;
         if (SITE_WORKFLOW && isset($values['__submit_workflow__']) && $values['__submit_workflow__']) {
             $model->cms_workflow = 1;
         }
         $model->page_template_container_id = $values['template_container_id'];
         $model->content_asset = $values['asset'];
         // set the timed values
         $timed_start = null;
         $timed_end = null;
         include_once 'n_date.php';
         if (isset($values['timed_start'])) {
             $timed_start = NDate::arrayToDate($values['timed_start']);
             $timed_start = NDate::convertTimeToUTC($timed_start);
             unset($values['timed_start']);
         }
         if (isset($values['timed_end'])) {
             $timed_end = NDate::arrayToDate($values['timed_end']);
             $timed_end = NDate::convertTimeToUTC($timed_end);
             unset($values['timed_end']);
         }
         if (!$workflow_active) {
             $table = $model->table();
             $def = $table['timed_start'];
             if (NDate::validDateTime($timed_start, $def)) {
                 $model->timed_start = $timed_start;
             } else {
                 $model->timed_start = N_DAO_NOTNULL & $def ? $timed_start : 'null';
             }
             $def = $table['timed_end'];
             if (NDate::validDateTime($timed_end, $def)) {
                 $model->timed_end = $timed_end;
             } else {
                 $model->timed_end = N_DAO_NOTNULL & $def ? $timed_end : 'null';
             }
         }
         $model->cms_created = $model->now();
         $model->cms_modified = $model->now();
         $model->cms_modified_by_user = $this->_auth->currentUserID();
         if (!is_array($values['asset_id'])) {
             $values['asset_id'] = array($values['asset_id']);
         }
         foreach ($values['asset_id'] as $asset_id) {
             $model->content_asset_id = $asset_id;
             $model->insert();
             if (defined('SITE_AUDIT_TRAIL') && SITE_AUDIT_TRAIL) {
                 // audit trail
                 $audit_trail =& NController::factory('audit_trail');
                 $audit_trail->insert(array('asset' => $asset_controller->name, 'asset_id' => $asset_id, 'action_taken' => AUDIT_ACTION_CONTENT_ADDEXISTING, 'page_content_id' => $model->{$model->primaryKey()}, 'page_id' => $model->page_id));
                 unset($audit_trail);
             }
         }
         if ($workflow_active) {
             $asset_controller =& NController::factory($values['asset']);
             $asset_controller->_auth = new NAuth();
             $asset_model =& $asset_controller->getDefaultModel();
             $asset_model->get($values['asset_id'][0]);
             $workflow_values = array();
             $workflow_values['page_content_id'] = $model->{$model->primaryKey()};
             $workflow_values['workflow_group_id'] = $workflow_group_model->{$workflow_group_model->primaryKey()};
             // add timed content
             $workflow_values['timed_start'] = $timed_start;
             $workflow_values['timed_end'] = $timed_end;
             $workflow->saveWorkflow($workflow_values, WORKFLOW_ACTION_ADDEXISTING, $asset_controller);
         }
         // delete the page cache
         $page =& NController::singleton('page');
         $page->deletePageCache($model->page_id);
         unset($page);
         // set up the referer
         if (isset($this->params['_referer']) && $this->params['_referer']) {
             $referer = urldecode($this->params['_referer']);
         } else {
             include_once 'view/helpers/url_helper.php';
             $referer = urlHelper::urlFor($this, array('controller' => 'page', 'action' => 'surftoedit', 'id' => $parameter));
         }
         header('Location:' . $referer);
         exit;
     }
     $this->auto_render = false;
     $this->page_title = 'Add Existing Content to "' . $page_model->title . '"';
     $this->set(array('title' => 'Select Content', 'form' => $form->toHTML()));
     $this->render(array('action' => 'form', 'layout' => 'plain'));
     unset($page_model);
 }