documentFieldsv2.php 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
<?php

/**
 * $Id$
 *
 * KnowledgeTree Community Edition
 * Document Management Made Simple
 * Copyright (C) 2008, 2009, 2010 KnowledgeTree Inc.
 * 
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
 * California 94120-7775, or email info@knowledgetree.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * KnowledgeTree" logo and retain the original copyright notice. If the display of the
 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
 * must display the words "Powered by KnowledgeTree" and retain the original
 * copyright notice.
 * Contributor( s): ______________________________________
 *
 */

require_once(KT_LIB_DIR . '/dispatcher.inc.php');
require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
require_once(KT_LIB_DIR . '/widgets/forms.inc.php');
require_once(KT_LIB_DIR . '/plugins/pluginutil.inc.php');

class KTDocumentFieldDispatcher extends KTAdminDispatcher {
    var $bAutomaticTransaction = true;
	var $bHaveConditional = null;
    var $sHelpPage = 'ktcore/admin/document fieldsets.html';

    function predispatch() {
        $this->aBreadcrumbs[] = array('url' => $_SERVER['PHP_SELF'], 'name' => _kt('Document Field Management'));
        $this->persistParams(array('fFieldsetId'));

        $this->oFieldset = KTFieldset::get(KTUtil::arrayGet($_REQUEST, 'fFieldsetId'));
        if (PEAR::isError($this->oFieldset)) {
            $this->oFieldset = null;
            unset($_REQUEST['fFieldset']); // prevent further attacks.
        } else {
            $this->aBreadcrumbs[] = array('url' => KTUtil::addQueryStringSelf($this->meldPersistQuery("","edit")), 'name' => $this->oFieldset->getName());
        }
        $this->bHaveConditional = KTPluginUtil::pluginIsActive('ktextra.conditionalmetadata.plugin');
    }


    function do_main () {
        $oTemplate =& $this->oValidator->validateTemplate('ktcore/metadata/admin/list');

        $oTemplate->setData(array(
		    'context' => $this,
            'fieldsets' => KTFieldset::getList("disabled != true AND namespace != 'tagcloud'"),
        ));
        return $oTemplate;
    }

    function form_create() {
        $oForm = new KTForm;
        $oForm->setOptions(array(
            'identifier' => 'ktcore.fieldsets.create',
            'label' => _kt("Create New Fieldset"),
            'submit_label' => _kt('Create Fieldset'),
            'cancel_action' => 'main',
            'fail_action' => 'newfieldset',
            'action' => 'create',
            'context' => $this,
        ));


        // construct the widget set.
        // we use a slight variation here, because "type" is only present in certain circumstances.
        $widgets = array(
            array('ktcore.widgets.string',array(
                'label' => _kt("Fieldset Name"),
                'name' => 'name',
                'required' => true,
                'description' => _kt("Each fieldset needs a unique name."),
            )),
            array('ktcore.widgets.text',array(
                'label' => _kt("Description"),
                'name' => 'description',
                'required' => true,
                'description' => _kt("In order to ensure that the data that users enter is useful, it is essential that you provide a good example."),
            )),
        );
        if ($this->bHaveConditional) {

            // FIXME get this from some external source.
            $type_vocab = array(
                'normal' => _kt("Normal"),
                'conditional' => _kt("Conditional"),
            );

            $widgets[] = array('ktcore.widgets.selection', array(
                'label' => _kt("Fieldset Type"),
                'use_simple' => false,
                'description' => _kt("It is possible to create different types of fieldsets.  The most common kind is a \"normal\" fieldset, which can be configured to have different kinds of fields.  The administrator may have installed additional plugins which provide different types of fieldsets."),
                'important_description' => _kt('Note that it is not possible to convert between different types of fieldsets, so please choose carefully.'),
                'name' => 'fieldset_type',
                'required' => true,
                'value' => 'normal',
                'vocab' => $type_vocab,
            ));
        }

        $widgets[] = array('ktcore.widgets.boolean',array(
                'label' => _kt("Generic"),
                'name' => 'generic',
                'description' => _kt("A generic fieldset is one that is available for every document by default. These fieldsets will be available for users to edit and add for every document in the document management system."),
            ));

        $oForm->setWidgets($widgets);

        // similarly, we construct validators here.
        $validators = array(
            array('ktcore.validators.string', array(
                'test' => 'name',
                'output' => 'name',
            )),
            array('ktcore.validators.string', array(
                'test' => 'description',
                'output' => 'description',
            )),
            array('ktcore.validators.boolean', array(
                'test' => 'generic',
                'output' => 'generic',
            )),
        );

        if ($this->bHaveConditional) {
            $validators[] = array('ktcore.validators.string', array(
                'test' => 'fieldset_type',
                'output' => 'fieldset_type',
            ));
        }

        $oForm->setValidators($validators);

        return $oForm;
    }

    function do_newfieldset() {
        $this->oPage->setBreadcrumbDetails(_kt("Create New Fieldset"));
        $oForm = $this->form_create();

        return $oForm->render();
    }

    function do_create() {
        $oForm = $this->form_create();
        $res = $oForm->validate();

        $data = $res['results'];
        $errors = $res['errors'];
        $extra_errors = array();

        if (!empty($data['name'])) {
            $oFieldset = KTFieldset::getByName($data['name']);
            if (!PEAR::isError($oFieldset)) {
                // means we're looking at an existing name
                $extra_errors['name'] = _kt("There is already a fieldset with that name.");
            }
        }

        $is_conditional = false;
        // FIXME this is inelegant.  get it from somewhere else.
        if ($this->bHaveConditional && ($data['fieldset_type'] == 'conditional')) {
            $is_conditional = true;
        }


        if (!empty($errors) || !empty($extra_errors)) {
            return $oForm->handleError(null, $extra_errors);
        }

        // we also need a namespace.
        $temp_name = $data['name'];
        $namespace = KTUtil::nameToLocalNamespace('fieldsets', $temp_name);
        $oOldFieldset = KTFieldset::getByNamespace($namespace);

        while (!PEAR::isError($oOldFieldset)) {
            $temp_name .= '_';
            $namespace = KTUtil::nameToLocalNamespace('fieldsets', $temp_name);
            $oOldFieldset = KTFieldset::getByNamespace($namespace);
        }

        // we now know its a non-conflicting one.
        // FIXME handle conditional fieldsets, which should be ... a different object.
        $oFieldset = KTFieldset::createFromArray(array(
            "name" => $data['name'],
	    	"description" => $data['description'],
            "namespace" => $namespace,
            "mandatory" => false,       // FIXME deprecated
	    	"isConditional" => $is_conditional,   // handle this
            "isGeneric" => $data['generic'],
            "isComplete" => false,
            "isComplex" => false,
            "isSystem" => false,
        ));
        if (PEAR::isError($oFieldset)) {
            return $oForm->handleError(sprintf(_kt("Failed to create fieldset: %s"), $oFieldset->getMessage()));
        }

        $this->successRedirectTo('edit',_kt("Fieldset created."), sprintf('fFieldsetId=%d', $oFieldset->getId()));
    }

	function getTypesForFieldset($oFieldset) {
	    global $default;
	    if ($oFieldset->getIsGeneric()) {
		    return _kt('All types use this generic fieldset.');
		}

	    $types = $oFieldset->getAssociatedTypes();
		if (PEAR::isError($types)) {
		    $default->log->debug('Fieldsets admin: Error retrieving list of associated document types.');
		    return _kt('Error retrieving list of types.');
		}
		if (empty($types)) {
		    return _kt('None');
		}

		$aNames = array();
		foreach ($types as $oType) {
		    if (!PEAR::isError($oType)) {
    		    $aNames[] = $oType->getName();
    		}else{
    		    $default->log->debug('Fieldsets admin: Document type gives error: '.$oType->getMessage());
    		}
		}

		$list = implode(', ', $aNames);
		$length = mb_strlen($list);

		if($length < 50){
		    return $list;
		}
		$default->log->debug('Fieldsets admin: wrapping the list of doc types from length '.$length);

		// Wrap the list to 50 characters per line
		$wrapList = '';
		$cut = 0;
		while ($length > 50 && $cut !== false){
		    $cut = strpos($list, ' ', 50);
		    $wrapList .= mb_strcut($list, 0, $cut);
		    $wrapList .= '<br />';
		    $list = mb_strcut($list, $cut);
		    $length = mb_strlen($list);
		}
		$wrapList .= $list;

		return $wrapList;
	}


    function do_edit() {
        // here we engage in some major evil.
        // we check for the subevent var
        // and subdispatch if appropriate.
        //
        // saves a little code-duplication (actually, a lot of code-duplication)

        // FIXME this is essentially a stub for the fieldset-delegation code.
        if ($this->oFieldset->getIsConditional()) {
            require_once('fieldsets/conditional.inc.php');
            $oSubDispatcher = new ConditionalFieldsetManagementDispatcher;
        } else {
            require_once('fieldsets/basic.inc.php');
            $oSubDispatcher = new BasicFieldsetManagementDispatcher;
        }

        $subevent_var = 'fieldset_action';
        $subevent = KTUtil::arrayGet($_REQUEST, $subevent_var);
        if (!empty($subevent)) {
            // do nothing, since this will handle everything
            $this_url = KTUtil::addQueryStringSelf($this->meldPersistQuery("","edit"));
            $oSubDispatcher->redispatch($subevent_var, null, $this, $this_url);
            exit(0);
        } else {
            // what we want is the "additional info" section
            $additional = $oSubDispatcher->describe_fieldset($this->oFieldset);
        }

        $oTemplate =& $this->oValidator->validateTemplate('ktcore/metadata/admin/edit');
        $oTemplate->setData(array(
            'context' => $this,
            'fieldset_name' => $this->oFieldset->getName(),
            'additional' => $additional,
        ));
        return $oTemplate->render();
    }

    function do_delete() {
        $this->startTransaction();

        // check if fieldset is associated with a document type - remove association
        $types = $this->oFieldset->getAssociatedTypes();
        $sFieldSetId = $this->oFieldset->getId();
        if(!PEAR::isError($types) AND !empty($types)){
            foreach($types as $oType){
                $res = KTMetadataUtil::removeSetsFromDocumentType($oType, $sFieldSetId);
            }
        }

        $res = $this->oFieldset->delete('true');
        $this->oValidator->notErrorFalse($res, array(
            'redirect_to' => array('main', ''),
            'message' => _kt('Could not delete fieldset'),
        ));
        $this->successRedirectToMain(_kt('Fieldset deleted'));
    }

    function form_edit() {
        $oForm = new KTForm;
        $oForm->setOptions(array(
            'identifier' => 'ktcore.fieldsets.edit',
            'label' => _kt("Change Fieldset Details"),
            'submit_label' => _kt('Update Fieldset'),
            'cancel_action' => 'edit',
            'fail_action' => 'editfieldset',
            'action' => 'savefieldset',
            'context' => $this,
        ));


        // construct the widget set.
        // we use a slight variation here, because "type" is only present in certain circumstances.
        $widgets = array(
            array('ktcore.widgets.string',array(
                'label' => _kt("Fieldset Name"),
                'name' => 'name',
                'required' => true,
                'description' => _kt("Each fieldset needs a unique name."),
                'value' => sanitizeForHTML($this->oFieldset->getName()),
            )),
            array('ktcore.widgets.text',array(
                'label' => _kt("Description"),
                'name' => 'description',
                'required' => true,
                'description' => _kt("In order to ensure that the data that users enter is useful, it is essential that you provide a good example."),
                'value' => sanitizeForHTML($this->oFieldset->getDescription()),
            )),
        );

        $widgets[] = array('ktcore.widgets.boolean',array(
                'label' => _kt("Generic"),
                'name' => 'generic',
                'description' => _kt("A generic fieldset is one that is available for every document by default. These fieldsets will be available for users to edit and add for every document in the document management system."),
                'value' => $this->oFieldset->getIsGeneric(),
            ));

        $oForm->setWidgets($widgets);

        // similarly, we construct validators here.
        $validators = array(
            array('ktcore.validators.string', array(
                'test' => 'name',
                'output' => 'name',
            )),
            array('ktcore.validators.string', array(
                'test' => 'description',
                'output' => 'description',
            )),
            array('ktcore.validators.boolean', array(
                'test' => 'generic',
                'output' => 'generic',
            )),
        );

        $oForm->setValidators($validators);

        return $oForm;
    }

    function do_editfieldset() {
        $oForm = $this->form_edit();
        $this->oPage->setBreadcrumbDetails(_kt('edit fieldset'));
        return $oForm->renderPage(_kt("Edit Fieldset"));
    }

    function do_savefieldset() {
        $oForm = $this->form_edit();
        $res = $oForm->validate();

        $data = $res['results'];
        $errors = $res['errors'];
        $extra_errors = array();

        // check that the fieldset name either hasn't changed, or doesn't exist.
        if ($data['name'] != $this->oFieldset->getName()) {
            $oOldFieldset = KTFieldset::getByName($data['name']);
            // If the fieldset exists throw an error. Mysql doesn't distinguish between Ž and e so check the names are different in php.
            if (!PEAR::isError($oOldFieldset) && $oOldFieldset->getName() == $data['name']) {
                $extra_errors['name'][] = _kt("A fieldset with that name already exists.");
            }
        }

        if (!empty($errors) || !empty($extra_errors)) {
            return $oForm->handleError(null, $extra_errors);
        }

        $this->startTransaction();

        $this->oFieldset->setName($data['name']);
        $this->oFieldset->setDescription($data['description']);
        $bGeneric = $data['generic'];
        if ($bGeneric != $this->oFieldset->getIsGeneric() && $bGeneric == true) {
            // delink it from all doctypes.
            $aTypes = $this->oFieldset->getAssociatedTypes();
            foreach ($aTypes as $oType) {
                $res = KTMetadataUtil::removeSetsFromDocumentType($oType, $this->oFieldset->getId());
                if (PEAR::isError($res)) {
                    $this->errorRedirectTo('edit', _kt('Could not save fieldset changes'));
                    exit(0);
                }
            }
        }

        $this->oFieldset->setIsGeneric($data['generic']);

        $res = $this->oFieldset->update();
        if (PEAR::isError($res)) {
            $this->errorRedirectTo('edit', _kt('Could not save fieldset changes'));
            exit(0);
        }

        return $this->successRedirectTo('edit', _kt("Fieldset details updated."));
    }
}

?>