basic.inc.php 34.9 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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
<?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');
require_once(KT_LIB_DIR . "/documentmanagement/MDTree.inc");

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

    function predispatch() {
        $this->persistParams(array('fFieldId'));
        $this->oFieldset = KTFieldset::get(KTUtil::arrayGet($_REQUEST, 'fFieldsetId'));
        if (PEAR::isError($this->oFieldset)) {
            $this->oFieldset = null;
            unset($_REQUEST['fFieldsetId']); // prevent further attacks.
        }
        $this->oField = DocumentField::get(KTUtil::arrayGet($_REQUEST, 'fFieldId'));
        if (PEAR::isError($this->oField)) {
            $this->oField = null;
            unset($_REQUEST['fFieldId']); // prevent further attacks.
        } else {
            $this->aBreadcrumbs[] = array('url' => KTUtil::addQueryStringSelf($this->meldPersistQuery("","managefield")), 'name' => $this->oField->getName());
        }
    }

    // API:  this provides information about the fieldset, including which actions are available.
    function describe_fieldset($oFieldset) {
        $this->persistParams(array('fFieldsetId','action'));
        $oTemplate =& $this->oValidator->validateTemplate('ktcore/metadata/admin/basic_overview');
        $oTemplate->setData(array(
            'context' => $this,
            'fields' => $oFieldset->getFields(),
        ));
        return $oTemplate->render();
    }

    function do_main () {
        return _kt("Something very unexpected happened.");
    }

    function getFieldTypeVocab() {
        $types = array(
            'normal' => _kt("Normal (String)"),
            'lookup' => _kt("Lookup"),
            'tree' => _kt("Tree"),
            'largetextbox' => _kt("Large Text"),
            'date' => _kt("Date"),
        );
        return $types;
    }

    function getDefaultType() {
        return 'normal';
    }

    function form_newfield() {
        $this->oPage->setBreadcrumbDetails(_kt('add field'));

        $oForm = new KTForm;
        $oForm->setOptions(array(
            'identifier' => 'ktcore.fieldsets.basic.field.create',
            'label' => _kt("Add New Field"),
            'submit_label' => _kt('Add Field'),
            'cancel_url' => $this->sParentUrl,
            'fail_action' => 'newfield',
            'action' => 'createfield',
            'context' => $this,
        ));

        $type_vocab = $this->getFieldTypeVocab();

        $oForm->setWidgets(array(
            array('ktcore.widgets.string',array(
                'label' => _kt("Field Name"),
                'name' => 'name',
                'required' => true,
                'description' => _kt("Within a given fieldset, each field needs a unique name."),
            )),
            array('ktcore.widgets.text',array(
                'label' => _kt("Description"),
                'name' => 'description',
                'required' => true,
                'description' => _kt("A good description can be the difference between useful metadata and poor metadata.  At the same time, overly long descriptions are far less valuable than concise ones."),
            )),
            array('ktcore.widgets.selection', array(
                'label' => _kt('Field Type'),
                'name' => 'field_type',
                'vocab' => $this->getFieldTypeVocab(),
                'description' => _kt("Different types of fields may be available, depending on the system."),
                'required' => true,
                'value' => $this->getDefaultType(),
            )),
            array('ktcore.widgets.boolean',array(
                'label' => _kt("Required"),
                'name' => 'required',
                'description' => _kt("Required fields must be filled in, or the adding process will be rejected."),
            )),

        ));

        $oForm->setValidators(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' => 'required',
                'output' => 'required',
            )),
            array('ktcore.validators.string', array(
                'test' => 'field_type',
                'output' => 'field_type',
            )),
        ));

        return $oForm;
    }

    function do_newfield() {
        $oForm = $this->form_newfield();

        return $oForm->render();
    }

    function do_createfield() {
        $oForm = $this->form_newfield();
        $res = $oForm->validate();

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

        $oField = DocumentField::getByFieldsetAndName($this->oFieldset, $data['name']);
        if (!PEAR::isError($oField)) {
            $extra_errors['name'] = _kt("A field with that name already exists in this fieldset.");
        }

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

        $lookup = false;
        $tree = false;

        //$largeField = false;

        $DataType = 'STRING';
        
        if ($data['field_type'] == 'lookup') {
            $lookup = true;
        } else if ($data['field_type'] == 'tree') {
            $lookup = true;
            $tree = true;
        } else if ($data['field_type'] == 'largetextbox') {
        	//$largeField = true;        	
        	$DataType = 'LARGE TEXT';
        } else if ($data['field_type'] == 'date') {
        	//$largeField = true;        	
        	$DataType = 'DATE';
        }
        
        $oField = DocumentField::createFromArray(array(
            'Name' => $data['name'],
            'Description' => $data['description'],
            'DataType' => $DataType,
            'IsGeneric' => false,
            'HasLookup' => $lookup,
            'HasLookupTree' => $tree,
            'ParentFieldset' => $this->oFieldset->getId(),
            'IsMandatory' => $data['required'],
        ));

        if (PEAR::isError($oField)) {
            return $oForm->handleError(sprintf(_kt("Unable to create field: %s"), $oField->getMessage()));
        }

        $this->successRedirectTo('managefield', _kt("Field created."), sprintf('fFieldId=%d', $oField->getId()));
    }

    function form_editfield($oField) {
        $oForm = new KTForm;
        $oForm->setOptions(array(
            'identifier' => 'ktcore.fieldsets.basic.field.edit',
            'label' => _kt("Edit Field"),
            'submit_label' => _kt('Update Field'),
            'cancel_url' => $this->sParentUrl,
            'fail_action' => 'managefield',
            'action' => 'updatefield',
            'context' => $this,
        ));

        $oForm->setWidgets(array(
            array('ktcore.widgets.string',array(
                'label' => _kt("Field Name"),
                'name' => 'name',
                'value' => sanitizeForHTML($oField->getName()),
                'required' => true,
                'description' => _kt("Within a given fieldset, each field needs a unique name."),
            )),
            array('ktcore.widgets.text',array(
                'label' => _kt("Description"),
                'name' => 'description',
                'value' => sanitizeForHTML($oField->getDescription()),
                'required' => true,
                'description' => _kt("A good description can be the difference between useful metadata and poor metadata.  At the same time, overly long descriptions are far less valuable than concise ones."),
            )),
            array('ktcore.widgets.boolean',array(
                'label' => _kt("Required"),
                'value' => $oField->getIsMandatory(),
                'name' => 'required',
                'description' => _kt("Required fields must be filled in, or the adding process will be rejected."),
            )),

        ));

        $oForm->setValidators(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' => 'required',
                'output' => 'required',
            )),
        ));

        return $oForm;
    }

    function do_managefield() {
        $oTemplate = $this->oValidator->validateTemplate('ktcore/metadata/admin/manage_field');

        $oTemplate->setData(array(
            'context' => $this,
            'field_name' => $this->oField->getName(),
            'field_id' => $this->oField->getId(),
            'form' => $this->form_editfield($this->oField),
            'field' => $this->oField,
        ));
        return $oTemplate->render();
    }
    
    function do_updatelargetextoptions() {

        $this->oField = DocumentField::get(KTUtil::arrayGet($_REQUEST, 'fFieldId'));
    	
        $oValues = KTUtil::arrayGet($_REQUEST, 'largefield');

        if($oValues['size'] != "")
    		$this->oField->setMaxLength($oValues['size']);
    	
    	$this->oField->setIsHTML($oValues['html']);
    	//$this->oField->setIsHTML(true);
    	
    	//$oValue = $this->oField->_fieldValues();
        //$this->successRedirectTo('managefield',_kt("Field updated. -> ") . $this->oField->getMaxLength() . " -> " . $oValue['max_length']);
        
        //$this->successRedirectTo('managefield',_kt("Field updated. -> ") . $oValues['html']);
        
        $res = $this->oField->update();
        if (PEAR::isError($res)) {
            return $oForm->handleError(sprintf(_kt("Failed to update field: %s"), $res->getMessage()));
        }

        $this->successRedirectTo('managefield',_kt("Field updated."));
    }

    function do_updatefield() {
        $oForm = $this->form_editfield($this->oField);
        $res = $oForm->validate();
        $data = $res['results'];
        $errors = $res['errors'];
        $extra_errors = array();

        // check that the field name either hasn't changed, or doesn't exist.
        if ($data['name'] != $this->oField->getName()) {
            $oOldField = DocumentField::getByFieldsetAndName($this->oFieldset, $data['name']);
            // If the field exists throw an error. Mysql doesn't distinguish between รฉ and e so check the names are different in php.
            if (!PEAR::isError($oOldField) && $oOldField->getName() == $data['name']) {
                $extra_errors['name'] = _kt("That name is already in use in this fieldset.  Please specify a unique name.");
            }
        }

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

        $this->oField->setName($data['name']);
        $this->oField->setDescription($data['description']);
        $this->oField->setIsMandatory($data['required']);

        $res = $this->oField->update();
        if (PEAR::isError($res)) {
            return $oForm->handleError(sprintf(_kt("Failed to update field: %s"), $res->getMessage()));
        }

        $this->successRedirectTo('managefield',_kt("Field updated."));
    }

    function form_addlookups() {
       $oForm = new KTForm;
        $oForm->setOptions(array(
            'identifier' => 'ktcore.fieldsets.basic.field.addlookup',
            'label' => _kt("Add Lookup Values"),
            'submit_label' => _kt('Add Lookups'),
            'cancel_action' => 'managefield',
            'fail_action' => 'addlookupvalues',
            'action' => 'createlookupvalues',
            'context' => $this,
        ));

        $oForm->setWidgets(array(
            array('ktcore.widgets.text',array(
                'label' => _kt("Lookup Values"),
                'name' => 'lookups',
                'required' => true,
                'description' => _kt("Lookup values are what a user can select from a dropdown.  These pre-created lookup values are useful, since they help you keep the metadata in the system organised."),
                'important_description' => _kt("Please enter the lookup values you wish to add, one per line."),
            )),
        ));

        $oForm->setValidators(array(
            array('ktcore.validators.string', array(
                'test' => 'lookups',
                'output' => 'lookups',
                'max_length' => 9999,
            )),
        ));

        return $oForm;
    }

    function do_addlookupvalues() {
        $this->oPage->setBreadcrumbDetails(_kt('add lookup values'));

        $oForm = $this->form_addlookups();
        return $oForm->render();
    }

    function do_createlookupvalues() {
        $oForm = $this->form_addlookups();
        $res = $oForm->validate();
        $data = $res['results'];
        $errors = $res['errors'];
        $extra_errors = array();


        $failed = array();
        $lookups = array();

        $raw_lookups = $data['lookups'];
        $lookup_candidates = explode("\n", $raw_lookups);
        foreach ($lookup_candidates as $candidate) {
            $name = trim($candidate);

            if (empty($name)) {
                continue;
            }

            // check for existing or to-be-created lookups.
            if ($lookups[$name]) {
                $failed[$name] = $name;
                continue;
            }

            if ($failed[$name]) {
                continue; // already blown up, fix it.
            }

            $oOldLookup = MetaData::getByValueAndDocumentField($name, $this->oField);
            if (!PEAR::isError($oOldLookup)) {
                $failed[$name] = $name;
                continue;
            }

            $lookups[$name] = $name;
        }
        if (!empty($failed)) {
            $extra_errors['lookups'][] = sprintf(_kt("The following lookups you specified already exist, or are specified twice: %s"), implode(', ', $failed));
        } else if (empty($lookups)) {
            $extra_errors['lookups'][] = _kt("You must have at least 1 new lookup value.");
        }

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

        $data['lookups'] = $lookups;

        foreach ($lookups as $value) {
            $oLookup = MetaData::createFromArray(array(
                'DocFieldId' => $this->oField->getId(),
                'sName' => $value,
                'iTreeParent' => null,
                'bDisabled' => false,
                'bIsStuck' => false,
            ));
            if (PEAR::isError($oLookup)) {
                return $oForm->handleError(sprintf(_kt("Failed to create lookup: %s"), $oLookup->getMessage()));
            }
        }

        $this->successRedirectTo('managefield', sprintf(_kt("%d lookups added."), count($lookups)));
    }

    function do_managelookups() {
        $this->oPage->setBreadcrumbDetails(_kt('manage lookup values'));

        // Add javascript to create the edit form
        $sJavaScript = "\nfunction editLookup(id)\n
            {\n
                var div = document.getElementById(id);\n
                var value = div.innerHTML;

                <!-- Replace all double quotes with &#34; -->\n
                matches = value.match(/\"/g);\n
                var newValue = value;\n
                if(matches){\n
                    for(var i = 0; i < matches.length; i++){\n
                        newValue = newValue.replace('\"', '&#34;');\n
                    }\n
                }\n\n

                var inner = '<input type=\"text\" name=\"lookup['+id+']\" id=\"lookup_'+id+'\" value=\"'+newValue+'\" />';\n
                inner += '<input type=\"hidden\" id=\"original_'+id+'\" value=\"'+newValue+'\" />';\n
                inner += '<input type=\"submit\" name=\"submit[edit]\" value=\""._kt('Save')."\" />';\n
                inner += '<input type=\"button\" onclick=\"javascript: closeLookupEdit('+id+');\" name=\"cancel\" value=\""._kt('Cancel')."\" />';\n
                div.innerHTML = inner;\n
                document.getElementById('lookup_'+id).focus();\n
            }\n\n

            function closeLookupEdit(id)
            {\n
                value = document.getElementById('original_'+id).value;\n
                document.getElementById(id).innerHTML = value;\n
            }\n\n";

        $this->oPage->requireJSStandalone($sJavaScript);

        $lookups =& MetaData::getByDocumentField($this->oField);
        $args = $this->meldPersistQuery("","metadataMultiAction", true);

        $oTemplate =& $this->oValidator->validateTemplate("ktcore/metadata/admin/manage_lookups");
        $oTemplate->setData(array(
            'context' => $this,
            'field_name' => $this->oField->getName(),
            'lookups' => $lookups,
            'args' => $args,
        ));
        return $oTemplate->render();
    }

    // {{{ do_metadataMultiAction
    function do_metadataMultiAction() {
        $subaction = array_keys(KTUtil::arrayGet($_REQUEST, 'submit', array()));
        $this->oValidator->notEmpty($subaction, array("message" => _kt("No action specified")));
        $subaction = $subaction[0];
        $method = null;
        if (method_exists($this, 'lookup_' . $subaction)) {
            $method = 'lookup_' . $subaction;
        }
        $this->oValidator->notEmpty($method, array("message" => _kt("Unknown action specified")));
        return $this->$method();
    }
    // }}}

    // {{{ lookup_remove
    function lookup_remove() {
        $oFieldset =& $this->oValidator->validateFieldset($_REQUEST['fFieldsetId']);
        $oField =& DocumentField::get($_REQUEST['fFieldId']);
        $aMetadata = KTUtil::arrayGet($_REQUEST, 'metadata');
        if (empty($aMetadata)) {
            $this->errorRedirectTo('managelookups', _kt('No lookups selected'));
        }
        foreach ($_REQUEST['metadata'] as $iMetaDataId) {
            $oMetaData =& MetaData::get($iMetaDataId);
            if (PEAR::isError($oMetaData)) {
                $this->errorRedirectTo('managelookups', _kt('Invalid lookup selected'));
            }
            $oMetaData->delete();
        }
        $this->successRedirectTo('managelookups', _kt('Lookups removed'));
        exit(0);
    }
    // }}}

    // {{{ lookup_disable
    function lookup_disable() {
        $oFieldset =& $this->oValidator->validateFieldset($_REQUEST['fFieldsetId']);
        $oField =& DocumentField::get($_REQUEST['fFieldId']);
        $aMetadata = KTUtil::arrayGet($_REQUEST, 'metadata');
        if (empty($aMetadata)) {
            $this->errorRedirectTo('managelookups', _kt('No lookups selected'));
        }
        foreach ($_REQUEST['metadata'] as $iMetaDataId) {
            $oMetaData =& MetaData::get($iMetaDataId);
            if (PEAR::isError($oMetaData)) {
                $this->errorRedirectTo('managelookups', _kt('Invalid lookup selected'));
            }
            $oMetaData->setDisabled(true);
            $oMetaData->update();
        }
        $this->successRedirectTo('managelookups', _kt('Lookups disabled'));
        exit(0);
    }
    // }}}

    /**
     * Save the edited lookup values
     *
     */
    function lookup_edit(){
        $aLookupValues = $_REQUEST['lookup'];

        if(empty($aLookupValues)){
            $this->errorRedirectTo('managelookups', _kt('No lookups were selected for editing'));
            exit;
        }

        foreach ($aLookupValues as $iMetaDataId => $sValue){
            $oMetaData = MetaData::get($iMetaDataId);
            if (PEAR::isError($oMetaData)) {
                $this->addErrorMessage(_kt('Invalid lookup selected').': '.$sValue);
                continue;
                //$this->errorRedirectTo('managelookups', _kt('Invalid lookup selected'));
            }
            if(empty($sValue)){
                $this->addErrorMessage(_kt('Lookup cannot be empty').': '.$oMetaData->getName());
                if(count($aLookupValues) == 1){
                    $this->redirectTo('managelookups');
                }
                continue;
            }
            $oMetaData->setName($sValue);
            $oMetaData->update();
        }

        $this->successRedirectTo('managelookups', _kt('Lookup values saved'));
        exit(0);
    }

    // {{{ lookup_enable
    function lookup_toggleenabled() {
        $oFieldset =& $this->oValidator->validateFieldset($_REQUEST['fFieldsetId']);
        $oField =& DocumentField::get($_REQUEST['fFieldId']);
        $aMetadata = KTUtil::arrayGet($_REQUEST, 'metadata');
        if (empty($aMetadata)) {
            $this->errorRedirectTo('managelookups', _kt('No lookups selected'));
        }
        foreach ($_REQUEST['metadata'] as $iMetaDataId) {
            $oMetaData =& MetaData::get($iMetaDataId);
            if (PEAR::isError($oMetadata)) {
                $this->errorRedirectTo('managelookups', _kt('Invalid lookup selected'));
            }
            $oMetaData->setDisabled(!$oMetaData->getDisabled());
            $oMetaData->update();
        }
        $this->successRedirectTo('managelookups', _kt('Status Toggled'));
        exit(0);
    }
    // }}}

    // {{{ lookup_togglestickiness
    function lookup_togglestickiness() {
        $oFieldset =& $this->oValidator->validateFieldset($_REQUEST['fFieldsetId']);
        $oField =& DocumentField::get($_REQUEST['fFieldId']);
        $aMetadata = KTUtil::arrayGet($_REQUEST, 'metadata');
        if (empty($aMetadata)) {
            $this->errorRedirectTo('managelookups', _kt('No lookups selected'));
        }
        foreach ($_REQUEST['metadata'] as $iMetaDataId) {
            $oMetaData =& MetaData::get($iMetaDataId);
            if (PEAR::isError($oMetaData)) {
                $this->errorRedirectTo('managelookups', _kt('Invalid lookups selected'));
            }
            $bStuck = (boolean)$oMetaData->getIsStuck();
            $oMetaData->setIsStuck(!$bStuck);
            $oMetaData->update();
        }
        $this->successRedirectTo('managelookups', _kt('Lookup stickiness toggled'));
        exit(0);
    }
    // }}}

// {{{ TREE
    // create and display the tree editing form.
    function do_managetree() {
        global $default;
        // extract.
        $iFieldsetId = KTUtil::getId($this->oFieldset);
        $iFieldId = KTUtil::getId($this->oField);

        $oFieldset =& $this->oFieldset;
        $oField =& $this->oField;

        $this->oPage->setBreadcrumbDetails(_kt('edit lookup tree'));

        $field_id = $iFieldId;
        $current_node = KTUtil::arrayGet($_REQUEST, 'current_node', 0);
        $subaction = KTUtil::arrayGet($_REQUEST, 'subaction');

        // validate
        if (empty($field_id)) { return $this->errorRedirectToMain(_kt("Must select a field to edit.")); }
        $oField =& DocumentField::get($field_id);
        if (PEAR::isError($oField)) { return $this->errorRedirectToMain(_kt("Invalid field.")); }

        $aErrorOptions = array(
            'redirect_to' => array('editTree', sprintf('field_id=%d', $field_id)),
        );

        // under here we do the subaction rendering.
        // we do this so we don't have to do _very_ strange things with multiple actions.
        //$default->log->debug("Subaction: " . $subaction);
        $fieldTree =& new MDTree();
        $fieldTree->buildForField($oField->getId());

        if ($subaction !== null) {
            $target = 'managetree';
            $msg = _kt('Changes saved.');
            if ($subaction === "addCategory") {
                $new_category = KTUtil::arrayGet($_REQUEST, 'category_name');
                if (empty($new_category)) {
                    return $this->errorRedirectTo("managetree", _kt("Must enter a name for the new category."), array("field_id" => $field_id, "fFieldsetId" => $iFieldsetId));
                } else {
                    $this->subact_addCategory($field_id, $current_node, $new_category, $fieldTree);
                }
                $msg = _kt('Category added'). ': ' . $new_category;
            }
            if ($subaction === "deleteCategory") {
                $this->subact_deleteCategory($fieldTree, $current_node);
                $current_node = 0;      // clear out, and don't try and render the newly deleted category.
                $msg = _kt('Category removed.');
            }
            if ($subaction === "linkKeywords") {
                $keywords = KTUtil::arrayGet($_REQUEST, 'keywordsToAdd');
                $aErrorOptions['message'] = _kt("No keywords selected");
                $this->oValidator->notEmpty($keywords, $aErrorOptions);
                $this->subact_linkKeywords($fieldTree, $current_node, $keywords);
                $current_node = 0;      // clear out, and don't try and render the newly deleted category.
                $msg = _kt('Keywords added to category.');
            }
            if ($subaction === "unlinkKeyword") {
                $keyword = KTUtil::arrayGet($_REQUEST, 'keyword_id');
                $this->subact_unlinkKeyword($fieldTree, $keyword);
                $msg = _kt('Keyword moved to base of tree.');
            }
            // now redirect
            $query = sprintf('field_id=%d&fFieldsetId=%d', $field_id, $iFieldsetId);
            return $this->successRedirectTo($target, $msg, $query);
        }
        if ($fieldTree->root === null) {
            return $this->errorRedirectToMain(_kt("Error building tree. Is this a valid tree-lookup field?"));
        }

        // FIXME extract this from MDTree (helper method?)
        $free_metadata = MetaData::getList('document_field_id = '.$oField->getId().' AND (treeorg_parent = 0 OR treeorg_parent IS NULL) AND (disabled = 0)');

        // render edit template.

        $oTemplate = $this->oValidator->validateTemplate("ktcore/metadata/admin/edit_lookuptree");
        $renderedTree = $this->_evilTreeRenderer($fieldTree);

        $this->oPage->setTitle(_kt('Edit Lookup Tree'));

        if ($current_node == 0) { $category_name = 'Root'; }
        else {
            $oNode = MDTreeNode::get($current_node);
            $category_name = $oNode->getName();
        }

        $aTemplateData = array(
            "context" => $this,
            "args" => $this->meldPersistQuery("","managetree", true),
            "field" => $oField,
            "oFieldset" => $oFieldset,
            "tree" => $fieldTree,
            "renderedTree" => $renderedTree,
            "currentNode" => $current_node,
            'category_name' => $category_name,
            "freechildren" => $free_metadata,

        );
        return $oTemplate->render($aTemplateData);
    }

    function subact_addCategory($field_id, $current_node, $new_category, &$constructedTree) {
        $newCategory = MDTreeNode::createFromArray(array (
             "iFieldId" => $field_id,
             "sName" => $new_category,
             "iParentNode" => $current_node,
        ));
        if (PEAR::isError($newCategory))
        {
            return false;
        }
        $constructedTree->addNode($newCategory);
        return true;
    }

    function subact_deleteCategory(&$constructedTree, $current_node) {
        $constructedTree->deleteNode($current_node);
        return true;
    }

    function subact_unlinkKeyword(&$constructedTree, $keyword) {
        $oKW = MetaData::get($keyword);
        if (PEAR::isError($oKW)) {
            return true;
        }
        $constructedTree->reparentKeyword($oKW->getId(), 0);
        return true;
    }


    function subact_linkKeywords(&$constructedTree, $current_node, $keywords) {
        foreach ($keywords as $md_id)
        {
            $constructedTree->reparentKeyword($md_id, $current_node);
        }
        return true;
    }

    /* ----------------------- EVIL HACK --------------------------
     *
     *  This whole thing needs to replaced, as soon as I work out how
     *  to non-sucking Smarty recursion.
     */

    function _evilTreeRecursion($subnode, $treeToRender)
    {
        // deliver us from evil....
        $iFieldId = $treeToRender->field_id;
        $oField = DocumentField::get($iFieldId);
        $iFieldsetId = $oField->getParentFieldsetId();

        $treeStr = "<ul>";
        foreach ($treeToRender->contents[$subnode] as $subnode_id => $subnode_val)
        {
            if ($subnode_id !== "leaves") {
                $treeStr .= '<li class="treenode active"><a class="pathnode inactive"  onclick="toggleElementClass(\'active\', this.parentNode); toggleElementClass(\'inactive\', this.parentNode);">' . $treeToRender->mapnodes[$subnode_val]->getName() . '</a>';
                $treeStr .= $this->_evilActionHelper($iFieldsetId, $iFieldId, false, $subnode_val);
                $treeStr .= $this->_evilTreeRecursion($subnode_val, $treeToRender);
                $treeStr .= '</li>';
            }
            else
            {
                foreach ($subnode_val as $leaf)
                {
                    $treeStr .= '<li class="leafnode">' . $treeToRender->lookups[$leaf]->getName();
                    $treeStr .= $this->_evilActionHelper($iFieldsetId, $iFieldId, true, $leaf);
                    $treeStr .=  '</li>';            }
                }
        }
        $treeStr .= '</ul>';
        return $treeStr;

    }

    // I can't seem to do recursion in smarty, and recursive templates seems a bad solution.
    // Come up with a better way to do this (? NBM)
    function _evilTreeRenderer($treeToRender) {
        //global $default;

        $treeStr = "<!-- this is rendered with an unholy hack. sorry. -->";
        $stack = array();
        $exitstack = array();

        // since the root is virtual, we need to fake it here.
        // the inner section is generised.
        $treeStr .= '<ul class="kt_treenodes"><li class="treenode active"><a class="pathnode"  onclick="toggleElementClass(\'active\', this.parentNode);toggleElementClass(\'inactive\', this.parentNode);">' . _kt('Root') . '</a>';
        $treeStr .= ' (<a href="' . KTUtil::addQueryStringSelf($this->meldPersistQuery('current_node=0', 'managetree')) . '">' . _kt('attach keywords') . '</a>)';
        $treeStr .= '<ul>';

        //$default->log->debug("EVILRENDER: " . print_r($treeToRender, true));
        foreach ($treeToRender->getRoot() as $node_id => $subtree_nodes)
        {
            //$default->log->debug("EVILRENDER: ".$node_id." => ".$subtree_nodes." (".($node_id === "leaves").")");
            // leaves are handled differently.
            if ($node_id !== "leaves") {
                // $default->log->debug("EVILRENDER: " . print_r($subtree_nodes, true));
                $treeStr .= '<li class="treenode active"><a class="pathnode" onclick="toggleElementClass(\'active\', this.parentNode);toggleElementClass(\'inactive\', this.parentNode);">' . $treeToRender->mapnodes[$subtree_nodes]->getName() . '</a>';
                $treeStr .= $this->_evilActionHelper($iFieldsetId, $iFieldId, false, $subtree_nodes);
                $treeStr .= $this->_evilTreeRecursion($subtree_nodes, $treeToRender);
                $treeStr .= '</li>';
            }
            else
            {
                foreach ($subtree_nodes as $leaf)
                {
                    $treeStr .= '<li class="leafnode">' . $treeToRender->lookups[$leaf]->getName();
                    $treeStr .= $this->_evilActionHelper($iFieldsetId, $iFieldId, true, $leaf);
                    $treeStr .=  '</li>';
                }
            }
        }
        $treeStr .= '</ul></li>';
        $treeStr .= '</ul>';

        return $treeStr;
    }

    // BS: don't hate me.
    // BD: sorry. I hate you.

    function _evilActionHelper($iFieldsetId, $iFieldId, $bIsKeyword, $current_node) {
        $actionStr = " (";
        if ($bIsKeyword === true) {
           $actionStr .= '<a href="' . KTUtil::addQueryStringSelf(KTUtil::addQueryStringSelf($this->meldPersistQuery('keyword_id='.$current_node.'&subaction=unlinkKeyword', 'managetree'))) . '">' . _kt('unlink') . '</a>';
        } else {
           $actionStr .= '<a href="' . KTUtil::addQueryStringSelf($this->meldPersistQuery('current_node=' . $current_node, 'managetree')) .'">' . _kt('attach keywords') . '</a> ';
           $actionStr .= '| <a href="' . KTUtil::addQueryStringSelf($this->meldPersistQuery('current_node='.$current_node.'&subaction=deleteCategory', 'managetree')) . '">' . _kt('delete') . '</a>';
        }
        $actionStr .= ")";
        return $actionStr;
    }

    function do_deletefield() {
        $res = $this->oField->delete();
        if (PEAR::isError($res)) {
            $this->errorRedirectToParent(sprintf(_kt("Unable to delete field: %s"), $res->getMessage()));
        }

        $this->successRedirectToParent(_kt("Field deleted."));
    }

    /**
     * Move field up in the order
     */
    function do_orderUp() {
        $iId = $this->oField->getID();
        $iFieldsetId = $this->oField->getParentFieldsetId();

        $res = $this->oField->movePosition($iFieldsetId, $iId, 'up');
        if ($res === false) {
            $this->errorRedirectToParent(_kt("Unable to move field up"));
        }

        $this->successRedirectToParent(_kt("Field moved up."));
    }

    /**
     * Move field down in the order
     */
    function do_orderDown() {
        $iId = $this->oField->getID();
        $iFieldsetId = $this->oField->getParentFieldsetId();

        $res = $this->oField->movePosition($iFieldsetId, $iId, 'down');
        if ($res === false) {
            $this->errorRedirectToParent(_kt("Unable to move field down"));
        }

        $this->successRedirectToParent(_kt("Field moved down."));
    }
}

?>