/** * get records and resolve fields * * @return Tinebase_Record_RecordSet */ protected function _getRecords() { $pagination = !empty($this->_options['sortInfo']) ? new Tinebase_Model_Pagination($this->_options['sortInfo']) : new Tinebase_Model_Pagination(); if ($this->_recordIds === NULL) { // need to fetch record ids first because filtered fields can change during iteration step if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Getting record ids using filter: ' . print_r($this->_filter->toArray(), TRUE) . ' and pagination: ' . print_r($pagination->toArray(), true)); } $this->_recordIds = $this->_controller->search($this->_filter, $pagination, FALSE, TRUE, $this->_options['searchAction']); $this->_totalCount = count($this->_recordIds); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Found ' . $this->_totalCount . ' total records to process.'); } if (empty($this->_recordIds)) { return new Tinebase_Record_RecordSet($this->_filter->getModelName()); } } // get records by filter (ensure acl) $filterClassname = get_class($this->_filter); $recordIdsForIteration = array_splice($this->_recordIds, 0, $this->_options['limit']); $idFilter = new $filterClassname(array(array('field' => isset($this->_options['idProperty']) || array_key_exists('idProperty', $this->_options) ? $this->_options['idProperty'] : 'id', 'operator' => 'in', 'value' => $recordIdsForIteration))); $records = $this->_controller->search($idFilter, $pagination, $this->_options['getRelations']); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Got ' . count($records) . ' for next iteration.'); } return $records; }
/** * get export object for given filter and format * * @param Tinebase_Model_Filter_FilterGroup $_filter * @param string|array $_options format (as string) or export definition id (array) * @param Tinebase_Controller_Record_Interface $_controller (optional) * @param array $_additionalOptions (optional) * @return Tinebase_Export_Abstract * @throws Tinebase_Exception_NotFound */ public static function factory($_filter, $_options, $_controller = NULL, $_additionalOptions = array()) { if (!is_array($_options)) { $_options = array('format' => $_options); } if (array_key_exists('definitionId', $_options)) { $definition = Tinebase_ImportExportDefinition::getInstance()->get($_options['definitionId']); $exportClass = $definition->plugin; // export plugin needs the definition id $_additionalOptions = array_merge($_additionalOptions, $_options); } else { if (array_key_exists('format', $_options) && !empty($_options['format'])) { $appName = $_filter->getApplicationName(); $model = $_filter->getModelName(); $exportClass = $appName . '_Export_' . ucfirst(strtolower($_options['format'])); if (!@class_exists($exportClass)) { // check for model specific export class list($a, $b, $modelPart) = explode('_', $model); $exportClass = $exportClass . '_' . $modelPart; if (!@class_exists($exportClass)) { throw new Tinebase_Exception_NotFound('No ' . $_options['format'] . ' export class found for ' . $appName . ' / ' . $model); } } } else { throw new Tinebase_Exception_InvalidArgument('Export definition ID or format required in options'); } } if (preg_match('/pdf/i', $exportClass)) { // legacy $result = new $exportClass($_additionalOptions); } else { $result = new $exportClass($_filter, $_controller, $_additionalOptions); } return $result; }
/** * get export object for given filter and format * * @param Tinebase_Model_Filter_FilterGroup $_filter * @param string|array $_options format (as string) or export definition id (array) * @param Tinebase_Controller_Record_Interface $_controller (optional) * @param array $_additionalOptions (optional) * @return Tinebase_Export_Abstract * @throws Tinebase_Exception_NotFound */ public static function factory($_filter, $_options, $_controller = NULL, $_additionalOptions = array()) { if (!is_array($_options)) { $_options = array('format' => $_options); } // always merge options? this needs to be refactored! $_additionalOptions = array_merge($_additionalOptions, $_options); if (isset($_options['definitionId']) || array_key_exists('definitionId', $_options)) { $definition = Tinebase_ImportExportDefinition::getInstance()->get($_options['definitionId']); $exportClass = $definition->plugin; } else { if ((isset($_options['format']) || array_key_exists('format', $_options)) && !empty($_options['format'])) { $appName = $_filter->getApplicationName(); $model = $_filter->getModelName(); $exportClass = $appName . '_Export_' . ucfirst(strtolower($_options['format'])); // start output buffering to catch errors, append them to log and exception ob_start(); if (!class_exists($exportClass)) { $ob = ob_get_length() > 0 ? ob_get_clean() : ''; // check for model specific export class list($a, $b, $modelPart) = explode('_', $model); $exportClass2 = $exportClass . '_' . $modelPart; if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->log(__METHOD__ . '::' . __LINE__ . ' Could not find class ' . $exportClass . ' trying ' . $exportClass2 . '. Output Buffer: ' . PHP_EOL . $ob, Zend_Log::NOTICE); } if (!class_exists($exportClass2)) { $ob = ob_get_length() > 0 ? ob_get_clean() : NULL; ob_end_flush(); throw new Tinebase_Exception_NotFound('No ' . $_options['format'] . ' export class found for ' . $appName . ' / ' . $model . '. ClassName: ' . $exportClass2 . ($ob ? 'Output: ' . $ob : '')); } else { $exportClass = $exportClass2; } } ob_end_flush(); } else { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Export options: ' . print_r($_options, TRUE)); } throw new Tinebase_Exception_InvalidArgument('Export definition ID or format required in options'); } } if (preg_match('/pdf/i', $exportClass)) { // legacy $result = new $exportClass($_additionalOptions); } else { $result = new $exportClass($_filter, $_controller, $_additionalOptions); } return $result; }
/** * Removes containers where current user has no access to * * @param Tinebase_Model_Filter_FilterGroup $_filter * @param string $_action get|update */ public function checkFilterACL(Tinebase_Model_Filter_FilterGroup $_filter, $_action = 'get') { if (!$this->_doContainerACLChecks) { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Container ACL disabled for ' . $_filter->getModelName() . '.'); } return TRUE; } $aclFilters = $_filter->getAclFilters(); if (!$aclFilters) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Force a standard containerFilter (specialNode = all) as ACL filter.'); } $containerFilter = $_filter->createFilter('container_id', 'specialNode', 'all', array('applicationName' => $_filter->getApplicationName())); $_filter->addFilter($containerFilter); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Setting filter grants for action ' . $_action); } switch ($_action) { case 'get': $_filter->setRequiredGrants(array(Tinebase_Model_Grants::GRANT_READ, Tinebase_Model_Grants::GRANT_ADMIN)); break; case 'update': $_filter->setRequiredGrants(array(Tinebase_Model_Grants::GRANT_EDIT, Tinebase_Model_Grants::GRANT_ADMIN)); break; case 'export': $_filter->setRequiredGrants(array(Tinebase_Model_Grants::GRANT_EXPORT, Tinebase_Model_Grants::GRANT_ADMIN)); break; case 'sync': $_filter->setRequiredGrants(array(Tinebase_Model_Grants::GRANT_SYNC, Tinebase_Model_Grants::GRANT_ADMIN)); break; default: throw new Tinebase_Exception_UnexpectedValue('Unknown action: ' . $_action); } }
/** * detach tag from multiple records identified by a filter * * @param Tinebase_Model_Filter_FilterGroup $_filter * @param mixed $_tag string|array|Tinebase_Model_Tag with existing and non-existing tag * @return void * * @todo maybe this could be done in a more generic way (in Tinebase_Controller_Record_Abstract) */ public function detachTagsFromMultipleRecords($_filter, $_tag) { list($appName, $i, $modelName) = explode('_', $_filter->getModelName()); $appId = Tinebase_Application::getInstance()->getApplicationByName($appName)->getId(); $controller = Tinebase_Core::getApplicationInstance($appName, $modelName); // only get records user has update rights to $controller->checkFilterACL($_filter, 'update'); $recordIds = $controller->search($_filter, NULL, FALSE, TRUE); foreach ((array) $_tag as $dirtyTagId) { try { $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction($this->_db); $this->_detachSingleTag($recordIds, $dirtyTagId, $appId, $controller); Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId); } catch (Exception $e) { Tinebase_TransactionManager::getInstance()->rollBack(); Tinebase_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ' ' . print_r($e->getMessage(), true)); throw $e; } } }
/** * the constructor * * @param Tinebase_Model_Filter_FilterGroup $_filter * @param Tinebase_Controller_Record_Interface $_controller (optional) * @param array $_additionalOptions (optional) additional options */ public function __construct(Tinebase_Model_Filter_FilterGroup $_filter, Tinebase_Controller_Record_Interface $_controller = NULL, $_additionalOptions = array()) { // init member vars $this->_modelName = $_filter->getModelName(); $this->_filter = $_filter; $this->_controller = $_controller !== NULL ? $_controller : Tinebase_Core::getApplicationInstance($this->_applicationName, $this->_modelName); $this->_translate = Tinebase_Translation::getTranslation($this->_applicationName); $this->_config = $this->_getExportConfig($_additionalOptions); $this->_locale = Tinebase_Core::get(Tinebase_Core::LOCALE); }
/** * the constructor * * @param Tinebase_Model_Filter_FilterGroup $_filter * @param Tinebase_Controller_Record_Interface $_controller (optional) * @param array $_additionalOptions (optional) additional options */ public function __construct(Tinebase_Model_Filter_FilterGroup $_filter, Tinebase_Controller_Record_Interface $_controller = NULL, $_additionalOptions = array()) { $this->_modelName = $_filter->getModelName(); $this->_filter = $_filter; $this->_controller = $_controller !== NULL ? $_controller : Tinebase_Core::getApplicationInstance($this->_applicationName, $this->_modelName); $this->_translate = Tinebase_Translation::getTranslation($this->_applicationName); $this->_config = $this->_getExportConfig($_additionalOptions); $this->_locale = Tinebase_Core::get(Tinebase_Core::LOCALE); if (isset($_additionalOptions['sortInfo'])) { if (isset($_additionalOptions['sortInfo']['field'])) { $this->_sortInfo['sort'] = $_additionalOptions['sortInfo']['field']; $this->_sortInfo['dir'] = isset($_additionalOptions['sortInfo']['direction']) ? $_additionalOptions['sortInfo']['direction'] : 'ASC'; } else { $this->_sortInfo = $_additionalOptions['sortInfo']; } } }
/** * detach tag from multiple records identified by a filter * * @param Tinebase_Model_Filter_FilterGroup $_filter * @param mixed $_tag string|array|Tinebase_Model_Tag with existing and non-existing tag * @return void */ public function detachTagsFromMultipleRecords($_filter, $_tag) { list($appName, $i, $modelName) = explode('_', $_filter->getModelName()); $appId = Tinebase_Application::getInstance()->getApplicationByName($appName)->getId(); $controller = Tinebase_Core::getApplicationInstance($appName, $modelName); if (!is_array($_tag)) { $_tag = array($_tag); } foreach ($_tag as $dirtyTagId) { $tag = $this->getTagsById($dirtyTagId, Tinebase_Model_TagRight::USE_RIGHT)->getFirstRecord(); if (empty($tag)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' No use right for tag, detaching not possible.'); return; } $tagId = $tag->getId(); // only get records user has update rights to $controller->checkFilterACL($_filter, 'update'); $recordIds = $controller->search($_filter, NULL, FALSE, TRUE); $recordIdList = '\'' . implode('\',\'', $recordIds) . '\''; $attachedIds = array(); $select = $this->_db->select()->from(array('tagging' => SQL_TABLE_PREFIX . 'tagging'), 'record_id')->where($this->_db->quoteIdentifier('application_id') . ' = ?', $appId)->where($this->_db->quoteIdentifier('tag_id') . ' = ? ', $tagId)->where($this->_db->quoteIdentifier('record_id') . ' IN ( ' . $recordIdList . ' ) '); foreach ($this->_db->fetchAssoc($select) as $tagArray) { $attachedIds[] = $tagArray['record_id']; } if (empty($attachedIds)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' There are no records we could detach the tag(s) from'); return; } Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Detaching 1 Tag from ' . count($attachedIds) . ' records.'); foreach ($attachedIds as $recordId) { $this->_db->delete(SQL_TABLE_PREFIX . 'tagging', 'tag_id=\'' . $tagId . '\' AND record_id=\'' . $recordId . '\' AND application_id=\'' . $appId . '\''); } $controller->concurrencyManagementAndModlogMultiple($attachedIds, array('tags' => array($tag->toArray())), array('tags' => array())); $this->_deleteOccurrence($tagId, count($attachedIds)); } }