/**
  * Checks that the updating property have a non-empty name,
  * and if the name have been changed, also checks that the same
  * name is not already taken by another property
  */
 private function validateUpdateMetadata($name, $label)
 {
     $name = trim($name);
     if ($name == '') {
         $valid = false;
         $this->feedback->log('error', $GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_new_name_missing'));
     } else {
         $mdFactory = new Docman_MetadataFactory($this->groupId);
         $md = $mdFactory->getFromLabel($label);
         // name has changed
         if ($md !== null && $md->getName() != $name) {
             if ($mdFactory->findByName($name)->count() == 0) {
                 $valid = true;
             } else {
                 $valid = false;
                 $this->feedback->log('error', $GLOBALS['Language']->getText('plugin_docman', 'admin_metadata_new_name_exists', $name));
             }
         } else {
             $valid = true;
         }
     }
     return $valid;
 }
 function _dispatch($view)
 {
     $user = $this->getUser();
     $dpm = Docman_PermissionsManager::instance($this->getGroupId());
     switch ($view) {
         case 'admin_watermark':
             $group_id = $this->request->get('group_id');
             $dwmdf = new DocmanWatermark_MetadataFactory();
             $md_id = $dwmdf->getMetadataIdFromGroupId($group_id);
             $this->_viewParams['md_id'] = $md_id;
             $this->_viewParams['group_id'] = $group_id;
             $md_arr = array();
             $dmf = new Docman_MetadataFactory($group_id);
             $mdIter = $dmf->getMetadataForGroup(true);
             $mdIter->rewind();
             while ($mdIter->valid()) {
                 $md = $mdIter->current();
                 $md_arr[] = $md->getId();
                 $mdIter->next();
             }
             if ($md_id != 0 && !in_array($md_id, $md_arr)) {
                 $this->feedback->log('warning', $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_metadata_deleted'));
             }
             $this->view = 'AdminWatermark';
             break;
         case 'admin_set_watermark_metadata':
             $group_id = $this->request->get('group_id');
             $id = $this->request->get('md_id');
             $this->_actionParams['group_id'] = $group_id;
             $this->_actionParams['md_id'] = $id;
             $dwm = new DocmanWatermark_Metadata();
             $dwm->setId($id);
             $dwm->setGroupId($group_id);
             $this->action = 'setup_metadata';
             $this->feedback->log('info', $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_update_metadata'));
             $this->_viewParams['redirect_to'] = '?group_id=' . $group_id . '&action=admin_watermark';
             $this->view = 'RedirectAfterCrud';
             break;
         case 'admin_set_watermark_metadata_values':
             $mdf = new Docman_MetadataFactory($this->request->get('group_id'));
             $dwmf = new DocmanWatermark_MetadataFactory();
             $md_id = $dwmf->getMetadataIdFromGroupId($this->request->get('group_id'));
             $mdLabel = $mdf->getLabelFromId($md_id);
             $mlvef = new Docman_MetadataListOfValuesElementFactory($md_id);
             $mlveIter = $mlvef->getIteratorByFieldId($md_id, $mdLabel, true);
             $mlveIter->rewind();
             $arrValues = array();
             $arrVals = array();
             while ($mlveIter->valid()) {
                 $dmv = $mlveIter->current();
                 $dwmv = new DocmanWatermark_MetadataValue();
                 if ($this->request->exist('chk_' . $dmv->getId())) {
                     $watermark = 1;
                 } else {
                     $watermark = 0;
                 }
                 $dwmv->setValueId($dmv->getId());
                 $dwmv->setWatermark($watermark);
                 $arrValues[] = $dwmv;
                 $arrVals['value_id'][] = $dmv->getId();
                 $arrVals['watermark'][] = $watermark;
                 $mlveIter->next();
             }
             $iterValues = new ArrayIterator($arrValues);
             $this->_actionParams['group_id'] = $this->request->get('group_id');
             $this->_actionParams['md_values'] = $iterValues;
             $this->action = 'setup_metadata_values';
             $this->feedback->log('info', $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_update_metadata_values'));
             $this->_viewParams['redirect_to'] = '?group_id=' . $this->request->get('group_id') . '&action=admin_watermark';
             $this->view = 'RedirectAfterCrud';
             break;
         case 'admin_import_from_project':
             $this->_actionParams['src_group_id'] = $this->request->get('project');
             $this->_actionParams['target_group_id'] = $this->request->get('group_id');
             $dwmif = new DocmanWatermark_MetadataImportFactory();
             $dwmif->setSrcProjectId($this->request->get('project'));
             $dwmif->setTargetProjectId($this->request->get('group_id'));
             $dmf = new Docman_MetadataFactory($this->request->get('project'));
             $dwmf = new DocmanWatermark_MetadataFactory();
             $md_id = $dwmf->getMetadataIdFromGroupId($dwmif->getSrcProjectId());
             $mdIter = $dmf->findByName($dwmf->getMetadataNameFromId($md_id));
             $mdIter->rewind();
             $md = $mdIter->current();
             $this->_actionParams['md'] = $md;
             $mdMap = $dwmif->getWatermarkMetadataMap($md);
             if ($mdMap['md'] != 0) {
                 $this->action = 'import_from_project';
                 $this->feedback->log('info', $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_imported_from_project'));
                 $this->_viewParams['md_id'] = $md_id;
                 $this->_viewParams['redirect_to'] = '?group_id=' . $this->request->get('group_id') . '&action=admin_watermark';
             } else {
                 $this->feedback->log('info', $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_import_from_project_not_match'));
                 $this->_viewParams['redirect_to'] = '?group_id=' . $this->request->get('group_id') . '&action=admin_import_metadata_check&import_group_id=' . $this->request->get('project');
             }
             $this->view = 'RedirectAfterCrud';
             break;
         case 'docmanwatermark_toggle_item':
             $this->action = 'docmanwatermark_toggle_item';
             break;
         default:
             die(htmlspecialchars($view) . ' is not supported');
             break;
     }
 }
 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;
 }