/** * Load all explicit settings that apply to this domain or contact. * * @return $this */ public function loadValues() { // Note: Don't use DAO child classes. They require fields() which require // translations -- which are keyed off settings! $this->values = array(); $this->combined = NULL; // Ordinarily, we just load values from `civicrm_setting`. But upgrades require care. // In v4.0 and earlier, all values were stored in `civicrm_domain.config_backend`. // In v4.1-v4.6, values were split between `civicrm_domain` and `civicrm_setting`. // In v4.7+, all values are stored in `civicrm_setting`. // Whenever a value is available in civicrm_setting, it will take precedence. $isUpgradeMode = \CRM_Core_Config::isUpgradeMode(); if ($isUpgradeMode && empty($this->contactId) && \CRM_Core_DAO::checkFieldExists('civicrm_domain', 'config_backend', FALSE)) { $config_backend = \CRM_Core_DAO::singleValueQuery('SELECT config_backend FROM civicrm_domain WHERE id = %1', array(1 => array($this->domainId, 'Positive'))); $oldSettings = \CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($this->domainId, $config_backend); \CRM_Utils_Array::extend($this->values, $oldSettings); } // Normal case. Aside: Short-circuit prevents unnecessary query. if (!$isUpgradeMode || \CRM_Core_DAO::checkTableExists('civicrm_setting')) { $dao = \CRM_Core_DAO::executeQuery($this->createQuery()->toSQL()); while ($dao->fetch()) { $this->values[$dao->name] = $dao->value !== NULL ? unserialize($dao->value) : NULL; } } return $this; }
/** * Determine the default settings. * * @param string $entity * Ex: 'domain' or 'contact'. * @return array * Array(string $settingName => mixed $value). */ protected function getDefaults($entity) { if (!$this->useDefaults) { return self::getSystemDefaults($entity); } $cacheKey = 'defaults:' . $entity; $defaults = $this->cache->get($cacheKey); if (!is_array($defaults)) { $specs = SettingsMetadata::getMetadata(array('is_contact' => $entity === 'contact' ? 1 : 0)); $defaults = array(); foreach ($specs as $key => $spec) { $defaults[$key] = \CRM_Utils_Array::value('default', $spec); } \CRM_Utils_Array::extend($defaults, self::getSystemDefaults($entity)); $this->cache->set($cacheKey, $defaults); } return $defaults; }
/** * Update the value of a token. * * @param string|array $a * @param string|array $b * @param mixed $c * @return $this */ public function tokens($a = NULL, $b = NULL, $c = NULL) { if (is_array($a)) { \CRM_Utils_Array::extend($this->tokens, $a); } elseif (is_array($b)) { \CRM_Utils_Array::extend($this->tokens[$a], $b); } elseif (is_array($c)) { \CRM_Utils_Array::extend($this->tokens[$a][$b], $c); } elseif ($c === NULL) { $this->tokens[$a] = $b; } else { $this->tokens[$a][$b] = $c; } return $this; }
/** * Global form rule. * * @param array $fields * The input form values. * @param array $files * @param CRM_Admin_Form_ScheduleReminders $self * * @return array|bool * True if no errors, else array of errors */ public static function formRule($fields, $files, $self) { $errors = array(); if (array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0 || array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0) { $errors['entity'] = ts('Please select appropriate value'); } if (!empty($fields['is_active']) && CRM_Utils_System::isNull($fields['subject'])) { $errors['subject'] = ts('Subject is a required field.'); } if (!empty($fields['is_active']) && CRM_Utils_System::isNull(trim(strip_tags($fields['html_message'])))) { $errors['html_message'] = ts('The HTML message is a required field.'); } if (empty($self->_context) && CRM_Utils_System::isNull(CRM_Utils_Array::value(1, $fields['entity']))) { $errors['entity'] = ts('Please select entity value'); } if (!CRM_Utils_System::isNull($fields['absolute_date'])) { if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date::format(date('Ymd'))) { $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.'); } } $recipientKind = array('participant_role' => array('name' => 'participant role', 'target_id' => 'recipient_listing'), 'manual' => array('name' => 'recipient', 'target_id' => 'recipient_manual_id')); if ($fields['limit_to'] != '' && array_key_exists($fields['recipient'], $recipientKind) && empty($fields[$recipientKind[$fields['recipient']]['target_id']])) { $errors[$recipientKind[$fields['recipient']]['target_id']] = ts('If "Also include" or "Limit to" are selected, you must specify at least one %1', array(1 => $recipientKind[$fields['recipient']]['name'])); } $actionSchedule = $self->parseActionSchedule($fields); if ($actionSchedule->mapping_id) { $mapping = CRM_Core_BAO_ActionSchedule::getMapping($actionSchedule->mapping_id); CRM_Utils_Array::extend($errors, $mapping->validateSchedule($actionSchedule)); } if (!empty($errors)) { return $errors; } return empty($errors) ? TRUE : $errors; }
/** * @param int $mappingID * @param $now * * @throws CRM_Core_Exception */ public static function sendMailings($mappingID, $now) { $mapping = CRM_Utils_Array::first(self::getMappings(array('id' => $mappingID))); $actionSchedule = new CRM_Core_DAO_ActionSchedule(); $actionSchedule->mapping_id = $mappingID; $actionSchedule->is_active = 1; $actionSchedule->find(FALSE); while ($actionSchedule->fetch()) { $query = CRM_Core_BAO_ActionSchedule::prepareMailingQuery($mapping, $actionSchedule); $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($actionSchedule->id, 'Integer'))); $multilingual = CRM_Core_I18n::isMultilingual(); while ($dao->fetch()) { // switch language if necessary if ($multilingual) { $preferred_language = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $dao->contactID, 'preferred_language'); CRM_Core_BAO_ActionSchedule::setCommunicationLanguage($actionSchedule->communication_language, $preferred_language); } $errors = array(); try { $tokenProcessor = self::createTokenProcessor($actionSchedule, $mapping); $tokenProcessor->addRow()->context('contactId', $dao->contactID)->context('actionSearchResult', (object) $dao->toArray()); foreach ($tokenProcessor->evaluate()->getRows() as $tokenRow) { if ($actionSchedule->mode == 'SMS' or $actionSchedule->mode == 'User_Preference') { CRM_Utils_Array::extend($errors, self::sendReminderSms($tokenRow, $actionSchedule, $dao->contactID)); } if ($actionSchedule->mode == 'Email' or $actionSchedule->mode == 'User_Preference') { CRM_Utils_Array::extend($errors, self::sendReminderEmail($tokenRow, $actionSchedule, $dao->contactID)); } } } catch (\Civi\Token\TokenException $e) { $errors['token_exception'] = $e->getMessage(); } // update action log record $logParams = array('id' => $dao->reminderID, 'is_error' => !empty($errors), 'message' => empty($errors) ? "null" : implode(' ', $errors), 'action_date_time' => $now); CRM_Core_BAO_ActionLog::create($logParams); // insert activity log record if needed if ($actionSchedule->record_activity && empty($errors)) { $caseID = empty($dao->case_id) ? NULL : $dao->case_id; CRM_Core_BAO_ActionSchedule::createMailingActivity($actionSchedule, $mapping, $dao->contactID, $dao->entityID, $caseID); } } $dao->free(); } }