Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/
class CRM_CivirulesActions_Case_Form_SetDateField extends CRM_CivirulesActions_Form_Form {
protected function getFields() {
$return = array();
$fields = civicrm_api3('Case', 'getfields', array('limit' => 99999));
foreach ($fields['values'] as $field) {
if (!isset($field['type'])) {
continue;
}
if (!($field['type'] & CRM_Utils_Type::T_DATE)) {
continue; //Field is not a Date field.
}
$fieldKey = $field['name'];
if (isset($field['title'])) {
$label = trim($field['title']);
} elseif (isset($field['label'])) {
$label = trim($field['label']);
} else {
$label = "";
}
if (empty($label)) {
$label = $field['name'];
}
if (!empty($field['groupTitle'])) {
$label = $field['groupTitle'].': '.$label;
}
$return[$fieldKey] = $label;
}
return $return;
}
public function buildQuickForm() {
$this->add('hidden', 'rule_action_id');
$this->add('select', 'field', ts('Field'), $this->getFields(), true, array('class' => 'crm-select2'));
$delayList = array('' => ts(' - Set date to time of processing of action - ')) + CRM_Civirules_Delay_Factory::getOptionList();
$this->add('select', 'date', ts('Set date'), $delayList);
foreach(CRM_Civirules_Delay_Factory::getAllDelayClasses() as $delay_class) {
$delay_class->addElements($this, 'date', $this->rule);
}
$this->assign('delayClasses', CRM_Civirules_Delay_Factory::getAllDelayClasses());
$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => ts('Cancel'))));
}
/**
* Overridden parent method to set default values
*
* @return array $defaultValues
* @access public
*/
public function setDefaultValues() {
$defaultValues = parent::setDefaultValues();
if (!empty($data['field'])) {
$defaultValues['field'] = $data['field'];
}
$data = unserialize($this->ruleAction->action_params);
foreach(CRM_Civirules_Delay_Factory::getAllDelayClasses() as $delay_class) {
$delay_class->setDefaultValues($defaultValues, 'date', $this->rule);
}
$activityDateClass = unserialize($data['date']);
if ($activityDateClass) {
$defaultValues['date'] = get_class($activityDateClass);
foreach($activityDateClass->getValues('date', $this->rule) as $key => $val) {
$defaultValues[$key] = $val;
}
}
return $defaultValues;
}
/**
* Function to add validation action rules (overrides parent function)
*
* @access public
*/
public function addRules() {
parent::addRules();
$this->addFormRule(array(
'CRM_CivirulesActions_Case_Form_SetDateField',
'validateDate'
));
}
/**
* Function to validate value of the delay
*
* @param array $fields
* @return array|bool
* @access public
* @static
*/
static function validateDate($fields) {
$errors = array();
if (!empty($fields['date'])) {
$ruleActionId = CRM_Utils_Request::retrieve('rule_action_id', 'Integer');
$ruleAction = new CRM_Civirules_BAO_RuleAction();
$ruleAction->id = $ruleActionId;
$ruleAction->find(true);
$rule = new CRM_Civirules_BAO_Rule();
$rule->id = $ruleAction->rule_id;
$rule->find(true);
$activityDateClass = CRM_Civirules_Delay_Factory::getDelayClassByName($fields['date']);
$activityDateClass->validate($fields, $errors, 'date', $rule);
}
if (count($errors)) {
return $errors;
}
return TRUE;
}
/**
* Overridden parent method to process form data after submitting
*
* @access public
*/
public function postProcess() {
$data['date'] = 'null';
if (!empty($this->_submitValues['date'])) {
$scheduledDateClass = CRM_Civirules_Delay_Factory::getDelayClassByName($this->_submitValues['date']);
$scheduledDateClass->setValues($this->_submitValues, 'date', $this->rule);
$data['date'] = serialize($scheduledDateClass);
}
$data['field'] = $this->_submitValues['field'];
$this->ruleAction->action_params = serialize($data);
$this->ruleAction->save();
parent::postProcess();
}
}