/**
  * clears a single state entry
  * 
  * @param string $_name
  * @return void
  */
 public function clearState($_name)
 {
     if (!Tinebase_Core::getUser()->hasRight('Tinebase', Tinebase_Acl_Rights::MANAGE_OWN_STATE)) {
         throw new Tinebase_Exception_AccessDenied("You don't have the right to manage your client state");
     }
     $recordToDelete = $this->_backend->search($this->_getFilter($_name))->getFirstRecord();
     if ($recordToDelete) {
         $this->_backend->delete($recordToDelete->getId());
     }
 }
 protected function _updateLeadConfig()
 {
     // get all configs for crm from DB
     $crmApp = Tinebase_Application::getInstance()->getApplicationByName('Crm');
     // either put default to DB or delete form DB
     $cb = new Tinebase_Backend_Sql(array('modelName' => 'Tinebase_Model_Config', 'tableName' => 'config'));
     $configRecords = $cb->search(new Tinebase_Model_ConfigFilter(array(array('field' => 'application_id', 'operator' => 'equals', 'value' => $crmApp->getId()))));
     $appDefaults = $configRecords->filter('name', 'appdefaults')->getFirstRecord();
     foreach (array('leadstate', 'leadtype', 'leadsource') as $oldValueName) {
         $keyFieldName = $oldValueName . 's';
         $DBconfig = $configRecords->filter('name', $keyFieldName)->getFirstRecord();
         // only update if custom config is found and if it is still in old format
         if ($DBconfig && strpos($DBconfig->value, $oldValueName) !== false) {
             $decodedConfig = json_decode($DBconfig->value, true);
             foreach ($decodedConfig as $key => $oldRecord) {
                 $decodedConfig[$key]['value'] = $oldRecord[$oldValueName];
                 unset($decodedConfig[$key][$oldValueName]);
             }
             // if no app defaults: use the first record as default
             $default = isset($appDefaults[$keyFieldName]) ? $appDefaults[$keyFieldName] : $decodedConfig[0]['id'];
             $DBconfig->value = json_encode(array('records' => $decodedConfig, 'default' => $default));
             $cb->update($DBconfig);
         }
     }
     if ($appDefaults) {
         $cb->delete($appDefaults->getId());
     }
 }
Example #3
0
 /**
  * set alarms of record
  *
  * @param Tinebase_Record_Abstract $_record
  * @param string $_alarmsProperty
  * @return void
  */
 public function setAlarmsOfRecord(Tinebase_Record_Abstract $_record, $_alarmsProperty = 'alarms')
 {
     $model = get_class($_record);
     $alarms = $_record->{$_alarmsProperty};
     $currentAlarms = $this->getAlarmsOfRecord($model, $_record);
     $diff = $currentAlarms->getMigration($alarms->getArrayOfIds());
     $this->_backend->delete($diff['toDeleteIds']);
     if (count($alarms) > 1) {
         Tinebase_Core::getLogger()->NOTICE(__METHOD__ . '::' . __LINE__ . "only the first alarm could is saved: " . print_r($alarms->toArray(), TRUE));
     }
     // create / update alarms
     foreach ($alarms as $alarm) {
         $id = $alarm->getId();
         if ($id) {
             $alarm = $this->_backend->update($alarm);
         } else {
             $alarm->record_id = $_record->getId();
             if (!$alarm->model) {
                 $alarm->model = $model;
             }
             $alarm = $this->_backend->create($alarm);
         }
         break;
     }
 }
 /**
  * only keep the last 60 jobs and purge all other
  * 
  * @param Tinebase_Model_AsyncJob $job
  */
 protected function _purgeOldJobs(Tinebase_Model_AsyncJob $job)
 {
     $deleteBefore = $job->seq - 60;
     if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
         Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Purging old Jobs before sequence: ' . $deleteBefore);
     }
     // avoid overloading by deleting old jobs in batches only
     $idsToDelete = $this->_backend->search(new Tinebase_Model_AsyncJobFilter(array(array('field' => 'seq', 'operator' => 'less', 'value' => $deleteBefore))), new Tinebase_Model_Pagination(array('limit' => 10000)), true);
     $this->_backend->delete($idsToDelete);
 }
Example #5
0
 /**
  * save custom fields of record in its custom fields table
  *
  * @param Tinebase_Record_Interface $_record
  */
 public function saveRecordCustomFields(Tinebase_Record_Interface $_record)
 {
     $applicationId = Tinebase_Application::getInstance()->getApplicationByName($_record->getApplication())->getId();
     $appCustomFields = $this->getCustomFieldsForApplication($applicationId, get_class($_record), Tinebase_Model_CustomField_Grant::GRANT_WRITE);
     $this->resolveConfigGrants($appCustomFields);
     $existingCustomFields = $this->_getCustomFields($_record->getId());
     $existingCustomFields->addIndices(array('customfield_id'));
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Updating custom fields for record of class ' . get_class($_record));
     }
     //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . print_r($_record->customfields, TRUE));
     //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . print_r($appCustomFields->toArray(), TRUE));
     foreach ($appCustomFields as $customField) {
         if (is_array($_record->customfields) && array_key_exists($customField->name, $_record->customfields)) {
             $value = $_record->customfields[$customField->name];
             $filtered = $existingCustomFields->filter('customfield_id', $customField->id);
             switch (count($filtered)) {
                 case 1:
                     $cf = $filtered->getFirstRecord();
                     if ($customField->valueIsEmpty($value)) {
                         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Deleting cf value for ' . $customField->name);
                         }
                         $this->_backendValue->delete($cf);
                     } else {
                         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Updateing value for ' . $customField->name . ' to ' . $value);
                         }
                         $cf->value = $value;
                         $this->_backendValue->update($cf);
                     }
                     break;
                 case 0:
                     if (!$customField->valueIsEmpty($value)) {
                         $cf = new Tinebase_Model_CustomField_Value(array('record_id' => $_record->getId(), 'customfield_id' => $customField->getId(), 'value' => $value));
                         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Creating value for ' . $customField->name . ' -> ' . $value);
                         }
                         $this->_backendValue->create($cf);
                     }
                     break;
                 default:
                     throw new Tinebase_Exception_UnexpectedValue('Oops, there should be only one custom field value here!');
             }
         }
     }
 }
 /**
  * set alarms of record
  *
  * @param Tinebase_Record_Abstract $_record
  * @param string $_alarmsProperty
  * @return void
  */
 public function setAlarmsOfRecord(Tinebase_Record_Abstract $_record, $_alarmsProperty = 'alarms')
 {
     $model = get_class($_record);
     $alarms = $_record->{$_alarmsProperty};
     $currentAlarms = $this->getAlarmsOfRecord($model, $_record);
     $diff = $currentAlarms->getMigration($alarms->getArrayOfIds());
     $this->_backend->delete($diff['toDeleteIds']);
     // create / update alarms
     foreach ($alarms as $alarm) {
         $id = $alarm->getId();
         if ($id) {
             $alarm = $this->_backend->update($alarm);
         } else {
             $alarm->record_id = $_record->getId();
             if (!$alarm->model) {
                 $alarm->model = $model;
             }
             $alarm = $this->_backend->create($alarm);
         }
     }
 }
 /**
  * Deletes a set of records.
  *
  * If one of the records could not be deleted, no record is deleted
  *
  * @param   array array of record identifiers
  * @return  Tinebase_Record_RecordSet
  */
 public function delete($_ids)
 {
     $this->_configBackend->delete($_ids);
 }
 /**
  * update to 8.15
  *
  * move keyFieldConfig defaults to config files
  */
 public function update_14()
 {
     // either put default to DB or delete form DB
     $cb = new Tinebase_Backend_Sql(array('modelName' => 'Tinebase_Model_Config', 'tableName' => 'config'));
     $configRecords = $cb->getAll();
     // iterate installed apps
     foreach (Tinebase_Application::getInstance()->getApplications() as $application) {
         try {
             // get config
             $config = Tinebase_Config::getAppConfig($application->name);
             if (!method_exists($config, 'getProperties')) {
                 continue;
             }
             foreach ($config->getProperties() as $name => $property) {
                 if ($property['type'] == Tinebase_Config_Abstract::TYPE_KEYFIELD_CONFIG) {
                     $dbConfig = $config->get($name);
                     $dbConfigRecords = $dbConfig['records']->toArray();
                     $defaultRecords = isset($property['default']['records']) ? $property['default']['records'] : null;
                     if ($defaultRecords && json_encode($dbConfigRecords) != json_encode($defaultRecords)) {
                         // set default value
                         if (array_key_exists('default', $property['default'])) {
                             $dbConfig->default = $property['default']['default'];
                             $config->set($name, $dbConfig->toArray());
                         }
                     } else {
                         // delete default config
                         $configRecord = $configRecords->filter('application_id', $application->getId())->filter('name', $name)->getFirstRecord();
                         if ($configRecord) {
                             $cb->delete($configRecord->getId());
                         }
                     }
                 }
             }
         } catch (Exception $e) {
             Setup_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ' could not migrate keyFieldConfig ' . $e);
         }
     }
     $this->setApplicationVersion('Tinebase', '8.15');
 }
 /**
  * clean timemachine_modlog for records that have been pruned (not deleted!)
  */
 public function clean()
 {
     $filter = new Tinebase_Model_Filter_FilterGroup();
     $pagination = new Tinebase_Model_Pagination();
     $pagination->limit = 10000;
     $pagination->sort = 'id';
     $totalCount = 0;
     while (($recordSet = $this->_backend->search($filter, $pagination)) && $recordSet->count() > 0) {
         $filter = new Tinebase_Model_Filter_FilterGroup();
         $pagination->start += $pagination->limit;
         $models = array();
         foreach ($recordSet as $modlog) {
             $models[$modlog->record_type][$modlog->record_id][] = $modlog->id;
         }
         foreach ($models as $model => &$ids) {
             $appNotFound = false;
             try {
                 $app = Tinebase_Core::getApplicationInstance($model, '', true);
             } catch (Tinebase_Exception_NotFound $tenf) {
                 $appNotFound = true;
             }
             if (!$appNotFound) {
                 if ($app instanceof Tinebase_Container) {
                     $backend = $app;
                 } else {
                     if (!$app instanceof Tinebase_Controller_Record_Abstract) {
                         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                             Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' model: ' . $model . ' controller: ' . get_class($app) . ' not an instance of Tinebase_Controller_Record_Abstract');
                         }
                         continue;
                     }
                     $backend = $app->getBackend();
                 }
                 if (!$backend instanceof Tinebase_Backend_Interface) {
                     if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                         Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' model: ' . $model . ' backend: ' . get_class($backend) . ' not an instance of Tinebase_Backend_Interface');
                     }
                     continue;
                 }
                 $record = new $model(null, true);
                 $modelFilter = $model . 'Filter';
                 $idFilter = new $modelFilter(array(), '', array('ignoreAcl' => true));
                 $idFilter->addFilter(new Tinebase_Model_Filter_Id(array('field' => $record->getIdProperty(), 'operator' => 'in', 'value' => array_keys($ids))));
                 $existingIds = $backend->search($idFilter, null, true);
                 if (!is_array($existingIds)) {
                     throw new Exception('search for model: ' . $model . ' returned not an array!');
                 }
                 foreach ($existingIds as $id) {
                     unset($ids[$id]);
                 }
             }
             if (count($ids) > 0) {
                 $toDelete = array();
                 foreach ($ids as $idArrays) {
                     foreach ($idArrays as $id) {
                         $toDelete[$id] = true;
                     }
                 }
                 $toDelete = array_keys($toDelete);
                 $this->_backend->delete($toDelete);
                 $totalCount += count($toDelete);
             }
         }
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
         Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' deleted ' . $totalCount . ' modlogs records');
     }
     return $totalCount;
 }
 /**
  * update to 7.4
  * 
  * - save each state_id in an own field
  */
 public function update_3()
 {
     // add a default value of "false", as PGSQL does allow notnull columns with default value null
     $declaration = new Setup_Backend_Schema_Field_Xml('
         <field>
             <name>state_id</name>
             <type>text</type>
             <length>128</length>
             <notnull>true</notnull>
             <default>false</default>
         </field>
     ');
     $this->_backend->addCol('state', $declaration);
     $this->_backend->dropIndex('state', 'user_id');
     $declaration = new Setup_Backend_Schema_Index_Xml('
         <index>
             <name>user_id--state_id</name>
             <unique>true</unique>
             <field>
                 <name>user_id</name>
             </field>
             <field>
                 <name>state_id</name>
             </field>
         </index>
     ');
     $this->_backend->addIndex('state', $declaration);
     $be = new Tinebase_Backend_Sql(array('modelName' => 'Tinebase_Model_State', 'tableName' => 'state'));
     $allStates = $be->getAll();
     foreach ($allStates as $oldState) {
         $oldData = Zend_Json::decode($oldState->data);
         foreach ($oldData as $stateId => $data) {
             $filter = new Tinebase_Model_StateFilter(array(array('field' => 'state_id', 'operator' => 'equals', 'value' => $stateId), array('field' => 'user_id', 'operator' => 'equals', 'value' => $oldState->user_id)));
             $result = $be->search($filter);
             try {
                 if ($result->count()) {
                     $record = $result->getFirstRecord();
                     $record->data = $data;
                     $be->update($record);
                 } else {
                     $record = new Tinebase_Model_State(array('user_id' => $oldState->user_id, 'state_id' => $stateId, 'data' => $data));
                     $be->create($record);
                 }
             } catch (Exception $e) {
                 if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . 'Could not transfer old state: ' . $stateId . ': ' . print_r($data, 1));
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . 'Exception Message: ' . $e->getMessage());
                 }
             }
         }
         $be->delete($oldState->getId());
     }
     // remove the default value "false" again
     $declaration = new Setup_Backend_Schema_Field_Xml('
         <field>
             <name>state_id</name>
             <type>text</type>
             <length>128</length>
             <notnull>true</notnull>
         </field>
     ');
     $this->_backend->alterCol('state', $declaration);
     $this->setApplicationVersion('Tinebase', '7.4');
     $this->setTableVersion('state', 2);
 }
 /**
  * save custom fields of record in its custom fields table
  *
  * @param Tinebase_Record_Interface $_record
  */
 public function saveRecordCustomFields(Tinebase_Record_Interface $_record)
 {
     $applicationId = Tinebase_Application::getInstance()->getApplicationByName($_record->getApplication())->getId();
     $appCustomFields = $this->getCustomFieldsForApplication($applicationId, get_class($_record), Tinebase_Model_CustomField_Grant::GRANT_WRITE);
     $this->resolveConfigGrants($appCustomFields);
     $existingCustomFields = $this->_getCustomFields($_record->getId());
     $existingCustomFields->addIndices(array('customfield_id'));
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Updating custom fields for record of class ' . get_class($_record));
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Record cf values: ' . print_r($_record->customfields, TRUE));
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' App cf names: ' . print_r($appCustomFields->name, TRUE));
     }
     foreach ($appCustomFields as $customField) {
         if (is_array($_record->customfields) && (isset($_record->customfields[$customField->name]) || array_key_exists($customField->name, $_record->customfields))) {
             $value = $_record->customfields[$customField->name];
             $filtered = $existingCustomFields->filter('customfield_id', $customField->id);
             // we need to resolve the modelName and the record value if array is given (e.g. on updating customfield)
             if (strtolower($customField->definition['type']) == 'record') {
                 $modelParts = explode('.', $customField->definition['recordConfig']['value']['records']);
                 // get model parts from saved record class e.g. Tine.Admin.Model.Group
                 $modelName = $modelParts[1] . '_Model_' . $modelParts[3];
                 if (is_array($value)) {
                     $model = new $modelName(array(), TRUE);
                     $value = $value[$model->getIdProperty()];
                 }
                 // check if customfield value is the record itself
                 if (get_class($_record) == $modelName && $_record->getId() == $value) {
                     throw new Tinebase_Exception_Record_Validation('It is not allowed to add the same record as customfield record!');
                 }
             }
             switch (count($filtered)) {
                 case 1:
                     $cf = $filtered->getFirstRecord();
                     if ($customField->valueIsEmpty($value)) {
                         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Deleting cf value for ' . $customField->name);
                         }
                         $this->_backendValue->delete($cf);
                     } else {
                         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Updating value for ' . $customField->name . ' to ' . $value);
                         }
                         $cf->value = $value;
                         $this->_backendValue->update($cf);
                     }
                     break;
                 case 0:
                     if (!$customField->valueIsEmpty($value)) {
                         $cf = new Tinebase_Model_CustomField_Value(array('record_id' => $_record->getId(), 'customfield_id' => $customField->getId(), 'value' => $value));
                         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Creating value for ' . $customField->name . ' -> ' . $value);
                         }
                         $this->_backendValue->create($cf);
                     }
                     break;
                 default:
                     throw new Tinebase_Exception_UnexpectedValue('Oops, there should be only one custom field value here!');
             }
         }
     }
 }
 /**
  * update payment methods
  */
 public function update_31()
 {
     $cb = new Tinebase_Backend_Sql(array('modelName' => 'Tinebase_Model_Config', 'tableName' => 'config'));
     $appId = Tinebase_Application::getInstance()->getApplicationByName('Sales')->getId();
     $idToDelete = $cb->search(new Tinebase_Model_ConfigFilter(array(array('field' => 'application_id', 'operator' => 'equals', 'value' => $appId), array('field' => 'name', 'operator' => 'equals', 'value' => Sales_Config::PAYMENT_METHODS)), 'AND'), null, true);
     if (isset($idToDelete[0])) {
         $cb->delete($idToDelete[0]);
     }
     // create payment types config
     $tc = array('name' => Sales_Config::PAYMENT_METHODS, 'records' => array(array('id' => 'BANK TRANSFER', 'value' => 'Bank transfer', 'system' => true), array('id' => 'DIRECT DEBIT', 'value' => 'Direct debit', 'system' => true), array('id' => 'CANCELLATION', 'value' => 'Cancellation', 'system' => true), array('id' => 'CREDIT', 'value' => 'Credit', 'system' => true), array('id' => 'CREDIT CARD', 'value' => 'Credit card', 'system' => true), array('id' => 'PAYPAL', 'value' => 'Paypal', 'system' => true)));
     $cb->create(new Tinebase_Model_Config(array('application_id' => $appId, 'name' => Sales_Config::PAYMENT_METHODS, 'value' => json_encode($tc))));
     $this->setApplicationVersion('Sales', '8.32');
 }