Commit a120ad25a7be4020418b0b7af71b48c0b00c9db3

Authored by Brad Shuttleworth
1 parent 7c02e690

Edit needs to have its own dispatcher.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@5818 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 193 additions and 0 deletions
plugins/ktcore/document/edit.php 0 → 100644
  1 +<?php
  2 +
  3 +/**
  4 + *
  5 + * The contents of this file are subject to the KnowledgeTree Public
  6 + * License Version 1.1 ("License"); You may not use this file except in
  7 + * compliance with the License. You may obtain a copy of the License at
  8 + * http://www.ktdms.com/KPL
  9 + *
  10 + * Software distributed under the License is distributed on an "AS IS"
  11 + * basis,
  12 + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13 + * for the specific language governing rights and limitations under the
  14 + * License.
  15 + *
  16 + * The Original Code is: KnowledgeTree Open Source
  17 + *
  18 + * The Initial Developer of the Original Code is The Jam Warehouse Software
  19 + * (Pty) Ltd, trading as KnowledgeTree.
  20 + * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
  21 + * (C) 2006 The Jam Warehouse Software (Pty) Ltd;
  22 + * All Rights Reserved.
  23 + *
  24 + */
  25 +
  26 +
  27 +require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
  28 +require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc');
  29 +require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
  30 +require_once(KT_LIB_DIR . '/documentmanagement/DocumentLink.inc');
  31 +require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
  32 +require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc');
  33 +require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
  34 +require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
  35 +require_once(KT_LIB_DIR . '/triggers/triggerregistry.inc.php');
  36 +require_once(KT_LIB_DIR . '/permissions/permission.inc.php');
  37 +require_once(KT_LIB_DIR . '/permissions/permissionutil.inc.php');
  38 +
  39 +require_once(KT_LIB_DIR . "/widgets/forms.inc.php");
  40 +require_once(KT_LIB_DIR . "/metadata/fieldsetregistry.inc.php");
  41 +
  42 +// {{{ KTDocumentEditAction
  43 +class KTDocumentEditAction extends KTDocumentAction {
  44 + var $sName = 'ktcore.actions.document.edit';
  45 +
  46 + var $_sShowPermission = "ktcore.permissions.write";
  47 + var $_bMutator = true;
  48 +
  49 + function getInfo() {
  50 + if ($this->oDocument->getIsCheckedOut()) {
  51 + return null;
  52 + }
  53 + return parent::getInfo();
  54 + }
  55 +
  56 + function getDisplayName() {
  57 + return _kt('Edit Metadata');
  58 + }
  59 +
  60 + function form_edit() {
  61 + $oForm = new KTForm;
  62 + $oForm->setOptions(array(
  63 + 'label' => _kt('Edit Metadata'),
  64 + 'submit_label' => _kt('Update Document'),
  65 + 'action' => 'update',
  66 + 'fail_action' => 'main',
  67 + 'cancel_url' => KTBrowseUtil::getUrlForDocument($this->oDocument),
  68 + 'context' => &$this,
  69 + 'extraargs' => $this->meldPersistQuery("","",true),
  70 + ));
  71 +
  72 +
  73 + $oFReg =& KTFieldsetRegistry::getSingleton();
  74 +
  75 + $doctypeid = $this->oDocument->getDocumentTypeID();
  76 +
  77 + $widgets = array(
  78 + array('ktcore.widgets.string', array(
  79 + 'label' => _kt("Document Title"),
  80 + 'description' => _kt("The document title is used as the main name of a document throughout KnowledgeTree."),
  81 + 'name' => 'document_title',
  82 + 'required' => true,
  83 + 'value' => $this->oDocument->getName(),
  84 + )),
  85 + );
  86 + $validators = array(
  87 + array('ktcore.validators.string',array(
  88 + 'test' => 'document_title',
  89 + 'output' => 'document_title',
  90 + )),
  91 + );
  92 + $fieldsets = (array) KTMetadataUtil::fieldsetsForDocument($this->oDocument);
  93 +
  94 + foreach ($fieldsets as $oFieldset) {
  95 + $widgets = kt_array_merge($widgets, $oFReg->widgetsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument));
  96 + $validators = kt_array_merge($validators, $oFReg->validatorsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument));
  97 + }
  98 +
  99 + $oForm->setWidgets($widgets);
  100 + $oForm->setValidators($validators);
  101 +
  102 + return $oForm;
  103 + }
  104 +
  105 + function do_main() {
  106 + $this->addErrorMessage("Doctype changing regressed.");
  107 +
  108 + $this->oPage->setBreadcrumbDetails("Edit Metadata");
  109 +
  110 + $oTemplate = $this->oValidator->validateTemplate('ktcore/document/edit');
  111 +
  112 + $oForm = $this->form_edit();
  113 +
  114 + $oTemplate->setData(array(
  115 + 'context' => $this,
  116 + 'form' => $oForm,
  117 + 'document' => $this->oDocument,
  118 + ));
  119 + return $oTemplate->render();
  120 + }
  121 +
  122 + function do_update() {
  123 + $oForm = $this->form_edit();
  124 +
  125 + $res = $oForm->validate();
  126 + if (!empty($res['errors'])) {
  127 + return $oForm->handleError();
  128 + }
  129 +
  130 + $data = $res['results'];
  131 +
  132 + // we need to format these in MDPack format
  133 + // which is a little archaic:
  134 + //
  135 + // array(
  136 + // array($oField, $sValue),
  137 + // array($oField, $sValue),
  138 + // array($oField, $sValue),
  139 + // );
  140 + //
  141 + // we do this the "easy" way.
  142 +
  143 + $fieldsets = KTMetadataUtil::fieldsetsForDocument($this->oDocument);
  144 +
  145 + $MDPack = array();
  146 + foreach ($fieldsets as $oFieldset) {
  147 + $fields = $oFieldset->getFields();
  148 + $values = (array) KTUtil::arrayGet($data, 'fieldset_' . $oFieldset->getId());
  149 + foreach ($fields as $oField) {
  150 + $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId());
  151 + $MDPack[] = array(
  152 + $oField,
  153 + $val
  154 + );
  155 + }
  156 + }
  157 +
  158 + $this->startTransaction();
  159 + $this->oDocument->setName($data['document_title']);
  160 + $res = $this->oDocument->update();
  161 + if (PEAR::isError($res)) {
  162 + $oForm->handleError(sprintf(_kt("Unexpected failure to update document title: %s"), $res->getMessage()));
  163 + }
  164 + $core_res = KTDocumentUtil::saveMetadata($this->oDocument, $MDPack);
  165 + if (PEAR::isError($core_res)) {
  166 + $oForm->handleError(_kt("Unexpected validation failure."));
  167 + }
  168 +
  169 + // post-triggers.
  170 + $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
  171 + $aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate');
  172 +
  173 + foreach ($aTriggers as $aTrigger) {
  174 + $sTrigger = $aTrigger[0];
  175 + $oTrigger = new $sTrigger;
  176 + $aInfo = array(
  177 + "document" => $this->oDocument,
  178 + );
  179 + $oTrigger->setInfo($aInfo);
  180 + $ret = $oTrigger->postValidate();
  181 + }
  182 +
  183 + $this->commitTransaction();
  184 +
  185 + redirect(KTBrowseUtil::getUrlForDocument($this->oDocument->getId()));
  186 + exit(0);
  187 +
  188 + }
  189 +
  190 +}
  191 +// }}}
  192 +
  193 +?>
... ...