From b54d78e38c2348417ff0adbbdae5c3dee692bc9d Mon Sep 17 00:00:00 2001 From: Erik Hommel <hommel@ee-atwork.nl> Date: Tue, 13 Jan 2015 16:17:31 +0100 Subject: [PATCH] added upgrader, sql files and DAO --- CRM/Civirules/DAO/Action.php | 114 ++++++++++ CRM/Civirules/DAO/Comparison.php | 92 ++++++++ CRM/Civirules/DAO/Condition.php | 109 +++++++++ CRM/Civirules/DAO/DataSelector.php | 114 ++++++++++ CRM/Civirules/DAO/Event.php | 108 +++++++++ CRM/Civirules/DAO/Rule.php | 102 +++++++++ CRM/Civirules/DAO/RuleAction.php | 117 ++++++++++ CRM/Civirules/DAO/RuleCondition.php | 106 +++++++++ CRM/Civirules/DAO/RuleEvent.php | 95 ++++++++ CRM/Civirules/Upgrader.php | 23 ++ CRM/Civirules/Upgrader/Base.php | 298 +++++++++++++++++++++++++ sql/create_civirule_action.sql | 17 ++ sql/create_civirule_comparison.sql | 9 + sql/create_civirule_condition.sql | 12 + sql/create_civirule_data_selector.sql | 12 + sql/create_civirule_event.sql | 12 + sql/create_civirule_rule.sql | 11 + sql/create_civirule_rule_action.sql | 31 +++ sql/create_civirule_rule_condition.sql | 30 +++ sql/create_civirule_rule_event.sql | 22 ++ 20 files changed, 1434 insertions(+) create mode 100644 CRM/Civirules/DAO/Action.php create mode 100644 CRM/Civirules/DAO/Comparison.php create mode 100644 CRM/Civirules/DAO/Condition.php create mode 100644 CRM/Civirules/DAO/DataSelector.php create mode 100644 CRM/Civirules/DAO/Event.php create mode 100644 CRM/Civirules/DAO/Rule.php create mode 100644 CRM/Civirules/DAO/RuleAction.php create mode 100644 CRM/Civirules/DAO/RuleCondition.php create mode 100644 CRM/Civirules/DAO/RuleEvent.php create mode 100644 CRM/Civirules/Upgrader.php create mode 100644 CRM/Civirules/Upgrader/Base.php create mode 100644 sql/create_civirule_action.sql create mode 100644 sql/create_civirule_comparison.sql create mode 100644 sql/create_civirule_condition.sql create mode 100644 sql/create_civirule_data_selector.sql create mode 100644 sql/create_civirule_event.sql create mode 100644 sql/create_civirule_rule.sql create mode 100644 sql/create_civirule_rule_action.sql create mode 100644 sql/create_civirule_rule_condition.sql create mode 100644 sql/create_civirule_rule_event.sql diff --git a/CRM/Civirules/DAO/Action.php b/CRM/Civirules/DAO/Action.php new file mode 100644 index 0000000..b6fc010 --- /dev/null +++ b/CRM/Civirules/DAO/Action.php @@ -0,0 +1,114 @@ +<?php +/** + * DAO Action for table civirule_action + * + * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ +class CRM_Civirules_DAO_Action extends CRM_Core_DAO { + /** + * static instance to hold the field values + * + * @var array + * @static + */ + static $_fields = null; + static $_export = null; + /** + * empty definition for virtual function + */ + static function getTableName() { + return 'civirule_action'; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() { + if (!(self::$_fields)) { + self::$_fields = array( + 'id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => true + ) , + 'label' => array( + 'name' => 'label', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 64, + ), + 'description' => array( + 'name' => 'description', + 'type' => CRM_Utils_Type::T_TEXT, + ), + 'entity' => array( + 'name' => 'entity', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 64, + ) , + 'column' => array( + 'name' => 'column', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 128, + ), + 'function_name' => array( + 'name' => 'function_name', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 256, + ), + 'is_active' => array( + 'name' => 'is_active', + 'type' => CRM_Utils_Type::T_INT, + ), + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the array key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'id', + 'label' => 'label', + 'description' => 'description', + 'entity' => 'entity', + 'column' => 'column', + 'function_name' => 'function_name', + 'is_active' => 'is_active' + ); + } + return self::$_fieldKeys; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['activity'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} \ No newline at end of file diff --git a/CRM/Civirules/DAO/Comparison.php b/CRM/Civirules/DAO/Comparison.php new file mode 100644 index 0000000..1bfcdc7 --- /dev/null +++ b/CRM/Civirules/DAO/Comparison.php @@ -0,0 +1,92 @@ +<?php +/** + * DAO Comparison for table civirule_comparison + * + * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ +class CRM_Civirules_DAO_Comparison extends CRM_Core_DAO { + /** + * static instance to hold the field values + * + * @var array + * @static + */ + static $_fields = null; + static $_export = null; + /** + * empty definition for virtual function + */ + static function getTableName() { + return 'civirule_comparison'; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() { + if (!(self::$_fields)) { + self::$_fields = array( + 'id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => true + ) , + 'label' => array( + 'name' => 'label', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 45, + ), + 'operator' => array( + 'name' => 'operator', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 45, + ) + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the array key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'id', + 'label' => 'label', + 'operator' => 'operator' + ); + } + return self::$_fieldKeys; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['activity'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} \ No newline at end of file diff --git a/CRM/Civirules/DAO/Condition.php b/CRM/Civirules/DAO/Condition.php new file mode 100644 index 0000000..9e23486 --- /dev/null +++ b/CRM/Civirules/DAO/Condition.php @@ -0,0 +1,109 @@ +<?php +/** + * DAO Condition for table civirule_condition + * + * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ +class CRM_Civirules_DAO_Condition extends CRM_Core_DAO { + /** + * static instance to hold the field values + * + * @var array + * @static + */ + static $_fields = null; + static $_export = null; + /** + * empty definition for virtual function + */ + static function getTableName() { + return 'civirule_condition'; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() { + if (!(self::$_fields)) { + self::$_fields = array( + 'id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => true + ) , + 'name' => array( + 'name' => 'name', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 64, + ), + 'label' => array( + 'name' => 'label', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 128, + ), + 'function_name' => array( + 'name' => 'function_name', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 256, + ), + 'civicrm_form_class' => array( + 'name' => 'civicrm_form_class', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 128, + ), + 'is_active' => array( + 'name' => 'is_active', + 'type' => CRM_Utils_Type::T_INT, + ) + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the array key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'id', + 'name' => 'name', + 'label' => 'label', + 'function_name' => 'function_name', + 'civicrm_form_class' => 'civicrm_form_class', + 'is_active' => 'is_active' + ); + } + return self::$_fieldKeys; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['activity'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} \ No newline at end of file diff --git a/CRM/Civirules/DAO/DataSelector.php b/CRM/Civirules/DAO/DataSelector.php new file mode 100644 index 0000000..cb984d2 --- /dev/null +++ b/CRM/Civirules/DAO/DataSelector.php @@ -0,0 +1,114 @@ +<?php +/** + * DAO DataSelector for table civirule_data_selector + * + * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ +class CRM_Civirules_DAO_DataSelector extends CRM_Core_DAO { + /** + * static instance to hold the field values + * + * @var array + * @static + */ + static $_fields = null; + static $_export = null; + /** + * empty definition for virtual function + */ + static function getTableName() { + return 'civirule_data_selector'; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() { + if (!(self::$_fields)) { + self::$_fields = array( + 'id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => true + ) , + 'label' => array( + 'name' => 'label', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 64, + ), + 'description' => array( + 'name' => 'description', + 'type' => CRM_Utils_Type::T_TEXT, + ), + 'entity' => array( + 'name' => 'entity', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 64, + ), + 'column' => array( + 'name' => 'column', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 128, + ), + 'function_name' => array( + 'name' => 'function_name', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 256, + ), + 'is_active' => array( + 'name' => 'is_active', + 'type' => CRM_Utils_Type::T_INT, + ) + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the array key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'id', + 'label' => 'label', + 'description' => 'description', + 'entity' => 'entity', + 'column' => 'column', + 'function_name' => 'function_name', + 'is_active' => 'is_active' + ); + } + return self::$_fieldKeys; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['activity'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} \ No newline at end of file diff --git a/CRM/Civirules/DAO/Event.php b/CRM/Civirules/DAO/Event.php new file mode 100644 index 0000000..3b7ecc1 --- /dev/null +++ b/CRM/Civirules/DAO/Event.php @@ -0,0 +1,108 @@ +<?php +/** + * DAO Event for table civirule_event + * + * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ +class CRM_Civirules_DAO_Event extends CRM_Core_DAO { + /** + * static instance to hold the field values + * + * @var array + * @static + */ + static $_fields = null; + static $_export = null; + /** + * empty definition for virtual function + */ + static function getTableName() { + return 'civirule_event'; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() { + if (!(self::$_fields)) { + self::$_fields = array( + 'id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => true + ) , + 'name' => array( + 'name' => 'name', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 64, + ), + 'label' => array( + 'name' => 'label', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 128, + ), + 'entity' => array( + 'name' => 'entity', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 64, + ), + 'action' => array( + 'name' => 'action', + 'type' => CRM_Utils_Type::T_STRING + ), + 'is_active' => array( + 'name' => 'is_active', + 'type' => CRM_Utils_Type::T_INT, + ) + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the array key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'id', + 'name' => 'name', + 'label' => 'label', + 'entity' => 'entity', + 'action' => 'action', + 'is_active' => 'is_active' + ); + } + return self::$_fieldKeys; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['activity'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} \ No newline at end of file diff --git a/CRM/Civirules/DAO/Rule.php b/CRM/Civirules/DAO/Rule.php new file mode 100644 index 0000000..77fc729 --- /dev/null +++ b/CRM/Civirules/DAO/Rule.php @@ -0,0 +1,102 @@ +<?php +/** + * DAO Rule for table civirule_rule + * + * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ +class CRM_Civirules_DAO_Rule extends CRM_Core_DAO { + /** + * static instance to hold the field values + * + * @var array + * @static + */ + static $_fields = null; + static $_export = null; + /** + * empty definition for virtual function + */ + static function getTableName() { + return 'civirule_rule'; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() { + if (!(self::$_fields)) { + self::$_fields = array( + 'id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => true + ) , + 'name' => array( + 'name' => 'name', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 64, + ) , + 'label' => array( + 'name' => 'label', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 128, + ), + 'is_active' => array( + 'name' => 'is_active', + 'type' => CRM_Utils_Type::T_INT, + ), + 'start_date' => array( + 'name' => 'start_date', + 'type' => CRM_Utils_Type::T_DATE, + ) + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the array key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'id', + 'name' => 'name', + 'label' => 'label', + 'is_active' => 'is_active', + 'start_date' => 'start_date' + ); + } + return self::$_fieldKeys; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['activity'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} \ No newline at end of file diff --git a/CRM/Civirules/DAO/RuleAction.php b/CRM/Civirules/DAO/RuleAction.php new file mode 100644 index 0000000..856e574 --- /dev/null +++ b/CRM/Civirules/DAO/RuleAction.php @@ -0,0 +1,117 @@ +<?php +/** + * DAO RuleAction for table civirule_rule_action + * + * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ +class CRM_Civirules_DAO_RuleAction extends CRM_Core_DAO { + /** + * static instance to hold the field values + * + * @var array + * @static + */ + static $_fields = null; + static $_export = null; + /** + * empty definition for virtual function + */ + static function getTableName() { + return 'civirule_rule_action'; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() { + if (!(self::$_fields)) { + self::$_fields = array( + 'id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => true + ) , + 'rule_id' => array( + 'name' => 'rule_id', + 'type' => CRM_Utils_Type::T_INT + ), + 'action_id' => array( + 'name' => 'action_id', + 'type' => CRM_Utils_Type::T_INT + ), + 'value' => array( + 'name' => 'value', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 128, + ), + 'comparison_id' => array( + 'name' => 'comparison_id', + 'type' => CRM_Utils_Type::T_INT + ), + 'params' => array( + 'name' => 'params', + 'type' => CRM_Utils_Type::T_BLOB + ), + 'civicrm_form_class' => array( + 'name' => 'civicrm_form_class', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 128 + ), + 'is_active' => array( + 'name' => 'is_active', + 'type' => CRM_Utils_Type::T_INT, + ), + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the array key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'id', + 'rule_id' => 'rule_id', + 'action_id' => 'action_id', + 'value' => 'value', + 'comparison_id' => 'comparison_id', + 'params' => 'params', + 'civicrm_form_class' => 'civicrm_form_class', + 'is_active' => 'is_active' + ); + } + return self::$_fieldKeys; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['activity'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} \ No newline at end of file diff --git a/CRM/Civirules/DAO/RuleCondition.php b/CRM/Civirules/DAO/RuleCondition.php new file mode 100644 index 0000000..ca287a7 --- /dev/null +++ b/CRM/Civirules/DAO/RuleCondition.php @@ -0,0 +1,106 @@ +<?php +/** + * DAO RuleCondition for table civirule_rule_condition + * + * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ +class CRM_Civirules_DAO_RuleCondition extends CRM_Core_DAO { + /** + * static instance to hold the field values + * + * @var array + * @static + */ + static $_fields = null; + static $_export = null; + /** + * empty definition for virtual function + */ + static function getTableName() { + return 'civirule_rule_condition'; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() { + if (!(self::$_fields)) { + self::$_fields = array( + 'id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => true + ) , + 'rule_id' => array( + 'name' => 'rule_id', + 'type' => CRM_Utils_Type::T_INT + ), + 'condition_id' => array( + 'name' => 'condition_id', + 'type' => CRM_Utils_Type::T_INT + ), + 'value' => array( + 'name' => 'value', + 'type' => CRM_Utils_Type::T_STRING, + 'maxlength' => 128, + ), + 'comparison_id' => array( + 'name' => 'comparison_id', + 'type' => CRM_Utils_Type::T_INT + ), + 'is_active' => array( + 'name' => 'is_active', + 'type' => CRM_Utils_Type::T_INT, + ), + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the array key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'id', + 'rule_id' => 'rule_id', + 'condition_id' => 'condition_id', + 'value' => 'value', + 'comparison_id' => 'comparison_id', + 'is_active' => 'is_active' + ); + } + return self::$_fieldKeys; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['activity'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} \ No newline at end of file diff --git a/CRM/Civirules/DAO/RuleEvent.php b/CRM/Civirules/DAO/RuleEvent.php new file mode 100644 index 0000000..a8db5a3 --- /dev/null +++ b/CRM/Civirules/DAO/RuleEvent.php @@ -0,0 +1,95 @@ +<?php +/** + * DAO RuleEvent for table civirule_rule_event + * + * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ +class CRM_Civirules_DAO_RuleEvent extends CRM_Core_DAO { + /** + * static instance to hold the field values + * + * @var array + * @static + */ + static $_fields = null; + static $_export = null; + /** + * empty definition for virtual function + */ + static function getTableName() { + return 'civirule_rule_event'; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() { + if (!(self::$_fields)) { + self::$_fields = array( + 'id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => true + ) , + 'rule_id' => array( + 'name' => 'rule_id', + 'type' => CRM_Utils_Type::T_INT + ), + 'event_id' => array( + 'name' => 'event_id', + 'type' => CRM_Utils_Type::T_INT + ), + 'is_active' => array( + 'name' => 'is_active', + 'type' => CRM_Utils_Type::T_INT, + ), + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the array key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'id', + 'rule_id' => 'rule_id', + 'event_id' => 'event_id', + 'is_active' => 'is_active', + ); + } + return self::$_fieldKeys; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['activity'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} \ No newline at end of file diff --git a/CRM/Civirules/Upgrader.php b/CRM/Civirules/Upgrader.php new file mode 100644 index 0000000..29ef36f --- /dev/null +++ b/CRM/Civirules/Upgrader.php @@ -0,0 +1,23 @@ +<?php + +/** + * Copyright (C) 2015 Coöperatieve CiviCooP U.A. <http://www.civicoop.org> + * Licensed to CiviCRM under the AGPL-3.0 + */ +class CRM_Civirules_Upgrader extends CRM_Civirules_Upgrader_Base { + /** + * Create CiviRules tables on extension install. Do not change the + * sequence as there will be dependencies in the foreign keys + */ + public function install() { + $this->executeSqlFile('sql/create_civirule_comparison.sql'); + $this->executeSqlFile('sql/create_civirule_condition.sql'); + $this->executeSqlFile('sql/create_civirule_data_selector.sql'); + $this->executeSqlFile('sql/create_civirule_event.sql'); + $this->executeSqlFile('sql/create_civirule_rule.sql'); + $this->executeSqlFile('sql/create_civirule_action.sql'); + $this->executeSqlFile('sql/create_civirule_rule_action.sql'); + $this->executeSqlFile('sql/create_civirule_rule_condition.sql'); + $this->executeSqlFile('sql/create_civirule_rule_event.sql'); + } +} \ No newline at end of file diff --git a/CRM/Civirules/Upgrader/Base.php b/CRM/Civirules/Upgrader/Base.php new file mode 100644 index 0000000..8700235 --- /dev/null +++ b/CRM/Civirules/Upgrader/Base.php @@ -0,0 +1,298 @@ +<?php + +// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file + +/** + * Base class which provides helpers to execute upgrade logic + */ +class CRM_Civirules_Upgrader_Base { + + /** + * @var varies, subclass of htis + */ + static $instance; + + /** + * @var CRM_Queue_TaskContext + */ + protected $ctx; + + /** + * @var string, eg 'com.example.myextension' + */ + protected $extensionName; + + /** + * @var string, full path to the extension's source tree + */ + protected $extensionDir; + + /** + * @var array(revisionNumber) sorted numerically + */ + private $revisions; + + /** + * Obtain a refernece to the active upgrade handler + */ + static public function instance() { + if (! self::$instance) { + // FIXME auto-generate + self::$instance = new CRM_Civirules_Upgrader( + 'org.civicoop.civirules', + realpath(__DIR__ .'/../../../') + ); + } + return self::$instance; + } + + /** + * Adapter that lets you add normal (non-static) member functions to the queue. + * + * Note: Each upgrader instance should only be associated with one + * task-context; otherwise, this will be non-reentrant. + * + * @code + * CRM_Civirules_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2'); + * @endcode + */ + static public function _queueAdapter() { + $instance = self::instance(); + $args = func_get_args(); + $instance->ctx = array_shift($args); + $instance->queue = $instance->ctx->queue; + $method = array_shift($args); + return call_user_func_array(array($instance, $method), $args); + } + + public function __construct($extensionName, $extensionDir) { + $this->extensionName = $extensionName; + $this->extensionDir = $extensionDir; + } + + // ******** Task helpers ******** + + /** + * Run a CustomData file + * + * @param string $relativePath the CustomData XML file path (relative to this extension's dir) + * @return bool + */ + public function executeCustomDataFile($relativePath) { + $xml_file = $this->extensionDir . '/' . $relativePath; + return $this->executeCustomDataFileByAbsPath($xml_file); + } + + /** + * Run a CustomData file + * + * @param string $xml_file the CustomData XML file path (absolute path) + * @return bool + */ + protected static function executeCustomDataFileByAbsPath($xml_file) { + require_once 'CRM/Utils/Migrate/Import.php'; + $import = new CRM_Utils_Migrate_Import(); + $import->run($xml_file); + return TRUE; + } + + /** + * Run a SQL file + * + * @param string $relativePath the SQL file path (relative to this extension's dir) + * @return bool + */ + public function executeSqlFile($relativePath) { + CRM_Utils_File::sourceSQLFile( + CIVICRM_DSN, + $this->extensionDir . '/' . $relativePath + ); + return TRUE; + } + + /** + * Run one SQL query + * + * This is just a wrapper for CRM_Core_DAO::executeSql, but it + * provides syntatic sugar for queueing several tasks that + * run different queries + */ + public function executeSql($query, $params = array()) { + // FIXME verify that we raise an exception on error + CRM_Core_DAO::executeSql($query, $params); + return TRUE; + } + + /** + * Syntatic sugar for enqueuing a task which calls a function + * in this class. The task is weighted so that it is processed + * as part of the currently-pending revision. + * + * After passing the $funcName, you can also pass parameters that will go to + * the function. Note that all params must be serializable. + */ + public function addTask($title) { + $args = func_get_args(); + $title = array_shift($args); + $task = new CRM_Queue_Task( + array(get_class($this), '_queueAdapter'), + $args, + $title + ); + return $this->queue->createItem($task, array('weight' => -1)); + } + + // ******** Revision-tracking helpers ******** + + /** + * Determine if there are any pending revisions + * + * @return bool + */ + public function hasPendingRevisions() { + $revisions = $this->getRevisions(); + $currentRevision = $this->getCurrentRevision(); + + if (empty($revisions)) { + return FALSE; + } + if (empty($currentRevision)) { + return TRUE; + } + + return ($currentRevision < max($revisions)); + } + + /** + * Add any pending revisions to the queue + */ + public function enqueuePendingRevisions(CRM_Queue_Queue $queue) { + $this->queue = $queue; + + $currentRevision = $this->getCurrentRevision(); + foreach ($this->getRevisions() as $revision) { + if ($revision > $currentRevision) { + $title = ts('Upgrade %1 to revision %2', array( + 1 => $this->extensionName, + 2 => $revision, + )); + + // note: don't use addTask() because it sets weight=-1 + + $task = new CRM_Queue_Task( + array(get_class($this), '_queueAdapter'), + array('upgrade_' . $revision), + $title + ); + $this->queue->createItem($task); + + $task = new CRM_Queue_Task( + array(get_class($this), '_queueAdapter'), + array('setCurrentRevision', $revision), + $title + ); + $this->queue->createItem($task); + } + } + } + + /** + * Get a list of revisions + * + * @return array(revisionNumbers) sorted numerically + */ + public function getRevisions() { + if (! is_array($this->revisions)) { + $this->revisions = array(); + + $clazz = new ReflectionClass(get_class($this)); + $methods = $clazz->getMethods(); + foreach ($methods as $method) { + if (preg_match('/^upgrade_(.*)/', $method->name, $matches)) { + $this->revisions[] = $matches[1]; + } + } + sort($this->revisions, SORT_NUMERIC); + } + + return $this->revisions; + } + + public function getCurrentRevision() { + // return CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName); + $key = $this->extensionName . ':version'; + return CRM_Core_BAO_Setting::getItem('Extension', $key); + } + + public function setCurrentRevision($revision) { + // We call this during hook_civicrm_install, but the underlying SQL + // UPDATE fails because the extension record hasn't been INSERTed yet. + // Instead, track revisions in our own namespace. + // CRM_Core_BAO_Extension::setSchemaVersion($this->extensionName, $revision); + + $key = $this->extensionName . ':version'; + CRM_Core_BAO_Setting::setItem($revision, 'Extension', $key); + return TRUE; + } + + // ******** Hook delegates ******** + + public function onInstall() { + $files = glob($this->extensionDir . '/sql/*_install.sql'); + if (is_array($files)) { + foreach ($files as $file) { + CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file); + } + } + $files = glob($this->extensionDir . '/xml/*_install.xml'); + if (is_array($files)) { + foreach ($files as $file) { + $this->executeCustomDataFileByAbsPath($file); + } + } + if (is_callable(array($this, 'install'))) { + $this->install(); + } + $revisions = $this->getRevisions(); + if (!empty($revisions)) { + $this->setCurrentRevision(max($revisions)); + } + } + + public function onUninstall() { + if (is_callable(array($this, 'uninstall'))) { + $this->uninstall(); + } + $files = glob($this->extensionDir . '/sql/*_uninstall.sql'); + if (is_array($files)) { + foreach ($files as $file) { + CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file); + } + } + $this->setCurrentRevision(NULL); + } + + public function onEnable() { + // stub for possible future use + if (is_callable(array($this, 'enable'))) { + $this->enable(); + } + } + + public function onDisable() { + // stub for possible future use + if (is_callable(array($this, 'disable'))) { + $this->disable(); + } + } + + public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) { + switch($op) { + case 'check': + return array($this->hasPendingRevisions()); + case 'enqueue': + return $this->enqueuePendingRevisions($queue); + default: + } + } +} diff --git a/sql/create_civirule_action.sql b/sql/create_civirule_action.sql new file mode 100644 index 0000000..211acb4 --- /dev/null +++ b/sql/create_civirule_action.sql @@ -0,0 +1,17 @@ +CREATE TABLE IF NOT EXISTS `civirule_action` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `name` VARCHAR(64) NULL, + `label` VARCHAR(128) NULL, + `data_selector_id` INT UNSIGNED NULL, + `is_active` TINYINT NULL DEFAULT 1, + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC), + INDEX `fk_data_selector_idx` (`data_selector_id` ASC), + CONSTRAINT `fk_data_selector` + FOREIGN KEY (`data_selector_id`) + REFERENCES `civirule_data_selector` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_general_ci diff --git a/sql/create_civirule_comparison.sql b/sql/create_civirule_comparison.sql new file mode 100644 index 0000000..aa69dec --- /dev/null +++ b/sql/create_civirule_comparison.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS `civirule_comparison` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `label` VARCHAR(45) NULL, + `operator` VARCHAR(45) NULL, + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC)) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_general_ci diff --git a/sql/create_civirule_condition.sql b/sql/create_civirule_condition.sql new file mode 100644 index 0000000..be671db --- /dev/null +++ b/sql/create_civirule_condition.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS `civirule_condition` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `name` VARCHAR(64) NULL, + `label` VARCHAR(128) NULL, + `function_name` VARCHAR(256) NULL, + `civicrm_form_class` VARCHAR(128) NULL, + `is_active` TINYINT NULL DEFAULT 1, + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC)) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_general_ci diff --git a/sql/create_civirule_data_selector.sql b/sql/create_civirule_data_selector.sql new file mode 100644 index 0000000..bdeac4d --- /dev/null +++ b/sql/create_civirule_data_selector.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS `civirule_data_selector` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `entity` VARCHAR(64) NULL, + `column` VARCHAR(128) NULL, + `label` VARCHAR(68) NULL, + `description` TEXT NULL, + `is_active` TINYINT NULL DEFAULT 1, + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC)) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_general_ci diff --git a/sql/create_civirule_event.sql b/sql/create_civirule_event.sql new file mode 100644 index 0000000..198fc7a --- /dev/null +++ b/sql/create_civirule_event.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS `civirule_event` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `name` VARCHAR(64) NULL, + `label` VARCHAR(128) NULL, + `entity` VARCHAR(68) NULL, + `action` VARCHAR(45) NULL, + `is_active` TINYINT NULL DEFAULT 1, + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC)) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_general_ci diff --git a/sql/create_civirule_rule.sql b/sql/create_civirule_rule.sql new file mode 100644 index 0000000..3c1db6f --- /dev/null +++ b/sql/create_civirule_rule.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS `civirule_rule` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `name` VARCHAR(64) NULL, + `label` VARCHAR(128) NULL, + `is_active` TINYINT NULL DEFAULT 1, + `start_date` DATE NULL, + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC)) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_general_ci diff --git a/sql/create_civirule_rule_action.sql b/sql/create_civirule_rule_action.sql new file mode 100644 index 0000000..503a00c --- /dev/null +++ b/sql/create_civirule_rule_action.sql @@ -0,0 +1,31 @@ +CREATE TABLE IF NOT EXISTS `civirule_rule_action` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `rule_id` INT UNSIGNED NULL, + `action_id` INT UNSIGNED NULL, + `value` VARCHAR(128) NULL, + `comparison_id` INT UNSIGNED NULL, + `extra_params` TEXT NULL, + `civicrm_form_class` VARCHAR(128) NULL, + `is_active` TINYINT NULL DEFAULT 1, + UNIQUE INDEX `id_UNIQUE` (`id` ASC), + INDEX `rule_idx` (`rule_id` ASC), + INDEX `fk_action_idx` (`action_id` ASC), + INDEX `fk_comparison_idx` (`comparison_id` ASC), + CONSTRAINT `fk_rule` + FOREIGN KEY (`rule_id`) + REFERENCES `civirule_rule` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_action` + FOREIGN KEY (`action_id`) + REFERENCES `civirule_action` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_comparison` + FOREIGN KEY (`comparison_id`) + REFERENCES `civirule_comparison` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_general_ci diff --git a/sql/create_civirule_rule_condition.sql b/sql/create_civirule_rule_condition.sql new file mode 100644 index 0000000..868a754 --- /dev/null +++ b/sql/create_civirule_rule_condition.sql @@ -0,0 +1,30 @@ +CREATE TABLE IF NOT EXISTS `civirule_rule_condition` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `rule_id` INT UNSIGNED NULL, + `condition_id` INT UNSIGNED NULL, + `value` VARCHAR(128) NULL, + `comparison_id` INT UNSIGNED NULL, + `is_active` TINYINT NULL DEFAULT 1, + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC), + INDEX `rule_idx` (`rule_id` ASC), + INDEX `fk_condition_idx` (`condition_id` ASC), + INDEX `fk_comparison_idx` (`comparison_id` ASC), + CONSTRAINT `fk_rule` + FOREIGN KEY (`rule_id`) + REFERENCES `civirule_rule` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_condition` + FOREIGN KEY (`condition_id`) + REFERENCES `civirule_condition` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_comparison` + FOREIGN KEY (`comparison_id`) + REFERENCES `civirule_comparison` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_general_ci diff --git a/sql/create_civirule_rule_event.sql b/sql/create_civirule_rule_event.sql new file mode 100644 index 0000000..7b5c375 --- /dev/null +++ b/sql/create_civirule_rule_event.sql @@ -0,0 +1,22 @@ +CREATE TABLE IF NOT EXISTS .`civirule_rule_event` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `rule_id` INT UNSIGNED NULL, + `event_id` INT UNSIGNED NULL, + `is_active` TINYINT NULL DEFAULT 1, + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC), + INDEX `rule_idx` (`rule_id` ASC), + INDEX `event_idx` (`event_id` ASC), + CONSTRAINT `fk_rule` + FOREIGN KEY (`rule_id`) + REFERENCES `civirule_rule` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_event` + FOREIGN KEY (`event_id`) + REFERENCES `civirule_event` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_general_ci -- GitLab