/**
  * @param array  $params         (reference ) an assoc array of name/value pairs
  *
  * @return object       CRM_Core_BAO_SEPAMandate object on success, null otherwise
  * @access public
  * @static (I do apologize, I don't want to)
  */
 static function add(&$params)
 {
     // handle 'normal' creation process inlcuding hooks
     $hook = empty($params['id']) ? 'create' : 'edit';
     CRM_Utils_Hook::pre($hook, 'SepaMandate', CRM_Utils_Array::value('id', $params), $params);
     // set default date to today
     if (!array_key_exists("date", $params)) {
         $params["date"] = date("YmdHis");
     }
     if (empty($params['id'])) {
         CRM_Utils_SepaCustomisationHooks::create_mandate($params);
         if (empty($params['reference'])) {
             // If no mandate reference was supplied by the caller nor the customisation hook, create a nice default one.
             $creditor = civicrm_api3('SepaCreditor', 'getsingle', array('id' => $params['creditor_id'], 'return' => 'mandate_prefix'));
             $dao = new CRM_Core_DAO();
             $database = $dao->database();
             $next_id = CRM_Core_DAO::singleValueQuery("SELECT auto_increment FROM information_schema.tables WHERE table_schema='{$database}' and table_name='civicrm_sdd_mandate';");
             $params['reference'] = $creditor['mandate_prefix'] . '-' . $params['creditor_id'] . '-' . $params['type'] . '-' . date("Y") . '-' . $next_id;
         }
     }
     // validate IBAN / BIC
     if (!empty($params['iban'])) {
         $params['iban'] = strtoupper($params['iban']);
         // create uppercase string
         $params['iban'] = str_replace(' ', '', $params['iban']);
         // strip spaces
         $iban_error = CRM_Sepa_Logic_Verification::verifyIBAN($params['iban']);
         if ($iban_error) {
             throw new CRM_Exception($iban_error . ':' . $params['iban']);
         }
     }
     if (!empty($params['bic'])) {
         $bic_error = CRM_Sepa_Logic_Verification::verifyBIC($params['bic']);
         if ($bic_error) {
             throw new CRM_Exception($bic_error . ':' . $params['bic']);
         }
     }
     // create the DAO object
     $dao = new CRM_Sepa_DAO_SEPAMandate();
     $dao->copyValues($params);
     if (self::is_active(CRM_Utils_Array::value('status', $params))) {
         if ($dao->validation_date == NULL) {
             $dao->validation_date = date("YmdHis");
         }
     }
     $dao->save();
     CRM_Utils_Hook::post($hook, 'SepaMandate', $dao->id, $dao);
     return $dao;
 }
Exemple #2
0
 /**
  * Clear leftover temporary tables.
  *
  * This is called on upgrade, during tests and site move, from the cron and via clear caches in the UI.
  *
  * Currently the UI clear caches does not pass a time interval - which may need review as it does risk
  * ripping the tables out from underneath a current action. This was considered but
  * out-of-scope for CRM-16167
  *
  * @param string|bool $timeInterval
  *   Optional time interval for mysql date function.g '2 day'. This can be used to prevent
  *   tables created recently from being deleted.
  */
 public static function clearTempTables($timeInterval = FALSE)
 {
     $dao = new CRM_Core_DAO();
     $query = "\n      SELECT TABLE_NAME as tableName\n      FROM   INFORMATION_SCHEMA.TABLES\n      WHERE  TABLE_SCHEMA = %1\n      AND (\n        TABLE_NAME LIKE 'civicrm_import_job_%'\n        OR TABLE_NAME LIKE 'civicrm_export_temp%'\n        OR TABLE_NAME LIKE 'civicrm_task_action_temp%'\n        OR TABLE_NAME LIKE 'civicrm_report_temp%'\n        )\n    ";
     if ($timeInterval) {
         $query .= " AND CREATE_TIME < DATE_SUB(NOW(), INTERVAL {$timeInterval})";
     }
     $tableDAO = CRM_Core_DAO::executeQuery($query, array(1 => array($dao->database(), 'String')));
     $tables = array();
     while ($tableDAO->fetch()) {
         $tables[] = $tableDAO->tableName;
     }
     if (!empty($tables)) {
         $table = implode(',', $tables);
         // drop leftover temporary tables
         CRM_Core_DAO::executeQuery("DROP TABLE {$table}");
     }
 }
Exemple #3
0
 public static function getIncompleteImportTables()
 {
     $dao = new CRM_Core_DAO();
     $database = $dao->database();
     $query = "SELECT   TABLE_NAME FROM INFORMATION_SCHEMA\n                  WHERE    TABLE_SCHEMA = ? AND\n                           TABLE_NAME LIKE 'civicrm_import_job_%'\n                  ORDER BY TABLE_NAME";
     $result = CRM_Core_DAO::executeQuery($query, array($database));
     $incompleteImportTables = array();
     while ($importTable = $result->fetch()) {
         if (!$this->isComplete($importTable)) {
             $incompleteImportTables[] = $importTable;
         }
     }
     return $incompleteImportTables;
 }
 /**
  * Process the form submission.
  *
  *
  * @return void
  */
 public function postProcess()
 {
     $values = $this->exportValues();
     //FIXME: Handle logic to replace is_default column by usage
     // reset used column to General (since there can only
     // be one 'Supervised' or 'Unsupervised' rule)
     if ($values['used'] != 'General') {
         $query = "\nUPDATE civicrm_dedupe_rule_group\n   SET used = 'General'\n WHERE contact_type = %1\n   AND used = %2";
         $queryParams = array(1 => array($this->_contactType, 'String'), 2 => array($values['used'], 'String'));
         CRM_Core_DAO::executeQuery($query, $queryParams);
     }
     $rgDao = new CRM_Dedupe_DAO_RuleGroup();
     if ($this->_action & CRM_Core_Action::UPDATE) {
         $rgDao->id = $this->_rgid;
     }
     $rgDao->title = $values['title'];
     $rgDao->is_reserved = CRM_Utils_Array::value('is_reserved', $values, FALSE);
     $rgDao->used = $values['used'];
     $rgDao->contact_type = $this->_contactType;
     $rgDao->threshold = $values['threshold'];
     $rgDao->save();
     // make sure name is set only during insert
     if ($this->_action & CRM_Core_Action::ADD) {
         // generate name based on title
         $rgDao->name = CRM_Utils_String::titleToVar($values['title']) . "_{$rgDao->id}";
         $rgDao->save();
     }
     // lets skip updating of fields for reserved dedupe group
     if ($rgDao->is_reserved) {
         CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title)), ts('Saved'), 'success');
         return;
     }
     $ruleDao = new CRM_Dedupe_DAO_Rule();
     $ruleDao->dedupe_rule_group_id = $rgDao->id;
     $ruleDao->delete();
     $ruleDao->free();
     $substrLenghts = array();
     $tables = array();
     $daoObj = new CRM_Core_DAO();
     $database = $daoObj->database();
     for ($count = 0; $count < self::RULES_COUNT; $count++) {
         if (empty($values["where_{$count}"])) {
             continue;
         }
         list($table, $field) = explode('.', CRM_Utils_Array::value("where_{$count}", $values));
         $length = !empty($values["length_{$count}"]) ? CRM_Utils_Array::value("length_{$count}", $values) : NULL;
         $weight = $values["weight_{$count}"];
         if ($table and $field) {
             $ruleDao = new CRM_Dedupe_DAO_Rule();
             $ruleDao->dedupe_rule_group_id = $rgDao->id;
             $ruleDao->rule_table = $table;
             $ruleDao->rule_field = $field;
             $ruleDao->rule_length = $length;
             $ruleDao->rule_weight = $weight;
             $ruleDao->save();
             $ruleDao->free();
             if (!array_key_exists($table, $tables)) {
                 $tables[$table] = array();
             }
             $tables[$table][] = $field;
         }
         // CRM-6245: we must pass table/field/length triples to the createIndexes() call below
         if ($length) {
             if (!isset($substrLenghts[$table])) {
                 $substrLenghts[$table] = array();
             }
             //CRM-13417 to avoid fatal error "Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys, 1089"
             $schemaQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS\n          WHERE TABLE_SCHEMA = '{$database}' AND\n          TABLE_NAME = '{$table}' AND COLUMN_NAME = '{$field}';";
             $dao = CRM_Core_DAO::executeQuery($schemaQuery);
             if ($dao->fetch()) {
                 // set the length to null for all the fields where prefix length is not supported. eg. int,tinyint,date,enum etc dataTypes.
                 if ($dao->COLUMN_NAME == $field && !in_array($dao->DATA_TYPE, array('char', 'varchar', 'binary', 'varbinary', 'text', 'blob'))) {
                     $length = NULL;
                 } elseif ($dao->COLUMN_NAME == $field && !empty($dao->CHARACTER_MAXIMUM_LENGTH) && $length > $dao->CHARACTER_MAXIMUM_LENGTH) {
                     //set the length to CHARACTER_MAXIMUM_LENGTH in case the length provided by the user is greater than the limit
                     $length = $dao->CHARACTER_MAXIMUM_LENGTH;
                 }
             }
             $substrLenghts[$table][$field] = $length;
         }
     }
     // also create an index for this dedupe rule
     // CRM-3837
     CRM_Utils_Hook::dupeQuery($ruleDao, 'dedupeIndexes', $tables);
     CRM_Core_BAO_SchemaHandler::createIndexes($tables, 'dedupe_index', $substrLenghts);
     //need to clear cache of deduped contacts
     //based on the previous rule
     $cacheKey = "merge {$this->_contactType}_{$this->_rgid}_%";
     CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);
     CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title)), ts('Saved'), 'success');
 }
Exemple #5
0
 /**
  * Do periodic cleanup of the CiviCRM session table. Also delete all session cache entries
  * which are a couple of days old. This keeps the session cache to a manageable size
  *
  * @return void
  * @static
  * @access private
  */
 static function cleanup($session = false, $table = false, $prevNext = false)
 {
     // clean up the session cache every $cacheCleanUpNumber probabilistically
     $cleanUpNumber = 757;
     // clean up all sessions older than $cacheTimeIntervalDays days
     $timeIntervalDays = 2;
     $timeIntervalMins = 30;
     if (mt_rand(1, 100000) % $cleanUpNumber == 0) {
         $session = $table = $prevNext = true;
     }
     if (!$session && !$table && !$prevNext) {
         return;
     }
     if ($prevNext) {
         // delete all PrevNext caches
         CRM_Core_BAO_PrevNextCache::cleanupCache();
     }
     if ($table) {
         // also delete all the action temp tables
         // that were created the same interval ago
         $dao = new CRM_Core_DAO();
         $query = "\nSELECT TABLE_NAME as tableName\nFROM   INFORMATION_SCHEMA.TABLES\nWHERE  TABLE_SCHEMA = %1\nAND    ( TABLE_NAME LIKE 'civicrm_task_action_temp_%'\n OR      TABLE_NAME LIKE 'civicrm_export_temp_%'\n OR      TABLE_NAME LIKE 'civicrm_import_job_%' )\nAND    CREATE_TIME < date_sub( NOW( ), INTERVAL {$timeIntervalDays} day )\n";
         $params = array(1 => array($dao->database(), 'String'));
         $tableDAO = CRM_Core_DAO::executeQuery($query, $params);
         $tables = array();
         while ($tableDAO->fetch()) {
             $tables[] = $tableDAO->tableName;
         }
         if (!empty($tables)) {
             $table = implode(',', $tables);
             // drop leftover temporary tables
             CRM_Core_DAO::executeQuery("DROP TABLE {$table}");
         }
     }
     if ($session) {
         // first delete all sessions which are related to any potential transaction
         // page
         $transactionPages = array('CRM_Contribute_Controller_Contribution', 'CRM_Event_Controller_Registration');
         $params = array(1 => array(date('Y-m-d H:i:s', time() - $timeIntervalMins * 60), 'String'));
         foreach ($transactionPages as $trPage) {
             $params[] = array("%{$trPage}%", 'String');
             $where[] = 'path LIKE %' . sizeof($params);
         }
         $sql = "\nDELETE FROM civicrm_cache\nWHERE       group_name = 'CiviCRM Session'\nAND         created_date <= %1\nAND         (" . implode(' OR ', $where) . ")";
         CRM_Core_DAO::executeQuery($sql, $params);
         $sql = "\nDELETE FROM civicrm_cache\nWHERE       group_name = 'CiviCRM Session'\nAND         created_date < date_sub( NOW( ), INTERVAL {$timeIntervalDays} DAY )\n";
         CRM_Core_DAO::executeQuery($sql);
     }
 }
Exemple #6
0
 /**
  * Get the name of the CiviCRM database.
  *
  * @return string
  */
 public static function getDatabaseName()
 {
     $daoObj = new CRM_Core_DAO();
     return $daoObj->database();
 }