Commit 7415adbbbd44fec252901a7152ea0c062c372143

Authored by Brad Shuttleworth
1 parent 5210ab1d

no more need for this high-level page.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@5822 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 0 additions and 537 deletions
edit.php deleted
1   -<?php
2   -/**
3   - * $Id$
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   - * @version $Revision$
28   - * @author Brad Shuttleworth, Jam Warehouse (Pty) Ltd, South Africa
29   - *
30   - */
31   -
32   -require_once("config/dmsDefaults.php");
33   -require_once(KT_LIB_DIR . '/dispatcher.inc.php');
34   -
35   -require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
36   -require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc');
37   -require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
38   -require_once(KT_LIB_DIR . '/documentmanagement/DocumentLink.inc');
39   -
40   -require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
41   -
42   -require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc');
43   -require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
44   -require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
45   -
46   -require_once(KT_LIB_DIR . "/templating/kt3template.inc.php");
47   -
48   -// widget includes.
49   -require_once(KT_LIB_DIR . "/widgets/portlet.inc.php");
50   -require_once(KT_LIB_DIR . "/widgets/fieldsetDisplay.inc.php");
51   -require_once(KT_LIB_DIR . "/widgets/FieldsetDisplayRegistry.inc.php");
52   -require_once(KT_LIB_DIR . "/actions/documentaction.inc.php");
53   -require_once(KT_LIB_DIR . "/browse/browseutil.inc.php");
54   -
55   -require_once(KT_LIB_DIR . '/triggers/triggerregistry.inc.php');
56   -
57   -
58   -require_once(KT_LIB_DIR . '/permissions/permission.inc.php');
59   -require_once(KT_LIB_DIR . '/permissions/permissionutil.inc.php');
60   -
61   -class KTEditDocumentDispatcher extends KTStandardDispatcher {
62   - var $bAutomaticTransaction = true;
63   - var $oDocument = null;
64   - var $oFolder = null;
65   - var $sSection = "view_details";
66   - var $sHelpPage = 'ktcore/browse.html';
67   -
68   - function check() {
69   - if (!parent::check()) { return false; }
70   -
71   - $document_id = KTUtil::arrayGet($_REQUEST, 'fDocumentId');
72   - if (empty($document_id)) {
73   - $this->errorPage(_kt("No document specified for editing."));
74   - }
75   - $oDocument = Document::get($document_id);
76   - if (PEAR::isError($oDocument)) {
77   - $this->errorPage(_kt("Invalid Document."));
78   - }
79   -
80   - $this->oDocument = $oDocument;
81   - $oPerm = KTPermission::getByName('ktcore.permissions.write');
82   -
83   - if (!KTPermissionUtil::userHasPermissionOnItem($this->oUser, $oPerm, $this->oDocument)) { return false; }
84   -
85   - if ($this->oDocument->getIsCheckedOut()) {
86   - return false;
87   - }
88   -
89   -
90   - return true;
91   - }
92   -
93   - function KTEditDocumentDispatcher() {
94   - $this->aBreadcrumbs = array(
95   - array('action' => 'browse', 'name' => _kt('Browse')),
96   - );
97   - return parent::KTStandardDispatcher();
98   - }
99   -
100   - // FIXME identify the current location somehow.
101   - function addPortlets($currentaction = null) {
102   - $actions = KTDocumentActionUtil::getDocumentActionsForDocument($this->oDocument, $this->oUser);
103   - $oPortlet = new KTActionPortlet(_kt("Document Actions"));
104   - $oPortlet->setActions($actions, $currentaction);
105   -
106   - $this->oPage->addPortlet($oPortlet);
107   - }
108   -
109   - function addBreadcrumbs() {
110   -
111   - $aOptions = array(
112   - "documentaction" => "editDocument",
113   - "folderaction" => "browse",
114   - );
115   -
116   - $this->aBreadcrumbs = array_merge($this->aBreadcrumbs, KTBrowseUtil::breadcrumbsForDocument($this->oDocument, $aOptions));
117   -
118   - if (!is_null($this->oDocument)) {
119   - $this->oPage->setSecondaryTitle($this->oDocument->getName());
120   - }
121   -
122   - }
123   -
124   - function errorPage($errorMessage) {
125   - $this->handleOutput($errorMessage);
126   - exit(0);
127   - }
128   -
129   - function do_selectType() {
130   -
131   - $document_id = KTUtil::arrayGet($_REQUEST, 'fDocumentId');
132   - if (empty($document_id)) {
133   - $this->errorPage(_kt("No document specified for editing."));
134   - }
135   - $oDocument = Document::get($document_id);
136   - if (PEAR::isError($oDocument)) {
137   - $this->errorPage(_kt("Invalid Document."));
138   - }
139   -
140   - $this->oDocument = $oDocument;
141   - $this->addPortlets("Edit");
142   - $this->addBreadcrumbs();
143   - $this->oPage->setBreadcrumbDetails(_kt('Change Document Type'));
144   -
145   - $aDocTypes = DocumentType::getList();
146   -
147   -
148   - $aDocTypes = array();
149   - foreach (DocumentType::getList() as $oDocumentType) {
150   - if(!$oDocumentType->getDisabled()) {
151   - $aDocTypes[] = $oDocumentType;
152   - }
153   - }
154   -
155   - $oDocumentType = DocumentType::get($oDocument->getDocumentTypeID());
156   -
157   - $oTemplating =& KTTemplating::getSingleton();
158   - $oTemplate =& $oTemplating->loadTemplate("ktcore/document/change_type");
159   - $aTemplateData = array(
160   - 'context' => $this,
161   - 'document' => $oDocument,
162   - 'document_type' => $oDocumentType,
163   - 'doctypes' => $aDocTypes,
164   - );
165   - $oTemplate->setData($aTemplateData);
166   - return $oTemplate->render();
167   - }
168   -
169   - function do_changeType() {
170   - // FIXME this could do with more postTriggers, etc.
171   -
172   - /* The basic procedure is:
173   - *
174   - * 1. find out what fieldsets we _have_
175   - * 2. find out what fieldsets we _should_ have.
176   - * 3. actively delete fieldsets we need to lose.
177   - * 4. run the edit script.
178   - */
179   - $newType = KTUtil::arrayGet($_REQUEST, 'fDocType');
180   - $oType = DocumentType::get($newType);
181   - if (PEAR::isError($oType) || ($oType == false)) {
182   - $this->errorRedirectToMain(_kt("Invalid type selected."));
183   - }
184   -
185   - $this->addInfoMessage(sprintf(_kt('You have selected a different document type (%s). Please review the information below, and save changes to the metadata once you are satisfied.'), $oType->getName()));
186   - $_REQUEST['setType'] = $newType;
187   -
188   - return $this->do_main($newType);
189   - }
190   -
191   -
192   - // "standard document editing"
193   - function do_main($newType=false) {
194   - $this->oPage->setBreadcrumbDetails("edit");
195   -
196   - $document_id = KTUtil::arrayGet($_REQUEST, 'fDocumentId');
197   - if (empty($document_id)) {
198   - $this->errorPage(_kt("No document specified for editing."));
199   - }
200   - $oDocument = Document::get($document_id);
201   - if (PEAR::isError($oDocument)) {
202   - $this->errorPage(_kt("Invalid Document."));
203   - }
204   -
205   - $this->oDocument = $oDocument;
206   - $this->oFolder = Folder::get($oDocument->getFolderId()); // FIXME do we need to check that this is valid?
207   - $this->addBreadcrumbs();
208   - $this->addPortlets("Edit");
209   -
210   - $fieldsets = array();
211   -
212   - // we always have a generic.
213   - array_push($fieldsets, new GenericFieldsetDisplay());
214   - $fieldsetDisplayReg =& KTFieldsetDisplayRegistry::getSingleton();
215   -
216   - $activesets = KTFieldset::getGenericFieldsets();
217   - foreach ($activesets as $oFieldset) {
218   - $displayClass = $fieldsetDisplayReg->getHandler($oFieldset->getNamespace());
219   - array_push($fieldsets, new $displayClass($oFieldset));
220   - }
221   -
222   - // FIXME can we key this on fieldset namespace? or can we have duplicates?
223   - // now we get the other fieldsets, IF there is a valid doctype.
224   -
225   - // quick solution to the change issue...
226   -
227   - if ($newType !== false) {
228   - $activesets = KTFieldset::getForDocumentType($newType);
229   - } else {
230   - $activesets = KTFieldset::getForDocumentType($oDocument->getDocumentTypeID());
231   - }
232   - foreach ($activesets as $oFieldset) {
233   - $displayClass = $fieldsetDisplayReg->getHandler($oFieldset->getNamespace());
234   - array_push($fieldsets, new $displayClass($oFieldset));
235   - }
236   -
237   - $document_data = array();
238   - $document_data["document"] =& $this->oDocument;
239   -
240   - // we want to grab all the md for this doc, since its faster that way.
241   - $mdlist =& DocumentFieldLink::getByDocument($document_id);
242   -
243   - $field_values = array();
244   - foreach ($mdlist as $oFieldLink) {
245   - $field_values[$oFieldLink->getDocumentFieldID()] = $oFieldLink->getValue();
246   - }
247   -
248   - $document_data["field_values"] = $field_values;
249   -
250   - $oTemplating =& KTTemplating::getSingleton();
251   - $oTemplate =& $oTemplating->loadTemplate("kt3/document/edit");
252   - $aTemplateData = array(
253   - 'context' => $this,
254   - 'document' => $this->oDocument,
255   - 'document_data' => $document_data, // FIXME what do we need here?
256   - 'fieldsets' => $fieldsets,
257   - 'has_error' => false,
258   - 'newType' => $newType,
259   - );
260   - $oTemplate->setData($aTemplateData);
261   - return $oTemplate->render();
262   - }
263   -
264   - function do_update() {
265   - $document_id = KTUtil::arrayGet($_REQUEST, 'fDocumentId');
266   - if (empty($document_id)) {
267   - $this->errorPage(_kt("No document specified for editing."));
268   - }
269   -
270   - $oDocument = Document::get($document_id);
271   - if (PEAR::isError($oDocument)) {
272   - $this->errorPage(_kt("Invalid Document."));
273   - }
274   -
275   - $aErrorOptions = array(
276   - 'redirect_to' => array('main', sprintf('fDocumentId=%d', $oDocument->getId())),
277   - 'message' => _kt('No name given'),
278   - 'max_str_len' => 200,
279   - );
280   -
281   - $title = KTUtil::arrayGet($_REQUEST, 'generic_title');
282   - $title = $this->oValidator->validateString($title, $aErrorOptions);
283   -
284   - $newType = KTUtil::arrayGet($_REQUEST, 'newType');
285   - if ($newType !== null) {
286   - $oDT = DocumentType::get($newType);
287   - if (PEAR::isError($oDT) || ($oDT == false)) {
288   - $this->errorRedirectToMain(_kt('Invalid document type specified for change.'));
289   - }
290   - } else {
291   - $oDT = null;
292   - }
293   -
294   - $this->oDocument = $oDocument;
295   - $this->oFolder = Folder::get($oDocument->getFolderId()); // FIXME do we need to check that this is valid?
296   - $this->addBreadcrumbs();
297   - $this->addPortlets("Edit");
298   -
299   - $fieldsets = array();
300   -
301   - // FIXME handle true generic document DATA (e.g. title).
302   -
303   - // $activesets = KTFieldset::getGenericFieldsets();
304   - $activesets =& KTMetadataUtil::fieldsetsForDocument($oDocument);
305   - foreach ($activesets as $oFieldset) {
306   - array_push($fieldsets, $oFieldset);
307   - }
308   -
309   - if ($newType !== null) {
310   - $activesets = KTFieldset::getForDocumentType($newType);
311   - }
312   -
313   -
314   - // FIXME use array_merge
315   - foreach ($activesets as $oFieldset) {
316   - array_push($fieldsets, $oFieldset);
317   - }
318   -
319   - // erk. we need all the items that the document _does_ need, _and_ what they have,
320   - // _and_ what they don't ...
321   - // we want to grab all the md for this doc, since its faster that way.
322   - $current_md =& DocumentFieldLink::getByDocument($document_id);
323   -
324   - // to get all fields, we merge repeatedly from KTFieldset::get
325   -
326   - $field_values = array();
327   -
328   - // i hate conditionality
329   - $condy_fs = array();
330   - foreach ($fieldsets as $oFieldSet) {
331   - $fields =& $oFieldSet->getFields();
332   - $isRealConditional = ($oFieldSet->getIsConditional() && KTMetadataUtil::validateCompleteness($oFieldSet));
333   - $condy_fs[$oFieldSet->getId()] = $isRealConditional;
334   - // FIXME this doesn't handle multi-fieldset fields - are they possible/meaningful?
335   - foreach ($fields as $oField) {
336   - $field_values[$oField->getID()] = array($oField, null);
337   - }
338   - }
339   -
340   -
341   - foreach ($current_md as $oFieldLink) {
342   - // we mustn't copy "old" values for conditional fieldsets. it breaks unset-set fields.
343   - $oField = $field_values[$oFieldLink->getDocumentFieldID()][0];
344   - if (!$condy_fs[$oField->getParentFieldsetId()]) {
345   - $field_values[$oFieldLink->getDocumentFieldID()][1] = $oFieldLink->getValue();
346   - }
347   -
348   - }
349   -
350   -
351   - // now, we need the full set of items that this document could contain.
352   -
353   - // FIXME this DOES NOT WORK for date items.
354   - // FIXME that's fine - we don't use range items here...
355   - $expect_vals = KTUtil::arrayGet($_REQUEST,'kt_core_fieldsets_expect');
356   -
357   - foreach ($field_values as $key => $val) {
358   - $newVal = KTUtil::arrayGet($_REQUEST, 'metadata_' . $key, null);
359   - $wantVal = KTUtil::arrayGet($expect_vals, 'metadata_' . $key, false);
360   -
361   - // FIXME this leaves no way to check if someone has actually removed the item.
362   - // FIXME we probably want to _not_ set anything that could be set ... but then how do you
363   - // FIXME know about managed values ...
364   -
365   - if ($newVal !== null) {
366   - $field_values[$key][1] = $newVal; // need the items themselves.
367   - } else if ($wantVal !== false) {
368   - // we sent it out, delete it.
369   -
370   - unset($field_values[$key]);
371   - }
372   - }
373   -
374   -
375   - // finally, we need to pass through and remove null entries (sigh)
376   - // FIXME alternatively we could build a new set, but that might break stuff?
377   -
378   - $final_values = array();
379   - foreach ($field_values as $aMDPack) {
380   - if ($aMDPack[1] !== null) {
381   - $final_values[] = $aMDPack;
382   - }
383   - }
384   - $field_values = $final_values;
385   -
386   - // FIXME handle md versions.
387   - //return '<pre>' . print_r($field_values, true) . '</pre>';
388   - $this->startTransaction();
389   - $iPreviousMetadataVersionId = $oDocument->getMetadataVersionId();
390   - $oDocument->startNewMetadataVersion($this->oUser);
391   - if (PEAR::isError($res)) {
392   - $this->errorRedirectToMain('Unable to create a metadata version of the document.');
393   - }
394   -
395   - $oDocument->setName($title);
396   - $oDocument->setLastModifiedDate(getCurrentDateTime());
397   - $oDocument->setModifiedUserId($this->oUser->getId());
398   -
399   - // FIXME refactor this into documentutil.
400   - // document type changing semantics
401   - if ($newType != null) {
402   - $oldType = DocumentType::get($oDocument->getDocumentTypeID());
403   - $oDocument->setDocumentTypeID($newType);
404   -
405   - // we need to find fieldsets that _were_ in the old one, and _delete_ those.
406   - $for_delete = array();
407   -
408   - $oldFieldsets = KTFieldset::getForDocumentType($oldType);
409   - $newFieldsets = KTFieldset::getForDocumentType($newType);
410   -
411   - // prune from MDPack.
412   - foreach ($oldFieldsets as $oFieldset) {
413   - $old_fields = $oFieldset->getFields();
414   - foreach ($old_fields as $oField) {
415   - $for_delete[$oField->getId()] = 1;
416   - }
417   - }
418   -
419   - foreach ($newFieldsets as $oFieldset) {
420   - $new_fields = $oFieldset->getFields();
421   - foreach ($new_fields as $oField) {
422   - unset($for_delete[$oField->getId()]);
423   - }
424   - }
425   -
426   - $newPack = array();
427   - foreach ($field_values as $MDPack) {
428   - if (!array_key_exists($MDPack[0]->getId(), $for_delete)) {
429   - $newPack[] = $MDPack;
430   - }
431   - }
432   - $field_values = $newPack;
433   -
434   -
435   - //var_dump($field_values);
436   - //exit(0);
437   - }
438   -
439   - $oDocumentTransaction = & new DocumentTransaction($oDocument, 'update metadata.', 'ktcore.transactions.update');
440   -
441   - $res = $oDocumentTransaction->create();
442   - if (PEAR::isError($res)) {
443   - $this->errorRedirectToMain('Failed to create transaction.');
444   - }
445   -
446   - $res = $oDocument->update();
447   - if (PEAR::isError($res)) {
448   - $this->errorRedirectToMain('Failed to change basic details about the document..');
449   - }
450   - $res = KTDocumentUtil::saveMetadata($oDocument, $field_values);
451   -
452   - if (PEAR::isError($res)) {
453   - $this->rollbackTransaction();
454   -
455   - // right. we're rolled out. now we want to regenerate the page, + errors.
456   -
457   - // fixme use map (urgh)
458   - $new_field_values = array();
459   -
460   - foreach ($field_values as $aMDSet) {
461   - $new_field_values[$aMDSet[0]->getId()] = $aMDSet[1];
462   -
463   - }
464   -
465   - // we need to do the displayAdaption.
466   -
467   - $fieldsetDisplayReg =& KTFieldsetDisplayRegistry::getSingleton();
468   -
469   - $new_fieldsets = array();
470   - array_push($new_fieldsets, new GenericFieldsetDisplay());
471   - foreach ($fieldsets as $oFieldset) {
472   - $displayClass = $fieldsetDisplayReg->getHandler($oFieldset->getNamespace());
473   - $oDisplayFieldset = new $displayClass($oFieldset);
474   - if (isset($res->aFailed['fieldset'][$oFieldset->getId()])) {
475   - $oDisplayFieldset->setError($res->aFailed['fieldset'][$oFieldset->getId()]);
476   - }
477   - $new_fieldsets[$oFieldset->getId()] = $oDisplayFieldset;
478   -
479   - }
480   -
481   - $document_data['document'] = $oDocument;
482   - $document_data["field_values"] = $new_field_values;
483   -
484   - //print '<pre>' . print_r($document_data['field_values'], true) . '</pre>';
485   -
486   - $document_data['errors'] = $res->aFailed['field'];
487   -
488   - $this->addErrorMessage(_kt('Validation failed. The values below correspond to your attempt to change the metadata on this item, and are not necessarily the values saved for the document. The validation errors are shown next to the metadata that failed validation. Select Cancel to return to the saved values.'));
489   -
490   - $oTemplating =& KTTemplating::getSingleton();
491   - $oTemplate =& $oTemplating->loadTemplate("kt3/document/edit");
492   - $aTemplateData = array(
493   - 'context' => $this,
494   - 'document' => $this->oDocument,
495   - 'document_data' => $document_data, // FIXME what do we need here?
496   - 'fieldsets' => $new_fieldsets,
497   - 'has_error' => true,
498   - );
499   - $oTemplate->setData($aTemplateData);
500   - return $oTemplate->render();
501   -
502   -
503   - } else {
504   -
505   - // post-triggers.
506   - $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
507   - $aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate');
508   -
509   - foreach ($aTriggers as $aTrigger) {
510   - $sTrigger = $aTrigger[0];
511   - $oTrigger = new $sTrigger;
512   - $aInfo = array(
513   - "document" => $oDocument,
514   - );
515   - $oTrigger->setInfo($aInfo);
516   - $ret = $oTrigger->postValidate();
517   - }
518   -
519   - $this->commitTransaction();
520   -
521   - // now we need to say we're ok.
522   - // this involves a redirect to view, with a message.
523   - // FIXME do not hard-code URLs
524   -
525   - redirect(KTBrowseUtil::getUrlForDocument($document_id));
526   - }
527   -
528   -
529   -
530   - }
531   -
532   -}
533   -$d =& new KTEditDocumentDispatcher;
534   -$d->dispatch();
535   -
536   -
537   -?>