public function getMetadataValuesIterator($fieldId)
 {
     $dar = $this->dao->searchByFieldId($fieldId);
     $valuesArr = array();
     $dar->rewind();
     while ($dar->valid()) {
         $row = $dar->current();
         $wmdv = new DocmanWatermark_MetadataValue();
         $wmdv->setValueId($row['value_id']);
         $wmdv->setWatermark($row['watermark']);
         $valuesArr[] = $wmdv;
         $dar->next();
     }
     return new ArrayIterator($valuesArr);
 }
 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;
     }
 }
 /**
  * Private Method to copy the metadata values setup from the src project to target project
  * @param  ArrayIterator(int) $dwmvs : iterator of metadata values  to insert in the table plugin_docman_watermark_love_md_extension
  * @return void
  */
 private function copyWatermarkMetadataValues($loves)
 {
     $arrdwmv = array();
     for ($i = 0; $i < count($loves); $i++) {
         $dwmv = new DocmanWatermark_MetadataValue();
         $dwmv->setValueId($loves[$i]['value_id']);
         $dwmv->setWatermark($loves[$i]['watermark']);
         $arrdwmv[] = $dwmv;
     }
     // update watermarking settings related to the source project
     $dwmvf = new DocmanWatermark_MetadataValueFactory();
     $dwmvf->updateMetadataValues(new ArrayIterator($arrdwmv), $this->targetProjectId);
 }