Commit 876fb9a60956f44e5c53cc0b1bb376517fb47c24

Authored by nbm
1 parent c2672dfe

PHP code for conditional metadata simple case


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3702 c91229c3-7414-0410-bfa2-8a42b809f60b
presentation/lookAndFeel/knowledgeTree/administration/docfieldmanagement/conditional/ajaxConditionals.php 0 → 100644
  1 +<?php
  2 +require_once("../../../../../config/dmsDefaults.php");
  3 +require_once(KT_DIR . "/presentation/Html.inc");
  4 +require_once(KT_LIB_DIR . "/templating/templating.inc.php");
  5 +require_once(KT_LIB_DIR . "/documentmanagement/DocumentField.inc");
  6 +require_once(KT_LIB_DIR . "/database/dbutil.inc");
  7 +require_once(KT_LIB_DIR . "/util/ktutil.inc");
  8 +require_once(KT_LIB_DIR . "/dispatcher.inc.php");
  9 +$sectionName = "Administration";
  10 +require_once(KT_DIR . "/presentation/webpageTemplate.inc");
  11 +require_once(KT_LIB_DIR . "/metadata/fieldset.inc.php");
  12 +
  13 +class AjaxConditionalAdminDispatcher extends KTDispatcher {
  14 + function do_main() {
  15 + return "Ajax Error.";
  16 + }
  17 +
  18 + // a lot simpler than the standard dispatcher, this DOESN'T include a large amount of "other" stuff ... we are _just_ called to handle
  19 + // input/output of simple HTML components.
  20 + function handleOutput($data) {
  21 + print $data;
  22 + }
  23 +
  24 + function do_getMasterFieldForSet() {
  25 + global $default;
  26 + $fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id');
  27 + if (empty($fieldset_id)) {
  28 + return "Ajax error: No fieldset specified.";
  29 + }
  30 +
  31 + $oFieldset = KTFieldset::get($fieldset_id);
  32 + if (PEAR::isError($oFieldset)) {
  33 + return "Ajax error: No such fieldset (".$fieldset_id.")";
  34 + }
  35 +
  36 + $oField = KTMetadataUtil::getMasterField($oFieldset);
  37 + if (PEAR::isError($oField)) {
  38 + return "Ajax Error (subselect check).";
  39 + }
  40 + $master_field_id = $oField->getId();
  41 +
  42 + $aFreeLookups = MetaData::getByDocumentField($oField);
  43 +
  44 + $oTemplating = new KTTemplating;
  45 + $oTemplate = $oTemplating->loadTemplate("ktcore/metadata/conditional/conditional_ajax_masterfield");
  46 + $aTemplateData = array(
  47 + "master_field_id" => $master_field_id,
  48 + "free_lookups" => $aFreeLookups,
  49 + );
  50 +
  51 + return $oTemplate->render($aTemplateData);
  52 +
  53 + }
  54 +
  55 +
  56 + function do_getFieldFromSet() {
  57 + global $default;
  58 +
  59 + $fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id');
  60 + $field_id = KTUtil::arrayGet($_REQUEST, 'field_id');
  61 +
  62 + if (empty($fieldset_id)) {
  63 + return "Ajax error: No fieldset specified.";
  64 + }
  65 +
  66 + if (empty($field_id)) {
  67 + return "Ajax error: No field specified.";
  68 + }
  69 +
  70 +
  71 + $oFieldset = KTFieldset::get($fieldset_id);
  72 + if (PEAR::isError($oFieldset)) {
  73 + return "Ajax error: No such fieldset (".$fieldset_id.")";
  74 + }
  75 +
  76 + $oField = DocumentField::get($field_id);
  77 + if (PEAR::isError($oField)) {
  78 + return "Ajax error: No such field (".$field_id.")";
  79 + }
  80 +
  81 + $master_field_id = $oFieldset->getMasterField();
  82 +
  83 +
  84 +
  85 + $sQuery = "SELECT md_look.id AS lookup_id, md_look.name AS lookup_val FROM $default->metadata_table AS md_look
  86 + WHERE md_look.document_field_id = ?";
  87 + $aParams = array($field_id);
  88 + $res = DBUtil::getResultArray(array($sQuery, $aParams));
  89 + if (PEAR::isError($res)) {
  90 + return "Ajax Error (subselect for lookups).";
  91 + }
  92 +
  93 + $sQuery = "SELECT md_cond.id AS rule_id, md_cond.name AS rule_name FROM $default->md_condition_table AS md_cond
  94 + WHERE md_cond.document_field_id = ? AND md_cond.name IS NOT NULL";
  95 + $aParams = array($field_id);
  96 + $rulesets = DBUtil::getResultArray(array($sQuery, $aParams));
  97 + if (PEAR::isError($rulesets)) {
  98 + return "Ajax Error (subselect for rulesets).";
  99 + }
  100 +
  101 +
  102 + $oTemplating = new KTTemplating;
  103 + $oTemplate = $oTemplating->loadTemplate("ktcore/conditional_ajax_subfield");
  104 + $aTemplateData = array(
  105 + "field_id" => $field_id,
  106 + "fieldset_id" => $fieldset_id,
  107 + "lookups" => $res,
  108 + "rulesets" => $rulesets,
  109 + );
  110 + return $oTemplate->render($aTemplateData);
  111 +
  112 + }
  113 +
  114 +}
  115 +
  116 +$oDispatcher = new AjaxConditionalAdminDispatcher();
  117 +$oDispatcher->dispatch();
  118 +
  119 +?>
... ...
presentation/lookAndFeel/knowledgeTree/administration/docfieldmanagement/conditional/manageConditionals.php 0 → 100644
  1 +<?php
  2 +require_once("../../../../../../config/dmsDefaults.php");
  3 +require_once(KT_DIR . "/presentation/Html.inc");
  4 +require_once(KT_LIB_DIR . "/templating/templating.inc.php");
  5 +require_once(KT_LIB_DIR . "/documentmanagement/DocumentField.inc");
  6 +require_once(KT_LIB_DIR . "/documentmanagement/MDCondition.inc");
  7 +require_once(KT_LIB_DIR . "/database/dbutil.inc");
  8 +require_once(KT_LIB_DIR . "/util/ktutil.inc");
  9 +require_once(KT_LIB_DIR . "/dispatcher.inc.php");
  10 +require_once(KT_LIB_DIR . "/metadata/fieldset.inc.php");
  11 +$sectionName = "Administration";
  12 +require_once(KT_DIR . "/presentation/webpageTemplate.inc");
  13 +
  14 +
  15 +
  16 +class ManageConditionalDispatcher extends KTStandardDispatcher {
  17 + function do_main() {
  18 +
  19 + $aFieldsets = KTFieldset::getList("is_conditional = 1");
  20 + $oTemplating = new KTTemplating;
  21 +
  22 + $oTemplate = $oTemplating->loadTemplate("ktcore/metadata/conditional/select_fieldset");
  23 + $aTemplateData = array(
  24 + "available_fieldsets" => $aFieldsets,
  25 + );
  26 + return $oTemplate->render($aTemplateData);
  27 + }
  28 +
  29 + // FIXME refactor this into do_editSimple(fieldset_id);
  30 + function do_editFieldset() {
  31 + $fieldset_id = KTUtil::arrayGet($_REQUEST, "fieldset_id");
  32 + $oTemplating = new KTTemplating;
  33 + $oTemplate = $oTemplating->loadTemplate("ktcore/metadata/conditional/editsimple");
  34 + /* alright: to "do" this we need at least:
  35 + * 1. the list of all the columns (id, name) and their available values.
  36 + * 2. the fieldset_id.
  37 + * we can then render in/out. Everything "intelligent" happens
  38 + * in AJAX (doing it with submits sucks arse.
  39 + *
  40 + * FIXME we fake it here with nested arrays...
  41 + */
  42 + $oFieldset =& KTFieldset::get($fieldset_id);
  43 + $aFields =& $oFieldset->getFields();
  44 + $aTemplateData = array(
  45 + "fieldset_id" => $fieldset_id,
  46 + "aFields" => $aFields,
  47 + "iMasterFieldId" => $aFields[0]->getId(),
  48 + );
  49 + return $oTemplate->render($aTemplateData);
  50 + }
  51 +
  52 +
  53 +
  54 + /** DELETE FROM HERE. */
  55 +
  56 +
  57 + function do_newMasterConditionSet() {
  58 + $fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id');
  59 +
  60 + if (empty($fieldset_id)) {
  61 + return $this->errorRedirectToMain("No fieldset specified.");
  62 + }
  63 + $oFieldset = KTFieldset::get($fieldset_id);
  64 + if (PEAR::isError($oFieldset)) {
  65 + return $this->errorRedirectToMain("Unable to open the specified fieldset.");
  66 + }
  67 +
  68 +
  69 + // now we need to get other fields in this set.
  70 + $aOtherFields = DocumentField::getList('parent_fieldset = '.$oFieldset->getId().' AND id != '.$oFieldset->getMasterField());
  71 + if (PEAR::isError($aOtherFields)) {
  72 + $this->errorRedirectToMain("Failed to get field list for table.");
  73 + }
  74 + $oTemplating = new KTTemplating;
  75 +
  76 + $oTemplate = $oTemplating->loadTemplate("ktcore/metadata/conditional/new_masterset");
  77 + $aTemplateData = array(
  78 + "fieldset_id" => $fieldset_id,
  79 + "other_fields" => $aOtherFields,
  80 + );
  81 + return $oTemplate->render($aTemplateData);
  82 + }
  83 +
  84 + function do_newSubConditionSet() {
  85 + global $default;
  86 + $default->log->debug('SUBCONDITION CREATION: starting.');
  87 + $fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id');
  88 + $starting_id = KTUtil::arrayGet($_REQUEST, 'starting_field'); // populated elsewhere.
  89 +
  90 + if (empty($fieldset_id)) {
  91 + return $this->errorRedirectToMain("No fieldset specified.");
  92 + }
  93 + if (empty($starting_id)) {
  94 + return $this->errorRedirectToMain("No field specified to start from.");
  95 + }
  96 + $default->log->debug('SUBCONDITION CREATION: extracted.');
  97 + $oFieldset = KTFieldset::get($fieldset_id);
  98 + if (PEAR::isError($oFieldset)) {
  99 + return $this->errorRedirectToMain("Unable to open the specified fieldset.");
  100 + }
  101 + $default->log->debug('SUBCONDITION CREATION: validated.');
  102 +
  103 + // now we need to get other fields in this set.
  104 + $aOtherFields = DocumentField::getList('parent_fieldset = '.$oFieldset->getId().' AND id != '.$oFieldset->getMasterField().' AND id != '.$starting_id);
  105 + if (PEAR::isError($aOtherFields)) {
  106 + $this->errorRedirectToMain("Failed to get field list for table.");
  107 + }
  108 + // FIXME we tableMappings.
  109 + $starting_lookup = DBUtil::getResultArray(array('SELECT id, name FROM metadata_lookup WHERE document_field_id = ? ', array($starting_id)));
  110 + if (PEAR::isError($starting_lookup)) {
  111 + return $this->errorRedirectToMain('invalid starting field.');
  112 + }
  113 + $default->log->debug('SUBCONDITION CREATION: rendering.');
  114 + $oTemplating = new KTTemplating;
  115 +
  116 + $oTemplate = $oTemplating->loadTemplate("ktcore/metadata/conditional/new_subchain");
  117 + $aTemplateData = array(
  118 + "fieldset_id" => $fieldset_id,
  119 + "starting_field" => $starting_id,
  120 + "starting_lookup" => $starting_lookup,
  121 + "other_fields" => $aOtherFields,
  122 + );
  123 + return $oTemplate->render($aTemplateData);
  124 + }
  125 +
  126 +
  127 + function handleOutput($data) {
  128 + global $main;
  129 + $main->bFormDisabled = true;
  130 + $main->setCentralPayload($data);
  131 + $main->render();
  132 + }
  133 +
  134 + /* create a "master" (or root) condition for a fieldset.
  135 + * this _must_ be for a "masterfield".
  136 + */
  137 +
  138 + function do_createMasterChain() {
  139 + global $default;
  140 + $default->log->debug("CREATE MASTER CHAIN: starting.");
  141 +
  142 +
  143 + // FIXME: delete EVERYTHING that chains FROM this item.
  144 + $fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id');
  145 + $rule_name = KTUtil::arrayGet($_REQUEST, 'rule_name');
  146 + $base_lookup_id = KTUtil::arrayGet($_REQUEST, 'lookup_id'); // we ARE on the master field, this represents that.
  147 +
  148 + $default->log->debug("CREATE MASTER CHAIN: lookup_id " .print_r($lookup_id, true));
  149 +
  150 + $fields_to_attach = KTUtil::arrayGet($_REQUEST, 'fields_to_attach'); // listed as fields_to_attach[], a list.
  151 + if (empty($fields_to_attach)) {
  152 + return $this->errorRedirectToMain("No fields specified.");
  153 + } else {
  154 + // get the list of fields, and their "chained" values.
  155 + // we key this by field.
  156 + $chained_direct_values = array();
  157 + foreach ($fields_to_attach as $field_id) {
  158 + $chained_direct_values[$field_id] = array();
  159 + // we now grab the appropriate values from the form.
  160 + // these will have been input as "direct_values_{$field_id}
  161 + $direct_values = KTUtil::arrayGet($_REQUEST, "direct_values_".$field_id);
  162 + if (empty($direct_values)) {
  163 + return $this->errorRedirectToMain("Missing input for field ".$field_id);
  164 + } else {
  165 + foreach ($direct_values as $lookup_id) {
  166 + // FIXME use MetaData::get() on these to verify their existence.
  167 + $chained_direct_values[$field_id][] = $lookup_id;
  168 + }
  169 + }
  170 + }
  171 + }
  172 +
  173 + $default->log->debug("CREATE MASTER CHAIN: lookup_id (2) " .print_r($lookup_id, true));
  174 + $rulesets_to_attach = KTUtil::arrayGet($_REQUEST, 'rulesets_to_attach');
  175 +
  176 + $oFieldset = KTFieldset::get($fieldset_id);
  177 + if (PEAR::isError($oFieldset)) {
  178 + return $this->errorRedirectToMain("Invalid Fieldset.");
  179 + }
  180 +
  181 + $resObj = MDConditionNode::createFromArray(array(
  182 + "iFieldId" => $oFieldset->getMasterField(),
  183 + "iLookupId" => $base_lookup_id,
  184 + "sName" => $rule_name,
  185 + ));
  186 +
  187 + $default->log->debug("CREATE MASTER CHAIN: created master chain node " .print_r($resObj, true));
  188 +
  189 + $resObj2 = MDConditionChain::createFromArray(array(
  190 + "iParentCondition" => null, // this is a MASTER chain.
  191 + "iChildCondition" => $resObj->getId(),
  192 + ));
  193 +
  194 + // the id of this "master rule".
  195 + $master_rule_id = $resObj->getId();
  196 +
  197 + // walk the connections to make, and ...
  198 + // NBM: please make this transactional...
  199 + foreach ($chained_direct_values as $field_id => $lookup_ids) {
  200 + foreach ($lookup_ids as $lookup_id) {
  201 + $lookupCreation = MDConditionNode::createFromArray(array(
  202 + "iFieldId" => $field_id,
  203 + "iLookupId" => $lookup_id,
  204 + ));
  205 + if (PEAR::isError($lookupCreation)) {
  206 + return $this->errorRedirectToMain("Error creating link to ".$field_id." => ".$lookup_id);
  207 + }
  208 + $lookupChain = MDConditionChain::createFromArray(array(
  209 + "iParentCondition" => $master_rule_id,
  210 + "iChildCondition" => $lookupCreation->getId(),
  211 + ));
  212 + if (PEAR::isError($lookupChain)) {
  213 + return $this->errorRedirectToMain("Error creating link to ".$field_id." => ".$lookup_id);
  214 + }
  215 + }
  216 + }
  217 + if (!empty($rulesets_to_attach)) {
  218 + foreach ($rulesets_to_attach as $child_ruleset) {
  219 + $lookupChain = MDConditionChain::createFromArray(array(
  220 + "iParentCondition" => $master_rule_id,
  221 + "iChildCondition" => $child_ruleset,
  222 + ));
  223 + if (PEAR::isError($lookupChain)) {
  224 + return $this->errorRedirectToMain("Error creating link to ruleset ".$child_ruleset);
  225 + }
  226 + }
  227 + }
  228 + $default->log->debug("CREATE MASTER CHAIN: done.");
  229 + return $this->errorRedirectToMain("Created ruleset.");
  230 + }
  231 +
  232 + function do_createSubChain() {
  233 + global $default;
  234 + $default->log->debug("CREATE SB CHAIN: starting.");
  235 +
  236 +
  237 + // FIXME: delete EVERYTHING that chains FROM this item.
  238 + $fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id');
  239 + $starting_id = KTUtil::arrayGet($_REQUEST, 'starting_field');
  240 + $rule_name = KTUtil::arrayGet($_REQUEST, 'rule_name');
  241 +
  242 + if (empty($fieldset_id)) {
  243 + return $this->errorRedirectToMain('No fieldset specified.');
  244 + }
  245 + if (empty($rule_name)) {
  246 + return $this->errorRedirectToMain('Sub-rules MUST have a name specified.');
  247 + }
  248 + if (empty($starting_id)) {
  249 + return $this->errorRedirectToMain('Must indicate which fieldset to start with.');
  250 + }
  251 +
  252 + $base_lookup_id = KTUtil::arrayGet($_REQUEST, 'lookup_id'); // we NEED TO KNOW which is the "base" element.
  253 +
  254 + $default->log->debug("CREATE SB CHAIN: lookup_id " .print_r($base_lookup_id, true));
  255 +
  256 + // these are the next layers of rules to attach.
  257 + $fields_to_attach = KTUtil::arrayGet($_REQUEST, 'fields_to_attach'); // listed as fields_to_attach[], a list.
  258 + $default->log->debug("CREATE SB CHAIN: fields_to_attach " .print_r($fields_to_attach, true));
  259 + if (empty($fields_to_attach)) {
  260 + return $this->errorRedirectToMain("No fields specified.");
  261 + } else {
  262 + // get the list of fields, and their "chained" values.
  263 + // we key this by field.
  264 + $chained_direct_values = array();
  265 + foreach ($fields_to_attach as $field_id) {
  266 + $chained_direct_values[$field_id] = array();
  267 + // we now grab the appropriate values from the form.
  268 + // these will have been input as "direct_values_{$field_id}
  269 + $direct_values = KTUtil::arrayGet($_REQUEST, "direct_values_".$field_id);
  270 + if (empty($direct_values)) {
  271 + return $this->errorRedirectToMain("Missing input for field ".$field_id);
  272 + } else {
  273 + foreach ($direct_values as $lookup_id) {
  274 + // FIXME use MetaData::get() on these to verify their existence.
  275 + $chained_direct_values[$field_id][] = $lookup_id;
  276 + }
  277 + }
  278 + }
  279 + }
  280 +
  281 + $default->log->debug("CREATE SB CHAIN: lookup_id (2) " .print_r($lookup_id, true));
  282 + $rulesets_to_attach = KTUtil::arrayGet($_REQUEST, 'rulesets_to_attach');
  283 +
  284 + $oFieldset = KTFieldset::get($fieldset_id);
  285 + if (PEAR::isError($oFieldset)) {
  286 + return $this->errorRedirectToMain("Invalid Fieldset.");
  287 + }
  288 +
  289 + $resObj = MDConditionNode::createFromArray(array(
  290 + "iFieldId" => $starting_id,
  291 + "iLookupId" => $base_lookup_id,
  292 + "sName" => $rule_name,
  293 + ));
  294 +
  295 + $default->log->debug("CREATE SB CHAIN: created master chain node " .print_r($resObj, true));
  296 +
  297 + // WE DON'T CREATE A PARENT-CHAIN ... this one must be picked up by other nodes.
  298 +
  299 + // the id of this "master rule".
  300 + $master_rule_id = $resObj->getId();
  301 +
  302 + // walk the connections to make, and ...
  303 + // NBM: please make this transactional...
  304 + foreach ($chained_direct_values as $field_id => $lookup_ids) {
  305 + foreach ($lookup_ids as $lookup_id) {
  306 + $lookupCreation = MDConditionNode::createFromArray(array(
  307 + "iFieldId" => $field_id,
  308 + "iLookupId" => $lookup_id,
  309 + ));
  310 + if (PEAR::isError($lookupCreation)) {
  311 + return $this->errorRedirectToMain("Error creating link to ".$field_id." => ".$lookup_id);
  312 + }
  313 + $lookupChain = MDConditionChain::createFromArray(array(
  314 + "iParentCondition" => $master_rule_id,
  315 + "iChildCondition" => $lookupCreation->getId(),
  316 + ));
  317 + if (PEAR::isError($lookupChain)) {
  318 + return $this->errorRedirectToMain("Error creating link to ".$field_id." => ".$lookup_id);
  319 + }
  320 + }
  321 + }
  322 + if (!empty($rulesets_to_attach)) {
  323 + foreach ($rulesets_to_attach as $child_ruleset) {
  324 + $lookupChain = MDConditionChain::createFromArray(array(
  325 + "iParentCondition" => $master_rule_id,
  326 + "iChildCondition" => $child_ruleset,
  327 + ));
  328 + if (PEAR::isError($lookupChain)) {
  329 + return $this->errorRedirectToMain("Error creating link to ruleset ".$child_ruleset);
  330 + }
  331 + }
  332 + }
  333 + $default->log->debug("CREATE SB CHAIN: done.");
  334 + return $this->errorRedirectToMain("Created ruleset.");
  335 + }
  336 +
  337 +}
  338 +
  339 +$oDispatcher = new ManageConditionalDispatcher();
  340 +$oDispatcher->dispatch();
  341 +
  342 +?>
... ...