/** * get grants for records * * @param Tinebase_Record_RecordSet $records */ public function getGrantsForRecords(Tinebase_Record_RecordSet $records) { $recordIds = $records->getArrayOfIds(); if (empty($recordIds)) { return; } $select = $this->_getAclSelectByRecordIds($recordIds)->group(array('record_id', 'account_type', 'account_id')); Tinebase_Backend_Sql_Abstract::traitGroup($select); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $select); } $stmt = $this->_db->query($select); $grantsData = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' grantsData: ' . print_r($grantsData, true)); } foreach ($grantsData as $grantData) { $givenGrants = explode(',', $grantData['account_grants']); foreach ($givenGrants as $grant) { $grantData[$grant] = TRUE; } $recordGrant = new $this->_modelName($grantData, true); unset($recordGrant->account_grant); $record = $records->getById($recordGrant->record_id); if (!$record->grants instanceof Tinebase_Record_RecordSet) { $record->grants = new Tinebase_Record_RecordSet($this->_modelName); } $record->grants->addRecord($recordGrant); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Records with grants: ' . print_r($records->toArray(), true)); } }
/** * Return a single record * * @param string $id * @return array record data */ public function getInvoice($id) { $invoice = $this->_get($id, Sales_Controller_Invoice::getInstance()); $json = new Tinebase_Convert_Json(); $resolvedProducts = new Tinebase_Record_RecordSet('Sales_Model_Product'); $productController = Sales_Controller_Product::getInstance(); foreach ($invoice['relations'] as &$relation) { if ($relation['related_model'] == "Sales_Model_ProductAggregate") { if (!($product = $resolvedProducts->getById($relation['related_record']['product_id']))) { $product = $productController->get($relation['related_record']['product_id']); $resolvedProducts->addRecord($product); } $relation['related_record']['product_id'] = $json->fromTine20Model($product); } } return $invoice; }
/** * compares two recordsets / only compares the ids / returns all records that are different in an array: * - removed -> all records that are in $this but not in $_recordSet * - added -> all records that are in $_recordSet but not in $this * - modified -> array of diffs for all different records that are in both record sets * * @param Tinebase_Record_RecordSet $recordSet * @return Tinebase_Record_RecordSetDiff */ public function diff($recordSet) { if (!$recordSet instanceof Tinebase_Record_RecordSet) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Did not get Tinebase_Record_RecordSet, skipping diff(' . $this->_recordClass . ')'); } return new Tinebase_Record_RecordSetDiff(array('model' => $this->getRecordClassName())); } if ($this->getRecordClassName() !== $recordSet->getRecordClassName()) { throw new Tinebase_Exception_InvalidArgument('can only compare recordsets with the same type of records'); } $existingRecordsIds = $this->getArrayOfIds(); $toCompareWithRecordsIds = $recordSet->getArrayOfIds(); $removedIds = array_diff($existingRecordsIds, $toCompareWithRecordsIds); $addedIds = array_diff($toCompareWithRecordsIds, $existingRecordsIds); $modifiedIds = array_intersect($existingRecordsIds, $toCompareWithRecordsIds); $removed = new Tinebase_Record_RecordSet($this->getRecordClassName()); $added = new Tinebase_Record_RecordSet($this->getRecordClassName()); $modified = new Tinebase_Record_RecordSet('Tinebase_Record_Diff'); foreach ($addedIds as $id) { $added->addRecord($recordSet->getById($id)); } // consider records without id, too foreach ($recordSet->getIdLessIndexes() as $index) { $added->addRecord($recordSet->getByIndex($index)); } foreach ($removedIds as $id) { $removed->addRecord($this->getById($id)); } // consider records without id, too foreach ($this->getIdLessIndexes() as $index) { $removed->addRecord($this->getByIndex($index)); } foreach ($modifiedIds as $id) { $diff = $this->getById($id)->diff($recordSet->getById($id)); if (!$diff->isEmpty()) { $modified->addRecord($diff); } } $result = new Tinebase_Record_RecordSetDiff(array('model' => $this->getRecordClassName(), 'added' => $added, 'removed' => $removed, 'modified' => $modified)); return $result; }
/** * sends a notification email to all responsibles of imported leads * * @throws Tinebase_Exception_Record_NotAllowed * * @see TODO add issue */ protected function _sendNotificationToResponsibles() { $responsibles = new Tinebase_Record_RecordSet('Addressbook_Model_Contact'); $leadsByResponsibleId = array(); foreach ($this->_importResult['results'] as $importedRecord) { // add responsibles if not already in record set and add lead to $leadsByResponsibleId $responsiblesForImportedLead = $importedRecord->getResponsibles(); foreach ($responsiblesForImportedLead as $responsible) { if (!$responsibles->getById($responsible->getId())) { $responsibles->addRecord($responsible); $leadsByResponsibleId[$responsible->getId()] = new Tinebase_Record_RecordSet('Crm_Model_Lead'); } $leadsByResponsibleId[$responsible->getId()]->addRecord($importedRecord); } } foreach ($responsibles as $responsible) { if (isset($leadsByResponsibleId[$responsible->getId()])) { $this->_sendNotificationToResponsible($responsible, $leadsByResponsibleId[$responsible->getId()]); } } }
/** * compares two recordsets / only compares the ids / returns all records that are different in an array: * - removed -> all records that are in $this but not in $_recordSet * - added -> all records that are in $_recordSet but not in $this * - modified -> array of diffs for all different records that are in both record sets * * @param Tinebase_Record_RecordSet $_recordSet * @return array */ public function diff($_recordSet) { if (!$_recordSet instanceof Tinebase_Record_RecordSet) { if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' ' . ' Did not get Tinebase_Record_RecordSet, skipping diff()'); } return array(); } if ($this->getRecordClassName() !== $_recordSet->getRecordClassName()) { throw new Tinebase_Exception_InvalidArgument('can only compare recordsets with the same type of records'); } $removed = new Tinebase_Record_RecordSet($this->getRecordClassName()); $added = new Tinebase_Record_RecordSet($this->getRecordClassName()); $modified = array(); $result = array(); $migration = $this->getMigration($_recordSet->getArrayOfIds()); foreach ($migration['toDeleteIds'] as $id) { $added->addRecord($this->getById($id)); } foreach ($migration['toCreateIds'] as $id) { $removed->addRecord($_recordSet->getById($id)); } foreach ($migration['toUpdateIds'] as $id) { $diff = $this->getById($id)->diff($_recordSet->getById($id)); if (!empty($diff)) { $modified[$id] = $diff; } } foreach (array('removed', 'added', 'modified') as $subresult) { if (count(${$subresult}) > 0) { $result[$subresult] = ${$subresult}; } } return $result; }