function _buildGenericCriteria() { $aFields =& DocumentField::getList(); foreach ($aFields as $oField) { $sNamespace = $oField->getNamespace(); $oFieldset =& KTFieldset::get($oField->getParentFieldset()); if ($oFieldset->getName() == 'Tag Cloud') { continue; } //if(is_null($oFieldset->userinfo)){continue;} $aInitialize = array(sprintf("%s: %s", $oFieldset->getName(), $oField->getName()), 'id', 'id', $oField->getId(), $sNamespace); $this->registerCriterion('GenericMetadataCriterion', $sNamespace, null, $aInitialize); } $this->_bGenericRegistered = true; }
function do_selectLookup() { $field = KTUtil::arrayGet($_REQUEST, 'fField', null); $oField = DocumentField::get($field); if (PEAR::isError($oField) || $oField == false || !$oField->getHasLookup()) { $this->errorRedirectToMain('No Field selected.'); exit(0); } $_REQUEST['fBrowseMode'] = 'lookup_value'; $aValues = MetaData::getByDocumentField($oField); $oTemplating =& KTTemplating::getSingleton(); $oTemplate = $oTemplating->loadTemplate('kt3/browse_lookup_value'); $aTemplateData = array('context' => $this, 'oField' => $oField, 'values' => $aValues); return $oTemplate->render($aTemplateData); }
function _upgradeSavedSearch($aSearch) { $aMapping = array('-1' => 'ktcore.criteria.name', '-6' => 'ktcore.criteria.id', '-2' => 'ktcore.criteria.title', '-3' => 'ktcore.criteria.creator', '-4' => 'ktcore.criteria.datecreated', '-5' => 'ktcore.criteria.documenttype', '-7' => 'ktcore.criteria.datemodified', '-8' => 'ktcore.criteria.size', '-9' => 'ktcore.criteria.content', '-10' => 'ktcore.criteria.workflowstate', '-13' => 'ktcore.criteria.discussiontext', '-12' => 'ktcore.criteria.searchabletext', '-11' => 'ktcore.criteria.transactiontext'); $aFieldsets =& KTFieldset::getList('disabled != true'); foreach ($aFieldsets as $oFieldset) { $aFields =& DocumentField::getByFieldset($oFieldset); foreach ($aFields as $oField) { $sNamespace = $oFieldset->getNamespace() . '.' . $oField->getName(); $sId = (string) $oField->getId(); $aMapping[$sId] = $sNamespace; } } foreach (array_keys($aSearch['subgroup']) as $sgkey) { $sg =& $aSearch['subgroup'][$sgkey]; foreach (array_keys($sg['values']) as $vkey) { $item =& $sg['values'][$vkey]; $type = $item['type']; $toreplace = 'bmd' . ((int) $type < 0 ? '_' : '') . abs((int) $type); $item['type'] = $aMapping[$type]; $nData = array(); foreach ($item['data'] as $k => $v) { $k = str_replace($toreplace, $aMapping[$type], $k); $nData[$k] = $v; } $item['data'] = $nData; } } return $aSearch; }
/** * Checks whether a conditional fieldset has the necessary * relationships set up to be usable - this means that for each * field, no matter how it is reached, there is at least one option * available to choose. */ function checkConditionalFieldsetCompleteness($oFieldset) { $oFieldset =& KTUtil::getObject('KTFieldset', $oFieldset); if ($oFieldset->getIsConditional() == false) { // If we're not conditional, we are fine. return true; } /* * First, ensure at least one master field item has a behaviour * assigned to it. That allows at least one item in the master * field to be chosen. */ $iMasterFieldId = $oFieldset->getMasterFieldId(); $sTable = KTUtil::getTableName('field_value_instances'); $sLookupTable = KTUtil::getTableName('metadata_lookup'); $aQuery = array("SELECT COUNT(FVI.id) AS cnt FROM {$sTable} AS FVI LEFT JOIN {$sLookupTable} AS ML ON (FVI.field_value_id = ML.id) WHERE FVI.field_id = ? AND ML.disabled = 0", array($iMasterFieldId)); $iCount = DBUtil::getOneResultKey($aQuery, 'cnt'); if (PEAR::isError($iCount)) { return $iCount; } $GLOBALS['default']->log->debug("Number of value instances for master field: {$iCount}"); if ($iCount == 0) { $GLOBALS['default']->log->debug("Number of value instances for master field is zero, failing"); return PEAR::raiseError(_kt("Master field has no values which are assigned to behaviours.")); } $GLOBALS['default']->log->debug("Number of value instances for master field is positive, continuing"); // fix for KTS-1023 // check that each master-field value has a valueinstance assigned. $sTable = KTUtil::getTableName('metadata_lookup'); $aQuery = array("SELECT COUNT(id) AS cnt FROM {$sTable} WHERE document_field_id = ? AND disabled = 0 ", array($iMasterFieldId)); $iValCount = DBUtil::getOneResultKey($aQuery, 'cnt'); // assumes that there cannot be more than 1 value instance for each master-field-value. if ($iValCount != $iCount) { return PEAR::raiseError(sprintf(_kt('%d values for the Master Field are not assigned to behaviours.'), $iValCount - $iCount)); } /* * Plan: For each behaviour that is assigned on the system, * ensure that it allows at least one value instance in each of * the fields that it needs to affect. */ $sTable = KTUtil::getTableName('field_value_instances'); $sFieldTable = KTUtil::getTableName('document_fields'); $aQuery = array("SELECT DISTINCT FV.behaviour_id AS behaviour_id FROM {$sTable} AS FV INNER JOIN {$sFieldTable} AS F ON FV.field_id = F.id WHERE F.parent_fieldset = ? AND FV.behaviour_id IS NOT NULL", array($oFieldset->getId())); $aBehaviourIds = DBUtil::getResultArrayKey($aQuery, 'behaviour_id'); if (PEAR::isError($aBehaviourIds)) { return $aBehaviourIds; } foreach ($aBehaviourIds as $iBehaviourId) { $GLOBALS['default']->log->debug("Checking behaviour id: " . $iBehaviourId); $oBehaviour =& KTFieldBehaviour::get($iBehaviourId); $sBehaviourName = $oBehaviour->getName(); $sBehaviourHumanName = $oBehaviour->getHumanName(); $iParentFieldId = $oBehaviour->getFieldId(); $GLOBALS['default']->log->debug(" field is " . $iParentFieldId); $aNextFields = KTMetadataUtil::getChildFieldIds($iParentFieldId); $oParentField =& DocumentField::get($iParentFieldId); $sParentFieldName = $oParentField->getName(); $GLOBALS['default']->log->debug(" next fields must include " . print_r($aNextFields, true)); $sTable = KTUtil::getTableName('field_behaviour_options'); $aQuery = array("SELECT DISTINCT field_id FROM {$sTable} WHERE behaviour_id = ?", array($iBehaviourId)); $aFields = DBUtil::getResultArrayKey($aQuery, 'field_id'); $GLOBALS['default']->log->debug(" actual fields are " . print_r($aNextFields, true)); /* foreach ($aNextFields as $iFieldId) { if (!in_array($iFieldId, $aFields)) { $GLOBALS['default']->log->debug(" field $iFieldId is not included, failing"); $oChildField =& DocumentField::get($iFieldId); $sChildFieldName = $oChildField->getName(); return PEAR::raiseError("Child field $sChildFieldName of parent field $sParentFieldName has no selectable values in behaviour $sBehaviourHumanName ($sBehaviourName)"); } */ } $GLOBALS['default']->log->debug("Got through: passed!"); return true; }
/** * Gets a simple array of document metadata fields * * <code> * $ktapi = new KTAPI(); * $session = $ktapi->start_system_session(); * $document = $ktapi->get_document_by_id($documentid); * $metadata = $document->get_packed_metadata(); * foreach($metadata as $field){ * echo '<br><br>Fieldset: ' . $field[0]->getParentFieldset(); * echo '<br>Field name: ' .$field[0]->getName(); * echo ' Value: ' . $field[1]; * } * </code> * * @author KnowledgeTree Team * @access public * @param array $metadata The full metadata fieldsets and fields * @return array An array of metadata field object and value pairs */ function get_packed_metadata($metadata = null) { global $default; if (is_null($metadata)) { $metadata = $this->get_metadata(); } $packed = array(); foreach ($metadata as $fieldset_metadata) { if (is_array($fieldset_metadata)) { $fieldsetname = $fieldset_metadata['fieldset']; $fields = $fieldset_metadata['fields']; } elseif ($fieldset_metadata instanceof stdClass) { $fieldsetname = $fieldset_metadata->fieldset; $fields = $fieldset_metadata->fields; } else { $default->log->debug("unexpected fieldset type"); continue; } $fieldset = KTFieldset::getByName($fieldsetname); if (is_null($fieldset) || PEAR::isError($fieldset) || $fieldset instanceof KTEntityNoObjects) { $default->log->debug("could not resolve fieldset: {$fieldsetname} for document id: {$this->documentid}"); // exit graciously continue; } foreach ($fields as $fieldinfo) { if (is_array($fieldinfo)) { $fieldname = $fieldinfo['name']; $value = $fieldinfo['value']; } elseif ($fieldinfo instanceof stdClass) { $fieldname = $fieldinfo->name; $value = $fieldinfo->value; } else { $default->log->debug("unexpected fieldinfo type"); continue; } $field = DocumentField::getByFieldsetAndName($fieldset, $fieldname); if (is_null($field) || PEAR::isError($field) || is_a($field, 'KTEntityNoObjects')) { $default->log->debug("Could not resolve field: {$fieldname} on fieldset {$fieldsetname} for document id: {$this->documentid}"); // exit graciously continue; } $packed[] = array($field, $value); } } return $packed; }
function &getFields() { return DocumentField::getByFieldset($this); }
$oSource = null; foreach ($aAuthenticationSources as $oPotentialSource) { if ($oPotentialSource->getName() == $sSourceName) { $oSource =& $oPotentialSource; } } if (empty($oSource)) { printf("No authentication source named %s found\n", $sSourceName); exit(1); } $oFieldset =& KTFieldset::getByNamespace($sFieldsetNamespace); if (PEAR::isError($oFieldset)) { printf("No fieldset named %s found\n", $sFieldsetNamespace); exit(1); } $oField = DocumentField::getByFieldsetAndName($oFieldset, $sFieldName); if (PEAR::isError($oField)) { printf("No field named %s found in fieldset %s\n", $sFieldName, $sFieldsetNamespace); exit(1); } $oAuthenticator =& KTAuthenticationUtil::getAuthenticatorForSource($oSource); $oLdap =& $oAuthenticator->oLdap; $aParams = array('scope' => 'sub', 'attributes' => array($sAttribute)); $aResults = $oLdap->search($sRootDn, $sSearch, $aParams); $aValues = array(); foreach ($aResults->entries() as $oEntry) { // print $oEntry->dn() . "\n"; $sValue = $oEntry->get_value($sAttribute, 'single'); // print $sValue . "\n"; if (!empty($sValue)) { $aValues[] = $sValue;
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; }
function do_upload() { set_time_limit(0); $aErrorOptions = array('redirect_to' => array('main', sprintf('fFolderId=%d', $this->oFolder->getId()))); $aErrorOptions['message'] = _kt('Invalid document type provided'); $oDocumentType = $this->oValidator->validateDocumentType($_REQUEST['fDocumentTypeId'], $aErrorOptions); unset($aErrorOptions['message']); $aFile = $this->oValidator->validateFile($_FILES['file'], $aErrorOptions); $matches = array(); $aFields = array(); foreach ($_REQUEST as $k => $v) { if (preg_match('/^metadata_(\\d+)$/', $k, $matches)) { $aFields[] = array(DocumentField::get($matches[1]), $v); } } $aOptions = array('documenttype' => $oDocumentType, 'metadata' => $aFields); $fs =& new KTZipImportStorage('file'); if (!$fs->CheckFormat()) { $sFormats = $fs->getFormats(); $this->addErrorMessage(sprintf(_kt("Bulk Upload failed. Archive is not an accepted format. Accepted formats are: %s"), $sFormats)); controllerRedirect("browse", 'fFolderId=' . $this->oFolder->getID()); exit; } $bm =& new KTBulkImportManager($this->oFolder, $fs, $this->oUser, $aOptions); $this->startTransaction(); $res = $bm->import(); $aErrorOptions['message'] = _kt("Bulk Upload failed"); $this->oValidator->notError($res, $aErrorOptions); $this->addInfoMessage(_kt("Bulk Upload successful")); $this->commitTransaction(); controllerRedirect("browse", 'fFolderId=' . $this->oFolder->getID()); exit(0); }
function widgetsForFieldset($fieldsetOrType, $sContainerName, $oDocument = null) { // this is likely to be called repeatedly. if (is_null($this->oWF)) { $this->oWF =& KTWidgetFactory::getSingleton(); } // we're going to create one of two things, here: // - conditional fieldset widget // - a "Fieldset" widget // FIXME delegate. $oFieldset =& $fieldsetOrType; $widgets = array(); $fields = $oFieldset->getFields(); if ($oFieldset->getIsConditional()) { $iMasterId = $oFieldset->getMasterFieldId(); $oMasterField = DocumentField::get($iMasterId); $newfields = array(); $newfields[] = $oMasterField; foreach ($fields as $oField) { if ($oField->getId() != $iMasterId) { $newfields[] = $oField; } } foreach ($newfields as $oField) { $fname = 'metadata_' . $oField->getId(); $value = null; if (!is_null($oDocument)) { $oFL = DocumentFieldLink::getByDocumentAndField($oDocument, $oField); if (!is_null($oFL) && !PEAR::isError($oFL)) { $value = $oFL->getValue(); } } $widgets[] = $this->oWF->get('ktcore.widgets.conditionalselection', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'id_method' => 'getName', 'label_method' => 'getName', 'unselected_label' => _kt("No selection."), 'simple_select' => false, 'master' => $oField->getId() == $iMasterId, 'masterid' => $iMasterId, 'fieldset' => $oFieldset->getId(), 'field' => $oField->getId())); } } else { foreach ($fields as $oField) { $fname = 'metadata_' . $oField->getId(); $value = null; // check if we had an old value if (!is_null($oDocument)) { $oFL = DocumentFieldLink::getByDocumentAndField($oDocument, $oField); if (!is_null($oFL) && !PEAR::isError($oFL)) { $value = $oFL->getValue(); } } // we have to hack in support for the hardcoded types of fields // handled by the "generic" widget. // // we try to make this a little more "sane" $type = ''; if ($oField->getHasLookup()) { if ($oField->getHasLookupTree()) { $type = 'ktcore.fields.tree'; } else { $type = 'ktcore.fields.lookup'; } } else { $type = 'ktcore.fields.string'; } if ($type == 'ktcore.fields.string') { $widgets[] = $this->oWF->get('ktcore.widgets.string', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription())); } else { if ($type == 'ktcore.fields.lookup') { $widgets[] = $this->oWF->get('ktcore.widgets.entityselection', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'id_method' => 'getName', 'label_method' => 'getName', 'unselected_label' => _kt("No selection."), 'simple_select' => false)); } else { if ($type == 'ktcore.fields.tree') { $widgets[] = $this->oWF->get('ktcore.widgets.treemetadata', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'field_id' => $oField->getId())); } } } } } return array($this->oWF->get('ktcore.widgets.fieldset', array('label' => $oFieldset->getName(), 'description' => $oFieldset->getDescription(), 'name' => $sContainerName, 'widgets' => $widgets))); }
function validate($data) { $results = array(); $errors = array(); // very simple if we're required and not present, fail // otherwise, its ok. $val = KTUtil::arrayGet($data, $this->sInputVariable); if (empty($val)) { // pass } else { if ($this->bMulti) { $val = (array) $val; $failed = array(); foreach ($val as $k) { if (!$this->aVocab[$k]) { $failed[] = $k; } } if (!empty($failed)) { $errors[$this->sBasename] = KTUtil::arrayGet($this->aOptions, 'error_message', sprintf(_kt('"%s" are not valid selections.'), implode(', ', $failed))); } } else { $mandatory = true; if (substr($this->sInputVariable, 0, 9) == 'metadata_') { $fieldid = substr($this->sInputVariable, 9); $field = DocumentField::get($fieldid); $mandatory = $field->getIsMandatory(); } if (!array_key_exists($val, $this->aVocab) && $mandatory) { $errors[$this->sBasename] = KTUtil::arrayGet($this->aOptions, 'error_message', sprintf(_kt('"%s"is not a valid selection.'), $val)); } } } if ($this->bProduceOutput) { $results[$this->sOutputVariable] = $val; } return array('errors' => $errors, 'results' => $results); }
/** * function to add the tagcloud entry to the document_fields table * * @param int $parentId * @return int $id */ function createDocumentField($parentId) { // create the document_field entry $id = DocumentField::createFromArray(array('Name' => 'Tag', 'Description' => 'Tag Words', 'DataType' => 'STRING', 'IsGeneric' => false, 'HasLookup' => false, 'HasLookupTree' => false, 'ParentFieldset' => $parentId, 'IsMandatory' => false)); return $id; }
function do_import() { set_time_limit(0); $aErrorOptions = array('redirect_to' => array('main', sprintf('fFolderId=%d', $this->oFolder->getId()))); $aErrorOptions['message'] = _kt('Invalid document type provided'); $oDocumentType = $this->oValidator->validateDocumentType($_REQUEST['fDocumentTypeId'], $aErrorOptions); $aErrorOptions['message'] = _kt('Invalid path provided'); $sPath = $this->oValidator->validateString($_REQUEST['path'], $aErrorOptions); $matches = array(); $aFields = array(); foreach ($_REQUEST as $k => $v) { if (preg_match('/^metadata_(\\d+)$/', $k, $matches)) { $aFields[] = array(DocumentField::get($matches[1]), $v); } } $aOptions = array('documenttype' => $oDocumentType, 'metadata' => $aFields, 'copy_upload' => 'true'); $po =& new JavascriptObserver($this); $po->start(); $oUploadChannel =& KTUploadChannel::getSingleton(); $oUploadChannel->addObserver($po); $fs =& new KTFSImportStorage($sPath); $bm =& new KTBulkImportManager($this->oFolder, $fs, $this->oUser, $aOptions); DBUtil::startTransaction(); $res = $bm->import(); if (PEAR::isError($res)) { DBUtil::rollback(); $_SESSION["KTErrorMessage"][] = _kt("Bulk import failed") . ": " . $res->getMessage(); } else { DBUtil::commit(); $this->addInfoMessage(_kt("Bulk import succeeded")); } $po->redirectToFolder($this->oFolder->getId()); exit(0); }
function do_changeToSimple() { $this->startTransaction(); $oFieldset =& $this->oValidator->validateFieldset($_REQUEST['fFieldsetId']); $oFieldset->setIsComplex(false); $res = $oFieldset->update(); $this->oValidator->notError($res, array('redirect_to' => array('manageConditional', 'fFieldsetId=' . $oFieldset->getId()), 'message' => _kt('Error changing to simple'))); $aFields = DocumentField::getByFieldset($oFieldset); if (!empty($aFields)) { $aFieldIds = array(); foreach ($aFields as $oField) { $aFieldIds[] = $oField->getId(); } // value instances $sTable = KTUtil::getTableName('field_value_instances'); $aQuery = array("DELETE FROM {$sTable} WHERE field_id IN (" . DBUtil::paramArray($aFieldIds) . ")", $aFieldIds); $res = DBUtil::runQuery($aQuery); //$this->addInfoMessage('value instances: ' . print_r($res, true)); // behaviours $sTable = KTUtil::getTableName('field_behaviours'); $aQuery = array("DELETE FROM {$sTable} WHERE field_id IN (" . DBUtil::paramArray($aFieldIds) . ")", $aFieldIds); $res = DBUtil::runQuery($aQuery); //$this->addInfoMessage('behaviours: ' . print_r($res, true)); } $this->oValidator->notError($res, array('redirect_to' => array('manageConditional', 'fFieldsetId=' . $oFieldset->getId()), 'message' => _kt('Error changing to simple'))); KTEntityUtil::clearAllCaches('KTFieldBehaviour'); KTEntityUtil::clearAllCaches('KTValueInstance'); $this->commitTransaction(); $this->addInfoMessage(_kt('Changed to simple')); redirect($this->sParentUrl); exit(0); }