function Docman_MetadataSqlQueryChunk($md)
 {
     $this->mdv = 'mdv_' . $md->getLabel();
     $this->mdId = $md->getId();
     $this->isRealMetadata = Docman_MetadataFactory::isRealMetadata($md->getLabel());
     if ($this->isRealMetadata) {
         switch ($md->getType()) {
             case PLUGIN_DOCMAN_METADATA_TYPE_TEXT:
                 $this->field = $this->mdv . '.valueText';
                 break;
             case PLUGIN_DOCMAN_METADATA_TYPE_STRING:
                 $this->field = $this->mdv . '.valueString';
                 break;
             case PLUGIN_DOCMAN_METADATA_TYPE_DATE:
                 $this->field = $this->mdv . '.valueDate';
                 break;
             case PLUGIN_DOCMAN_METADATA_TYPE_LIST:
                 $this->field = $this->mdv . '.valueInt';
                 break;
         }
     } else {
         switch ($md->getLabel()) {
             case 'owner':
                 $this->field = 'i.user_id';
                 break;
             default:
                 $this->field = 'i.' . $md->getLabel();
         }
     }
 }
 /**
  * List the available metadata
  */
 function getMetadataTable($mdIter, $groupId, $defaultUrl)
 {
     $content = '';
     $mdFactory = new Docman_MetadataFactory($groupId);
     $content .= '<h3>' . $GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_list_title') . '</h3>' . "\n";
     $content .= $GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_instructions') . "\n";
     $content .= html_build_list_table_top(array($GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_list_name'), $GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_list_descr'), $GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_list_status'), $GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_list_delete')));
     $altRowClass = 0;
     $mdIter->rewind();
     while ($mdIter->valid()) {
         $md =& $mdIter->current();
         $canDelete = false;
         if ($mdFactory->isRealMetadata($md->getLabel())) {
             $canDelete = true;
         }
         $trclass = html_get_alt_row_color($altRowClass++);
         $content .= '<tr class="' . $trclass . '">';
         $nameUrl = $this->buildUrl($defaultUrl, array('action' => 'admin_md_details', 'md' => $md->getLabel()));
         $nameHref = '<a href="' . $nameUrl . '">' . $this->hp->purify($md->getName()) . '</a>';
         $content .= '<td>' . $nameHref . '</td>';
         $content .= '<td>' . $this->hp->purify($md->getDescription()) . '</td>';
         $content .= '<td>';
         if ($md->isRequired()) {
             $content .= "-";
         } else {
             if ($md->isUsed()) {
                 $content .= "Used";
             } else {
                 $content .= "Unused";
             }
         }
         $content .= '</td>';
         $trash = '-';
         if ($canDelete) {
             $link = $this->buildUrl($defaultUrl, array('action' => 'admin_delete_metadata', 'md' => $md->getLabel()));
             $warn = $GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_list_delete_warn', $this->hp->purify($md->getName()));
             $alt = $GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_list_delete_alt', $this->hp->purify($md->getName()));
             $trash = html_trash_link($link, $warn, $alt);
         }
         $content .= '<td>' . $trash . '</td>';
         $content .= '</tr>' . "\n";
         $mdIter->next();
     }
     $content .= '</table>' . "\n";
     return $content;
 }
 function massUpdate($srcItemId, $mdLabel, $itemIdArray)
 {
     $mdFactory = new Docman_MetadataFactory($this->groupId);
     if ($mdFactory->isRealMetadata($mdLabel)) {
         $md = $mdFactory->getFromLabel($mdLabel);
         $dao =& $this->getDao();
         $dao->massUpdate($srcItemId, $md->getId(), $md->getType(), $itemIdArray);
     } else {
         $itemFactory = new Docman_ItemFactory($this->groupId);
         $itemFactory->massUpdate($srcItemId, $mdLabel, $itemIdArray);
     }
 }
 function isValidLabel($label)
 {
     $valid = false;
     if (Docman_MetadataFactory::isHardCodedMetadata($label)) {
         $valid = true;
     } else {
         $valid = Docman_MetadataFactory::isRealMetadata($label);
     }
     return $valid;
 }
 /**
  * This function return an iterator on a list of documents (no folders).
  *
  * How it works:
  * 1. Get the list of all documents that match the criteria (if
  *    any!). (permissions apply).
  *    Note: the final list of documents is a subset of this result.
  * 2. Get the list of folders behind $parentId (permissions apply).
  * 3. Check that each document in list 1. is in a folder of list 2.
  * 5. Apply limits ($start, $offset) is only a subset of the list is required.
  * 6. If needed, add the metadata to the items. 
  */
 function &getItemSubTreeAsList($parentId, &$nbItemsFound, $params = null)
 {
     $_parentId = (int) $parentId;
     $user =& $params['user'];
     // Prepare filters if any
     $filter = null;
     if (isset($params['filter'])) {
         $filter =& $params['filter'];
     }
     // Obsolescence
     $searchItemsParams = array();
     if (isset($params['ignore_obsolete'])) {
         $searchItemsParams['ignore_obsolete'] = $params['ignore_obsolete'];
     }
     // Range of documents to return
     $start = 0;
     if (isset($params['start'])) {
         $start = $params['start'];
     }
     $end = 25;
     if (isset($params['offset'])) {
         $end = $start + $params['offset'];
     }
     $dao =& $this->_getItemDao();
     //
     // Build Folder List
     //
     $parentItem = $this->getItemFromDb($parentId);
     $dPm = Docman_PermissionsManager::instance($parentItem->getGroupId());
     $folderList = array($parentId => &$parentItem);
     $pathIdArray = array($parentId => array());
     $pathTitleArray = array($parentId => array());
     $parentIds = array($parentId);
     $folderIds = array($parentId);
     $i = 0;
     do {
         $i++;
         $dar = $dao->searchSubFolders($parentIds);
         $parentIds = array();
         $itemIds = array();
         $itemRows = array();
         if ($dar && !$dar->isError()) {
             $dar->rewind();
             while ($dar->valid()) {
                 $row = $dar->current();
                 $itemRows[$row['item_id']] = $row;
                 $itemIds[] = $row['item_id'];
                 $parentIds[$row['item_id']] = $row['item_id'];
                 $dar->next();
             }
             // Fetch all the permissions at the same time
             $dPm->retreiveReadPermissionsForItems($itemIds, $user);
             // Build hierarchy: only keep displayable items
             foreach ($itemIds as $id) {
                 if ($dPm->userCanRead($user, $id)) {
                     $folderList[$id] =& $this->getItemFromRow($itemRows[$id]);
                     // Update path
                     $pathIdArray[$id] = array_merge($pathIdArray[$folderList[$id]->getParentId()], array($id));
                     $pathTitleArray[$id] = array_merge($pathTitleArray[$folderList[$id]->getParentId()], array($folderList[$id]->getTitle()));
                 } else {
                     unset($parentIds[$id]);
                 }
             }
         }
     } while (count($parentIds) > 0);
     //
     // Keep only documents in allowed subfolders
     //
     $mdFactory = new Docman_MetadataFactory($this->groupId);
     $ci = null;
     if ($filter !== null) {
         $ci = $filter->getColumnIterator();
     }
     //
     // Build Document list
     //
     $itemArray = array();
     if (isset($params['obsolete_only']) && $params['obsolete_only']) {
         $dar = $dao->searchObsoleteByGroupId($this->groupId);
     } else {
         $dar = $dao->searchByGroupId($this->groupId, $filter, $searchItemsParams);
     }
     $nbItemsFound = 0;
     if ($dar && !$dar->isError()) {
         $this->preloadItemPerms($dar, $user, $this->groupId);
         $dar->rewind();
         while ($dar->valid()) {
             $row = $dar->current();
             // The document is not is one of the allowed subfolder so we
             // can delete it. As a side effect decrease the number of
             // document found.
             if ($dPm->userCanRead($user, $row['item_id']) && isset($folderList[$row['parent_id']])) {
                 if ($nbItemsFound >= $start && $nbItemsFound < $end || isset($params['getall']) && $params['getall']) {
                     $itemArray[$row['item_id']] =& $this->getItemFromRow($row);
                     // Append Path
                     $itemArray[$row['item_id']]->setPathTitle($pathTitleArray[$row['parent_id']]);
                     $itemArray[$row['item_id']]->setPathId($pathIdArray[$row['parent_id']]);
                     // Append metadata
                     if ($ci !== null) {
                         $ci->rewind();
                         while ($ci->valid()) {
                             $c = $ci->current();
                             if ($c->md !== null && $mdFactory->isRealMetadata($c->md->getLabel())) {
                                 $mdFactory->addMetadataValueToItem($itemArray[$row['item_id']], $c->md);
                             }
                             $ci->next();
                         }
                     }
                 }
                 $nbItemsFound++;
             }
             $dar->next();
         }
     }
     $docIter =& new ArrayIterator($itemArray);
     return $docIter;
 }
 protected function getNodeForMetadata(Docman_Metadata $metadata)
 {
     $metaDataFactory = new Docman_MetadataFactory($metadata->getGroupId());
     if ($metaDataFactory->isRealMetadata($metadata->getLabel())) {
         $node = $this->doc->createElement('property');
         $node->setAttribute('title', $metadata->getName());
         if ($metadata->getValue() instanceof ArrayIterator) {
             $this->getNodeForMetadataValues($metadata->getValue(), $node);
         } else {
             $value = $metadata->getValue();
             if ($value != '' && $metadata->getType() == PLUGIN_DOCMAN_METADATA_TYPE_DATE) {
                 $value = date('c', $value);
             }
             $node->appendChild($this->doc->createTextNode($value));
         }
         return $node;
     }
 }
 /**
  *
  */
 function cloneFilter($srcFilter, $dstReport, $metadataMapping)
 {
     $dstMdFactory = new Docman_MetadataFactory($dstReport->getGroupId());
     $newLabel = null;
     if ($dstMdFactory->isRealMetadata($srcFilter->md->getLabel())) {
         // Check if there is a corresponding MD in the dst project
         // Should never happens in case of initial template clone
         // but main exists with 'clone this report' function
         if (isset($metadataMapping['md'][$srcFilter->md->getId()])) {
             // For real metadata, create MD based on the new ID
             $newLabel = 'field_' . $metadataMapping['md'][$srcFilter->md->getId()];
         }
     } else {
         // Check in use
         $newLabel = $srcFilter->md->getLabel();
     }
     if ($newLabel !== null) {
         $dstFilterFactory = $this->getFilterFactory($dstReport->getGroupId());
         $gsMd = $this->getGlobalSearchMetadata();
         if ($newLabel == $gsMd->getLabel()) {
             $newMd = $gsMd;
         } else {
             $itMd = $this->getItemTypeSearchMetadata();
             if ($newLabel == $itMd->getLabel()) {
                 $newMd = $itMd;
             } else {
                 $newMd = $dstMdFactory->getFromLabel($newLabel);
             }
         }
         if ($newMd->isUsed()) {
             // Create new filter
             $dstFilter = $dstFilterFactory->createFromMetadata($newMd, $dstReport->getAdvancedSearch());
             // Append values
             $this->cloneFilterValues($srcFilter, $dstFilter, $metadataMapping);
             // Save filter
             $dstFilterFactory->createFilter($dstReport->getId(), $dstFilter);
         }
     }
 }
 function getMetadataCompareTable(&$sthToImport)
 {
     $html = '';
     // True if there is sth to import in dst project.
     $sthToImport = false;
     // For source project, only get the 'Used' metadata.
     $srcMdFactory = new Docman_MetadataFactory($this->srcGo->getGroupId());
     $srcMdIter = $srcMdFactory->getMetadataForGroup(true);
     // For destination (current) project, get all metadata.
     $dstMdFactory = new Docman_MetadataFactory($this->dstGo->getGroupId());
     $dstMdIter = $dstMdFactory->getMetadataForGroup();
     $dstMdArray = $this->getArrayFromIterator($dstMdIter, 'getLabel');
     // Get mapping between the 2 definitions
     $mdMap = array();
     $srcMdFactory->getMetadataMapping($this->dstGo->getGroupId(), $mdMap);
     $html .= $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_desc', array($this->dstGo->getPublicName(), $this->srcGo->getPublicName()));
     // Table
     $html .= "<table border=\"1\">\n";
     $html .= "<tr>\n";
     $html .= "<th colspan=\"2\">" . $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_tbl_prop') . "</th>\n";
     $html .= "<th>" . $this->srcGo->getPublicName() . "</th>\n";
     $html .= "<th>" . $this->dstGo->getPublicName() . "</th>\n";
     $html .= "<th>" . $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_tbl_diff', array($this->dstGo->getPublicName(), $this->srcGo->getPublicName())) . "</th>\n";
     $html .= "<th>" . $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_tbl_action', array($this->dstGo->getPublicName())) . "</th>\n";
     $html .= "</tr>\n";
     // Keep a trace of metadata that matched in the dst metadata list.
     $matchingMd = array();
     $srcMdIter->rewind();
     while ($srcMdIter->valid()) {
         $srcMd = $srcMdIter->current();
         $dstMd = null;
         //
         // Compute the differences between the 2 projects
         //
         $dstMdStatus = 'missing';
         $dstMdLabel = '';
         if ($srcMdFactory->isRealMetadata($srcMd->getLabel())) {
             if (isset($mdMap['md'][$srcMd->getId()])) {
                 $dstMdLabel = $srcMdFactory->getLabelFromId($mdMap['md'][$srcMd->getId()]);
             }
         } else {
             $dstMdLabel = $srcMd->getLabel();
         }
         if (isset($dstMdArray[$dstMdLabel])) {
             $dstMd = $dstMdArray[$dstMdLabel];
             if ($dstMd !== false) {
                 $matchingMd[$dstMdLabel] = true;
                 $dstMdStatus = 'equivalent';
                 if ($dstMd->equals($srcMd)) {
                     $dstMdStatus = 'equals';
                 } else {
                     $sthToImport = true;
                 }
             } else {
                 $sthToImport = true;
             }
         } else {
             // The metadata is not in the metadata map list, check if it's
             // not a name conflict
             $dstMdi = $dstMdFactory->findByName($srcMd->getName());
             if ($dstMdi->count() == 1) {
                 $dstMdStatus = 'conflict';
             } else {
                 $sthToImport = true;
             }
         }
         //
         // Display result
         //
         $html .= "<tr>\n";
         // Property
         $html .= "<td colspan=\"2\" style=\"font-weight: bold;\">";
         $html .= $srcMd->getName();
         $html .= "</td>";
         // Presence in source project
         $html .= "<td align=\"center\">";
         $html .= '<img src="' . $this->docmanIcons->getThemeIcon('tick.png') . '" />';
         $html .= "</td>";
         // Presence in destination project
         $html .= "<td align=\"center\">";
         switch ($dstMdStatus) {
             case 'equals':
             case 'equivalent':
                 $html .= '<img src="' . $this->docmanIcons->getThemeIcon('tick.png') . '" />';
                 break;
         }
         $html .= "</td>";
         // Differences
         $html .= "<td class=\"docman_md_" . $dstMdStatus . "\">";
         switch ($dstMdStatus) {
             case 'equivalent':
             case 'missing':
             case 'conflict':
                 $html .= $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_tbl_status_' . $dstMdStatus);
                 break;
         }
         $html .= "</td>";
         // Action
         $html .= "<td>";
         switch ($dstMdStatus) {
             case 'equals':
                 // Nothing to do
                 break;
             case 'equivalent':
                 $diffArray = $this->checkMdDifferences($srcMd, $dstMd, $mdMap['love']);
                 $diffStr = '<ul style="padding:0;padding-left:1.5em;margin:0;"><li>';
                 $diffStr .= implode('</li><li>', $diffArray);
                 $diffStr .= '</li></ul>';
                 $html .= $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_tbl_act_update_md', array($srcMd->getName(), $this->dstGo->getPublicName(), $diffStr));
                 break;
             case 'missing':
                 $html .= $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_tbl_act_import_md', array($srcMd->getName()));
                 break;
             case 'conflict':
                 $html .= $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_tbl_act_conflict');
                 break;
         }
         $html .= "</td>";
         $html .= "</tr>\n";
         //
         // List of values
         //
         if ($srcMd->getType() == PLUGIN_DOCMAN_METADATA_TYPE_LIST) {
             if ($dstMd !== null) {
                 $html .= $this->getLoveCompareTable($srcMd, $dstMd, $mdMap, $sthToImport);
             }
         }
         unset($dstMd);
         $srcMdIter->next();
     }
     // Append to the table the metadata in the dst project that where not
     // present in the src project.
     foreach ($dstMdArray as $md) {
         if (!isset($matchingMd[$md->getLabel()])) {
             $html .= "<tr>\n";
             // Name
             $html .= "<td colspan=\"2\" style=\"font-weight: bold;\">";
             $html .= $md->getName();
             $html .= "</td>";
             // Presence in source project
             $html .= "<td></td>";
             // Presence in destination project
             $html .= "<td align=\"center\">";
             $html .= '<img src="' . $this->docmanIcons->getThemeIcon('tick.png') . '" />';
             $html .= "</td>";
             // Differences
             $html .= "<td></td>";
             // Action
             $html .= "<td></td>";
             $html .= "</td>";
             $html .= "</tr>\n";
         }
     }
     $html .= "</table>\n";
     return $html;
 }