function getMetaDataValuesTable($groupId, $mdId, $vals)
 {
     $mdf = new Docman_MetadataFactory($groupId);
     $mdLabel = $mdf->getLabelFromId($mdId);
     $mlvef = new Docman_MetadataListOfValuesElementFactory($mdId);
     $mlveIter = $mlvef->getIteratorByFieldId($mdId, $mdLabel, true);
     $html = '<h3>' . $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_confidentiality_field_values') . '</h3>';
     $html .= '<p>' . $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_select_field_values');
     $html .= '<form name="metadata_field_values" method="post" action="?group_id=' . $groupId . '&action=admin_set_watermark_metadata_values">';
     $titles = array();
     $titles[] = $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_use_watermark');
     $titles[] = $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_values');
     $mlveIter->rewind();
     $iter_empty = 1;
     if ($mlveIter->valid()) {
         $iter_empty = 0;
         $html .= html_build_list_table_top($titles, false, false, false);
     }
     $i = 1;
     while ($mlveIter->valid()) {
         $mdv = $mlveIter->current();
         $id = $mdv->getId();
         if (isset($vals['value_id'])) {
             $posValue = array_search($id, $vals['value_id']);
         } else {
             $posValue = false;
         }
         // exclude status field and None Value
         if ($id != 100 && $mdv->getName() != 'Status') {
             $name = $mdv->getName();
             $html .= '<tr class="' . html_get_alt_row_color($i) . '"><td align="center"><input type="checkbox" name="chk_' . $id . '"';
             if ($vals['watermark'][$posValue] == 1 && $posValue !== false) {
                 $html .= ' checked ';
             }
             $html .= '/></td>';
             $html .= '<td><b>' . $name . '</b></td>';
             $html .= '</tr>';
             $i++;
         }
         $mlveIter->next();
     }
     if (!$iter_empty) {
         $html .= '</table>';
         $html .= '<input name="submit_metadatafield_value" type="submit" value="' . $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_save_settings') . '">';
     } else {
         $html .= '<b>' . $GLOBALS['Language']->getText('plugin_docmanwatermark', 'admin_disabled') . '</b>';
     }
     $html .= '</form>';
     return $html;
 }
 /**
  * Public Method to create the watermark metadata map in the target project (customized for the import purpose)
  * @param  Docman_Metadata object dm: the metadata object to be checked
  * @return array ('md' => int matched metadataId, 'love' => related loves)
  */
 public function getWatermarkMetadataMap($md)
 {
     // get the metadata iterator from target project
     $dmd = new Docman_MetadataDao(CodendiDataAccess::instance());
     $dar = $dmd->searchByName($this->targetProjectId, $md->getName());
     // if the name doesn't match then return md = 0
     $dar->rewind();
     if (!$dar->valid()) {
         return array('md' => 0, 'love' => array());
     } else {
         // check metadata properties type, useIt, AllowMultipleValue if it doesn't much then return md = 0
         while ($dar->valid()) {
             $row = $dar->current();
             if ($row['use_it'] == $md->isUsed() && $row['data_type'] == $md->getType() && $row['mul_val_ok'] == $md->isMultipleValuesAllowed()) {
                 // it seams that the metadata is ok
                 // check now if source Metadata values already exist
                 $dmlovef = new Docman_MetadataListOfValuesElementFactory($md->getId());
                 $loveIter = $dmlovef->getIteratorByFieldId($md->getId(), $md->getLabel(), true);
                 $dmloveft = new Docman_MetadataListOfValuesElementFactory($row['field_id']);
                 $loveIter->rewind();
                 $i = 0;
                 $loves = array();
                 while ($loveIter->valid()) {
                     $love = $loveIter->current();
                     $lovetIter = $dmloveft->getByName($love->getName(), $row['field_id'], true);
                     $lovetIter->rewind();
                     if (!$lovetIter->valid()) {
                         return array('md' => 0, 'love' => array());
                     }
                     $lovet = $lovetIter->current();
                     $dwmdvf = new DocmanWatermark_MetadataValueFactory();
                     $loves[$i]['value_id'] = $lovet->getId();
                     $loves[$i]['watermark'] = $dwmdvf->isWatermarkedOnValue($love->getId());
                     $i++;
                     $loveIter->next();
                 }
                 return array('md' => $row['field_id'], 'love' => $loves);
             }
             $dar->next();
         }
         return array('md' => 0, 'love' => array());
     }
 }
 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;
     }
 }
 /**
  *
  * Same algo used in Docman_View_ItemDetailsSectionPaste::_checkLoveToImport
  */
 function getLoveCompareTable($srcMd, $dstMd, $mdMap, &$sthToImport)
 {
     $html = '';
     if ($srcMd->getLabel() == 'status') {
         // No differences possible with status.
         return $html;
     }
     // Get list of ListOfValues elements from dst project
     $srcLoveFactory = new Docman_MetadataListOfValuesElementFactory($srcMd->getId());
     $srcLoveIter = $srcLoveFactory->getIteratorByFieldId($srcMd->getId(), $srcMd->getLabel(), true);
     // Get list of ListOfValues elements from dst project
     $dstLoveFactory = new Docman_MetadataListOfValuesElementFactory($dstMd->getId());
     $dstLoveIter = $dstLoveFactory->getIteratorByFieldId($dstMd->getId(), $dstMd->getLabel(), true);
     $dstLoveArray = $this->getArrayFromIterator($dstLoveIter, 'getId');
     $maxRow = max($srcLoveIter->count(), $dstLoveIter->count());
     // Keep a trace of matching love
     $matchingLove = array();
     while ($srcLoveIter->valid()) {
         $srcLove = $srcLoveIter->current();
         $rowStyle = 'missing';
         // Compute the differences
         $dstLove = false;
         if (isset($mdMap['love'][$srcLove->getId()])) {
             $dstLove = $dstLoveArray[$mdMap['love'][$srcLove->getId()]];
             $matchingLove[$dstLove->getId()] = true;
             $rowStyle = 'equals';
         } else {
             $sthToImport = true;
         }
         $html .= "<tr>\n";
         // Name
         $html .= "<td style=\"padding-left: 2em;\"></td>\n";
         $html .= "<td>" . Docman_MetadataHtmlList::_getElementName($srcLove) . "</td>\n";
         // Presence in source project
         $html .= '<td align="center"><img src="' . $this->docmanIcons->getThemeIcon('tick.png') . '" /></td>';
         // Presence in destination project
         $html .= "<td align=\"center\">";
         switch ($rowStyle) {
             case 'equals':
                 $html .= '<img src="' . $this->docmanIcons->getThemeIcon('tick.png') . '" />';
                 break;
         }
         $html .= "</td>\n";
         // Differences
         $html .= "<td class=\"docman_md_" . $rowStyle . "\">";
         switch ($rowStyle) {
             case 'missing':
                 $html .= $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_tbl_status_' . $rowStyle);
         }
         $html .= "</td>\n";
         // Action
         $html .= "<td>";
         switch ($rowStyle) {
             case 'missing':
                 $html .= $GLOBALS['Language']->getText('plugin_docman', 'admin_md_import_tbl_act_import_love', array($srcLove->getName()));
         }
         $html .= "</td\n>";
         $html .= "</tr>\n";
         $srcLoveIter->next();
     }
     // Append to the table the list of values elements in the dst project
     // that where not present in the src project.
     foreach ($dstLoveArray as $love) {
         if (!isset($matchingLove[$love->getId()])) {
             $html .= "<tr>\n";
             // Name
             $html .= "<td>&nbsp;</td>\n";
             $html .= "<td>" . $love->getName() . "</td>\n";
             // Presence in source project
             $html .= "<td></td>\n";
             // Presence in destination project
             $html .= '<td align="center"><img src="' . $this->docmanIcons->getThemeIcon('tick.png') . '" /></td>';
             // Differences
             $html .= "<td></td>\n";
             // Action
             $html .= "<td></td>\n";
             $html .= "</tr>\n";
         }
     }
     return $html;
 }