function addNewContent($parameter, $load_model_content = false) { $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; $asset_controller =& NController::singleton($asset); $asset_controller->_auth = new NAuth(); // load the model layer with info $asset_model =& $asset_controller->getDefaultModel(); // create the form include_once 'controller/form.php'; $cform = new ControllerForm($asset_controller, $asset_model); $form =& $cform->getForm(); // check for workflow if (SITE_WORKFLOW) { // get the users rights and bit compare them below $workflow =& NController::factory('workflow'); $user_rights = $workflow->getWorkflowUserRights($page_model); $workflow_group =& $workflow->getWorkflowGroup($page_model); if ($workflow_group && !($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); } // timed content $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); $form->addElement('submit', '__submit_timed__', 'Add Scheduled Content'); // page_content values $form->addElement('hidden', 'template_container_id', $template_container_id); $form->addElement('hidden', 'asset', $asset); if (isset($this->params['_referer'])) { $form->addElement('hidden', '_referer', urlencode($this->params['_referer'])); } // assign the info and render $asset_controller->base_dir = APP_DIR; $assigns = array(); $table = $asset_model->table(); $fields = $asset_model->fields(); if ($form->validate()) { $values = $form->getSubmitValues(); if (in_array('cms_created', $fields)) { $asset_model->cms_created = $asset_model->now(); } if (in_array('cms_modified', $fields)) { $asset_model->cms_modified = $asset_model->now(); } // set the user id if it's applicable and available if (in_array('cms_modified_by_user', $fields)) { $asset_model->cms_modified_by_user = isset($asset_controller->_auth) ? $asset_controller->_auth->currentUserId() : 0; } $referer = isset($values['_referer']) ? $values['_referer'] : false; if ($referer) { // cheat and remove the referer from the form oject so it doesn't redirect if (isset($form->_submitValues['_referer'])) { unset($form->_submitValues['_referer']); } } $success = $form->process(array($cform, 'processForm')); $asset_id = $asset_model->{$asset_model->primaryKey()}; if ($success) { $values = $form->exportValues(); $model =& $this->loadModel($this->name); $workflow_active = false; $workflow_required = false; if (SITE_WORKFLOW) { $workflow =& NController::factory('workflow'); if ($workflow_group_model = $workflow->getWorkflowGroup($page_model)) { $workflow_required = true; } if ($workflow_required && isset($values['__submit_workflow__'])) { $workflow_active = true; } } $model->page_id = $parameter; $model->page_template_container_id = $values['template_container_id']; $model->content_asset = $values['asset']; $model->content_asset_id = $asset_id; // prep the timed content values if they exist if (isset($values['timed_start'])) { $values['timed_start'] = NDate::arrayToDate($values['timed_start']); $values['timed_start'] = NDate::convertTimeToUTC($values['timed_start']); } if (isset($values['timed_end'])) { $values['timed_end'] = NDate::arrayToDate($values['timed_end']); $values['timed_end'] = NDate::convertTimeToUTC($values['timed_end']); } // if (!$workflow_active) { if (isset($values['timed_start'])) { $model->timed_start = $values['timed_start']; } if (isset($values['timed_end'])) { $model->timed_end = $values['timed_end']; } } if ($workflow_required) { $model->cms_workflow = 1; } $model->cms_created = $model->now(); $model->cms_modified = $model->now(); $model->cms_modified_by_user = $this->_auth->currentUserID(); if ($workflow_active) { // set page_content cms_workflow to 1 so it can't show up $model->cms_workflow = 1; } $model->insert(); // do the workflow stuff if appropriate if ($workflow_active) { $page_content_id = $model->{$model->primaryKey()}; $workflow_values = array(); $workflow_values['page_content_id'] = $page_content_id; $workflow_values['workflow_group_id'] = $workflow_group_model->{$workflow_group_model->primaryKey()}; // add timed content if (isset($values['timed_start'])) { $workflow_values['timed_start'] = $values['timed_start']; } if (isset($values['timed_end'])) { $workflow_values['timed_end'] = $values['timed_end']; } $workflow->saveWorkflow($workflow_values, WORKFLOW_ACTION_ADDNEW, $asset_controller); // set page_content cms_workflow to 1 so it can't show up } // delete the page cache $page =& NController::singleton('page'); $page->deletePageCache($model->page_id); 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_model->{$asset_model->primaryKey()}, 'action_taken' => AUDIT_ACTION_CONTENT_ADDNEW, 'page_content_id' => $model->{$model->primaryKey()}, 'page_id' => $model->page_id)); unset($audit_trail); } unset($page); unset($model); } 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; } else { if ($asset_model) { $this->set(array('form' => $form->toHTML(), 'asset' => $asset_controller->name, 'asset_name' => Inflector::humanize($asset_controller->name))); } } $this->auto_render = false; $this->page_title = 'Add New Content to "' . $page_model->title . '"'; $this->render(array('action' => 'form', 'layout' => 'default')); }
function process($workflow_id) { $this->auto_render = false; $model =& $this->getDefaultModel(); $model->completed = 0; // get the workflow info if (!$model->get($workflow_id)) { $this->redirectTo('index'); } // push to the client's time zone $this->convertDateTimesToClient($model); // load the workflow group $workflow_group =& $model->getLink('workflow_group_id', 'workflow_group'); // get the asset info for the email $asset_ctrl =& NController::factory($model->asset); $asset_model =& $asset_ctrl->getDefaultModel(); if (!$asset_ctrl || !$asset_model->get($model->asset_id)) { $this->redirectTo('index'); } $asset_name = $asset_ctrl->page_title ? $asset_ctrl->page_title : Inflector::humanize($asset_ctrl->name); // load the workflow draft into the asset_model for the form $workflow_values = unserialize($model->draft); foreach ($workflow_values as $field => $val) { $asset_model->{$field} = $val; } // check for the content being linked to any other pages $page_content_model =& NModel::factory('page_content'); $page_contents =& $page_content_model->getActivePageContent($asset_ctrl->name, $asset_model->{$asset_model->primaryKey()}); $pages = false; if ($model->action == WORKFLOW_ACTION_EDIT && $page_contents && count($page_contents) > 1) { $pages = array(); $tmp_page_model = clone $page_model; foreach ($page_contents as $page_content) { if (!isset($pages[$tmp_page_model->{$tmp_page_model->primaryKey()}])) { $tmp_page_model->reset(); $tmp_page_model->get($page_content->page_id); $pages[$tmp_page_model->{$tmp_page_model->primaryKey()}] = $tmp_page_model->toArray(); } } if (count($pages) == 1) { $pages = false; } unset($tmp_page_model); } unset($page_contents); $this->set('pages', $pages); unset($pages); // get the page info for the email $page_content_model =& $model->getLink('page_content_id', 'page_content'); $page =& NController::singleton('page'); $page_model = $page->getDefaultModel(); if (!$page_model->get($page_content_model->page_id)) { $this->redirectTo('index'); } // set up urls $public_site = preg_replace('|/$|', '', PUBLIC_SITE); $admin_site = preg_replace('|/$|', '', defined('ADMIN_URL') && ADMIN_URL ? ADMIN_URL : PUBLIC_SITE); $live_url = $public_site . $page->getHref($page_model->toArray()); $page->nterchange = true; $preview_url = $admin_site . $page->getHref($page_model->toArray()); // user rights $user_rights =& $this->getWorkflowUserRights($page_model); if ($user_rights & WORKFLOW_RIGHT_EDIT && !($user_rights & WORKFLOW_RIGHT_APPROVE)) { // only an author, can't approve anything $this->redirectTo('index'); } $description = ''; switch ($model->action) { case WORKFLOW_ACTION_EDIT: $description = 'This content is being edited on the page.'; break; case WORKFLOW_ACTION_DELETE: $description = 'This content is being deleted.'; break; case WORKFLOW_ACTION_ADDNEW: $description = 'This is new content being added to the page.'; break; case WORKFLOW_ACTION_ADDEXISTING: $description = 'This is existing content being added to the page.'; break; case WORKFLOW_ACTION_REMOVE: $description = 'The content is being removed from the page.'; break; } $this->set('description', $description); // set up the form $cform = new ControllerForm($asset_ctrl, $asset_model); $form =& $cform->getForm(); if (SITE_DRAFTS) { // remove the submit draft button since we're in workflow $form->removeElement('__submit_draft__'); } // remove the submit button since we're in workflow $form->removeElement('__submit__'); // get the workflow form $wcform = new ControllerForm($this, $model); $wform =& $wcform->getForm(); $submit =& $wform->getElement('__submit__'); $submit->setName('__submit_workflow__'); if ($user_rights & WORKFLOW_RIGHT_PUBLISH) { $submit->setValue('Submit & Publish'); } else { $submit->setValue('Submit'); } // add workflow form to the existing form foreach ($wform->_elements as $el) { $form->addElement($el); } $form->addFormRule(array(&$this, 'validateWorkflowProcess')); /* if (!($user_rights & WORKFLOW_RIGHT_EDIT)) { $fields = $asset_model->fields(); foreach ($fields as $i=>$field) { if ($form->elementExists($field)) { $el = &$form->getElement($field); $el->freeze(); } } } */ if ($model->action == WORKFLOW_ACTION_REMOVE) { $fields = $asset_model->fields(); $form->removeElement('timed_start'); $form->removeElement('timed_end'); $form->freeze($fields); } // act on the submission if ($form->validate()) { $values = $form->getSubmitValues(); $model->comments = $values['comments']; $fields = $asset_model->fields(); $draft_values = array(); foreach ($fields as $field) { if (isset($values[$field])) { $draft_values[$field] = $values[$field]; } } $auth = new NAuth(); $user_id = $auth->currentUserID(); unset($auth); if ($values['workflow_approve'] == 0) { // if not approved, then email the original user and set the workflow unsubmitted if ($parent_workflow = $model->parent_workflow) { $model->delete(); $model->reset(); $model->get($parent_workflow); $model->approved = 0; } else { $model->submitted = 0; } if (isset($draft_values)) { $model->draft = serialize($draft_values); } $model->comments = $values['comments']; include_once 'n_date.php'; $model->cms_created = NDate::convertTimeToUTC($model->cms_created, '%Y-%m-%d %H:%M:%S'); $model->cms_modified = NDate::convertTimeToUTC($model->cms_modified, '%Y-%m-%d %H:%M:%S'); $model->update(); $current_user =& NModel::factory('cms_auth'); $current_user->get($user_id); $user_model =& NModel::factory('cms_auth'); $user_model->get($model->cms_modified_by_user); include_once 'Mail.php'; $mail =& Mail::factory('mail', "-f{$current_user->email}"); $headers['From'] = "{$current_user->real_name} <{$current_user->email}>"; $headers['Subject'] = 'Website: "' . $workflow_group->workflow_title . '" Workflow Group has content that was declined'; $msg = ''; $msg .= "The workflow for the \"{$asset_model->cms_headline}\" {$asset_name} record on the {$page_model->title} page was declined.\n\n"; $msg .= "COMMENTS:\n{$model->comments}\n\n"; $msg .= "You can view the current live page at:\n{$live_url}\n\n"; $msg .= "You can preview the page at:\n{$preview_url}\n\n"; $msg .= "To Edit & Resubmit your changes, please go to Your Dashboard:\n" . $admin_site . '/' . APP_DIR . "/dashboard\n\n"; $this->set('public_site', $public_site); $this->set('admin_site', $admin_site); // gather the users $recipients = array(); $email = "{$user_model->real_name} <{$user_model->email}>"; $recipients[] = $email; $mail->send($recipients, $headers, $msg); unset($mail); if (defined('SITE_AUDIT_TRAIL') && SITE_AUDIT_TRAIL) { // audit trail $audit_trail =& NController::factory('audit_trail'); $audit_trail->insert(array('asset' => $model->asset, 'asset_id' => $model->asset_id, 'action_taken' => AUDIT_ACTION_WORKFLOW_DECLINE, 'workflow_id' => $model->{$model->primaryKey()}, 'workflow_group_id' => $model->workflow_group_id, 'page_id' => $model->page_id, 'page_content_id' => $model->page_content_id)); unset($audit_trail); } $this->render(array('layout' => 'plain', 'action' => 'disapproved')); exit; } else { // if approved, then set any comments, set the workflow approved, insert a new one // either submit it (which emails it to the next users) // or publish it (if there are no next users) if ($user_rights & WORKFLOW_RIGHT_PUBLISH) { // publish by pulling the draft, updating the original asset and marking page_content.cms_workflow=0 if (isset($draft_values)) { foreach ($draft_values as $field => $val) { $asset_model->{$field} = $val; } } $asset_model->cms_modified = $asset_model->now(); $asset_model->cms_modified_by_user = $user_id; $asset_model->cms_draft = 0; $asset_model->update(); // kill a draft if one exists $workflow_model =& NModel::factory('workflow'); $workflow_model->parent_workflow = 0; $workflow_model->asset = $model->asset; $workflow_model->asset_id = $model->asset_id; $workflow_model->workflow_group_id = $model->workflow_group_id; if ($workflow_model->find(null, true)) { // delete drafts related to this workflow $draft_model =& NModel::factory('cms_drafts'); $draft_model->asset = $model->asset; $draft_model->asset_id = $model->asset_id; $draft_model->cms_modified_by_user = $workflow_model->cms_modified_by_user; if ($draft_model->find()) { while ($draft_model->fetch()) { $draft_model->delete(); } } } if ($workflow_model->action == WORKFLOW_ACTION_REMOVE) { // delete the page_content to detach if ($page_content_model->{$page_content_model->primaryKey()}) { $page_content_model->delete(); } unset($page_content_model); $page->deletePageCache($model->page_id); if (defined('SITE_AUDIT_TRAIL') && SITE_AUDIT_TRAIL) { // audit trail $audit_trail =& NController::factory('audit_trail'); $audit_trail->insert(array('asset' => $model->asset, 'asset_id' => $model->asset_id, 'action_taken' => AUDIT_ACTION_WORKFLOW_APPROVEREMOVE, 'workflow_id' => $model->{$model->primaryKey()}, 'workflow_group_id' => $model->workflow_group_id, 'page_id' => $model->page_id, 'page_content_id' => $model->page_content_id)); unset($audit_trail); } $this->completeWorkflow($workflow_id); $this->set('workflow_group', $workflow_group->toArray()); $this->set('asset_name', $asset_name); $this->set('asset', $asset_model->toArray()); $this->set('page', $page_model->toArray()); $this->render(array('action' => 'removed', 'layout' => 'default')); exit; } else { // update the page_content row if ($page_content_model->{$page_content_model->primaryKey()}) { $timed_start = NDate::arrayToDate($values['timed_start']); $timed_end = NDate::arrayToDate($values['timed_end']); if (!NDate::validDateTime($timed_start)) { $timed_start = 'null'; } if (!NDate::validDateTime($timed_end)) { $timed_end = 'null'; } // set the timed content in page_content $page_content_model->timed_start = $timed_start; $page_content_model->timed_end = $timed_end; // set page_content cms_workflow to 0, allowing the content to show up $page_content_model->cms_workflow = 0; $page_content_model->update(); } unset($page_content_model); $this->completeWorkflow($workflow_id); $page->deletePageCache($page_model->{$page_model->primaryKey()}); if (defined('SITE_AUDIT_TRAIL') && SITE_AUDIT_TRAIL) { // audit trail $audit_trail =& NController::factory('audit_trail'); $audit_trail->insert(array('asset' => $model->asset, 'asset_id' => $model->asset_id, 'action_taken' => AUDIT_ACTION_WORKFLOW_APPROVEPUBLISH, 'workflow_id' => $model->{$model->primaryKey()}, 'workflow_group_id' => $model->workflow_group_id, 'page_id' => $model->page_id, 'page_content_id' => $model->page_content_id)); unset($audit_trail); } $this->set('workflow_group', $workflow_group->toArray()); $this->set('asset_name', $asset_name); $this->set('asset', $asset_model->toArray()); $this->set('page', $page_model->toArray()); $this->render(array('action' => 'published', 'layout' => 'default')); exit; } } else { $model->approved = 1; $model->comments = $values['comments']; $timed_start = isset($values['timed_start']) ? NDate::arrayToDate($values['timed_start']) : null; $timed_end = isset($values['timed_end']) ? NDate::arrayToDate($values['timed_end']) : null; if (!NDate::validDateTime($timed_start)) { $timed_start = 'null'; } if (!NDate::validDateTime($timed_end)) { $timed_end = 'null'; } $model->timed_start = $timed_start; $model->timed_end = $timed_end; include_once 'n_date.php'; $model->cms_created = NDate::convertTimeToUTC($model->cms_created, '%Y-%m-%d %H:%M:%S'); $model->cms_modified = NDate::convertTimeToUTC($model->cms_modified, '%Y-%m-%d %H:%M:%S'); $model->update(); $parent_workflow = $model->{$model->primaryKey()}; $new_workflow =& NModel::factory($this->name); $new_workflow->page_id = $model->page_id; $new_workflow->page_content_id = $model->page_content_id; $new_workflow->workflow_group_id = $model->workflow_group_id; $new_workflow->asset = $model->asset; $new_workflow->asset_id = $model->asset_id; $new_workflow->action = $model->action; $new_workflow->draft = serialize($draft_values); $new_workflow->comments = $values['comments']; $new_workflow->timed_start = $timed_start; $new_workflow->timed_end = $timed_end; $new_workflow->parent_workflow = $parent_workflow; $new_workflow->cms_created = $new_workflow->now(); $new_workflow->cms_modified = $new_workflow->now(); $new_workflow->cms_modified_by_user = $user_id; $new_workflow->insert(); if (defined('SITE_AUDIT_TRAIL') && SITE_AUDIT_TRAIL) { // audit trail $audit_trail =& NController::factory('audit_trail'); $audit_trail->insert(array('asset' => $model->asset, 'asset_id' => $model->asset_id, 'action_taken' => AUDIT_ACTION_WORKFLOW_APPROVE, 'workflow_id' => $model->{$model->primaryKey()}, 'workflow_group_id' => $model->workflow_group_id, 'page_id' => $model->page_id, 'page_content_id' => $model->page_content_id)); unset($audit_trail); } $this->flash->set('notice', 'Your workflow step has been submitted and notifications have been sent.'); $this->redirectTo('submit', $new_workflow->{$new_workflow->primaryKey()}); } } } unset($page_content_model); // set up the view $this->set('workflow_group', $workflow_group->toArray()); $this->set('asset_name', $asset_name); $this->set('asset', $asset_model->toArray()); $this->set('page', $page_model->toArray()); $this->set('form', $form->toHTML()); $this->render(array('layout' => 'plain', 'action' => 'process')); }
function getForm() { require_once 'controller/form.php'; $model = $this->getDefaultModel(); $cform = new ControllerForm($this, $model); return $cform->getForm(); }
function viewlist($parameter = null) { $this->auto_render = false; $wusers_ctrl =& NController::singleton('workflow_users'); include_once 'controller/form.php'; $this->base_dir = APP_DIR; $model =& $this->loadModel($this->name); $pk = $model->primaryKey(); if ($model) { if ($parameter) { $model->{$model->primaryKey()} = $parameter; } $model->find(); $rows = $model->fetchAll(true); $wusers_model =& $wusers_ctrl->loadModel($wusers_ctrl->name); $workflows = ''; // put the _headline variable in for the viewlist template foreach ($rows as $key => $workflow_group) { $cform = new ControllerForm($wusers_ctrl, $wusers_model); $form =& $cform->getForm('add_person_form_' . $workflow_group[$pk]); $form->removeElement('workflow_group_id'); $form->setDefaults(array('workflow_group_id' => $workflow_group[$pk])); $form->addElement('hidden', 'workflow_group_id', $workflow_group[$pk]); $form->addElement('hidden', '_referer', urlencode($_SERVER['REQUEST_URI'])); $cform->makeRemoteForm(array('url' => array('controller' => 'workflow_users', 'action' => 'add_user'), 'loading' => 'workflowManager.addUserLoading(request, ' . $workflow_group[$pk] . ')', 'complete' => 'workflowManager.onAddUser(request, ' . $workflow_group[$pk] . ')')); if ($form->validate()) { $fields = $wusers_model->fields(); if (in_array('cms_created', $fields)) { $wusers_model->cms_created = $model->now(); } if (in_array('cms_modified', $fields)) { $wusers_model->cms_modified = $model->now(); } // set the user id if it's applicable and available if (in_array('cms_modified_by_user', $fields)) { $wusers_model->cms_modified_by_user = isset($this->_auth) && is_object($this->_auth) ? $this->_auth->currentUserID() : 0; } $success = $form->process(array($cform, 'processForm')); } $headline = $model->getHeadline(); if (!empty($headline)) { if (is_array($headline)) { $workflow_group['_headline'] = ''; foreach ($headline as $row) { $workflow_group['_headline'] .= $workflow_group['_headline'] ? ' - ' : ''; $workflow_group['_headline'] .= $workflow_group[$row]; } } else { $workflow_group['_headline'] = $workflow_group[$headline]; } } else { $workflow_group['_headline'] = $workflow_group['cms_headline']; } $workflow_group['_users'] = array(); $wusers_model->reset(); $wusers_model->workflow_group_id = $workflow_group[$pk]; if ($wusers_model->find()) { while ($wusers_model->fetch()) { $user_model =& $wusers_model->getLink('user_id', 'cms_auth'); if ($user_model) { $user = $wusers_model->toArray(); $user['real_name'] = $user_model->real_name; $workflow_group['_users'][] = $user; } } } $workflow_group['asset'] = $this->name; $workflow_group['add_user_form'] = $form->toHTML(); $this->set($workflow_group); $workflows .= $this->render(array('action' => 'workflow_group', 'return' => true)); unset($form); unset($cform); } $this->set(array('asset' => $this->name, 'workflows' => $workflows)); $main_content = $this->render(array('return' => true)); $sidebar_content = $this->render(array('action' => 'workflow_description', 'return' => true)); } $this->renderLayout('default', $main_content, $sidebar_content); }
/** * Updates the current model's values * * Grabs the default model, validates the values (if they haven't * been yet), and calls the model's update() method * * @see NModel::update(); * @param $validated boolean Whether the values are prevalidated or not. * Defaults to false. * @access public * @return int The number of records affected (should be 1) */ function update($validated = false) { $model =& $this->getDefaultModel(); $pk = $model->primaryKey(); $fields = $model->fields(); if (!$model || !isset($this->params[$pk]) || $model->get($this->params[$pk])) { return false; } // create the form (this also does the validation) $cform = new ControllerForm($this, $model); $form =& $cform->getForm(); if (!$validated && !$form->validate()) { return false; } if (in_array('cms_modified', $fields)) { $model->cms_modified = $model->now(); } // set the user id if it's applicable and available if (in_array('cms_modified_by_user', $fields)) { $model->cms_modified_by_user = isset($this->_auth) && is_object($this->_auth) ? $this->_auth->currentUserID() : 0; } return $form->process(array($cform, 'processForm')); }