Skip to content
Snippets Groups Projects
Commit 2415fc85 authored by jaapjansma's avatar jaapjansma
Browse files

refactored directory structure and form class for actions

parent 2aca212c
No related branches found
No related tags found
No related merge requests found
<?php
class CRM_CivirulesActions_Form_Form extends CRM_Core_Form
{
protected $ruleActionId = false;
protected $ruleAction;
protected $action;
protected $rule;
protected $event;
/**
* @var CRM_Civirules_Event
*/
protected $eventClass;
/**
* Overridden parent method to perform processing before form is build
*
* @access public
*/
public function preProcess()
{
$this->ruleActionId = CRM_Utils_Request::retrieve('rule_action_id', 'Integer');
$this->ruleAction = new CRM_Civirules_BAO_RuleAction();
$this->ruleAction->id = $this->ruleActionId;
$this->action = new CRM_Civirules_BAO_Action();
$this->rule = new CRM_Civirules_BAO_Rule();
$this->event = new CRM_Civirules_BAO_Event();
if (!$this->ruleAction->find(true)) {
throw new Exception('Civirules could not find ruleAction');
}
$this->action->id = $this->ruleAction->action_id;
if (!$this->action->find(true)) {
throw new Exception('Civirules could not find action');
}
$this->rule->id = $this->ruleAction->rule_id;
if (!$this->rule->find(true)) {
throw new Exception('Civirules could not find rule');
}
$this->event->id = $this->rule->event_id;
if (!$this->event->find(true)) {
throw new Exception('Civirules could not find event');
}
$this->eventClass = CRM_Civirules_BAO_Event::getPostEventObjectByClassName($this->event->class_name, true);
$this->eventClass->setEventId($this->event->id);
parent::preProcess();
$this->setFormTitle();
}
/**
* Overridden parent method to set default values
*
* @return array $defaultValues
* @access public
*/
public function setDefaultValues() {
$defaultValues = array();
$defaultValues['rule_action_id'] = $this->ruleActionId;
return $defaultValues;
}
public function postProcess() {
$session = CRM_Core_Session::singleton();
$session->setStatus('Action '.$this->condition->label.' parameters updated to CiviRule '.$this->rule->label, 'Action parameters updated', 'success');
$redirectUrl = CRM_Utils_System::url('civicrm/civirule/form/rule', 'action=update&id='.$this->ruleId, TRUE);
CRM_Utils_System::redirect($redirectUrl);
}
/**
* Method to set the form title
*
* @access protected
*/
protected function setFormTitle() {
$title = 'CiviRules Edit Action parameters';
$this->assign('ruleActionHeader', 'Edit Action '.$this->action->label.' of CiviRule '.$this->rule->label);
CRM_Utils_System::setTitle($title);
}
}
\ No newline at end of file
......@@ -6,37 +6,8 @@
* @license AGPL-3.0
*/
class CRM_CivirulesActions_Form_GroupContact extends CRM_Core_Form {
class CRM_CivirulesActions_Form_GroupContact_GroupId extends CRM_CivirulesActions_Form_Form {
protected $ruleActionId = false;
protected $ruleAction;
protected $action;
/**
* Overridden parent method to do pre-form building processing
*
* @throws Exception when action or rule action not found
* @access public
*/
public function preProcess() {
$this->ruleActionId = CRM_Utils_Request::retrieve('rule_action_id', 'Integer');
$this->ruleAction = new CRM_Civirules_BAO_RuleAction();
$this->action = new CRM_Civirules_BAO_Action();
$this->ruleAction->id = $this->ruleActionId;
if ($this->ruleAction->find(true)) {
$this->action->id = $this->ruleAction->action_id;
if (!$this->action->find(true)) {
throw new Exception('CiviRules Could not find action with id '.$this->ruleAction->action_id);
}
} else {
throw new Exception('CiviRules Could not find rule action with id '.$this->ruleActionId);
}
parent::preProcess();
}
/**
* Method to get groups
......@@ -54,8 +25,6 @@ class CRM_CivirulesActions_Form_GroupContact extends CRM_Core_Form {
* @access public
*/
public function buildQuickForm() {
$this->setFormTitle();
$this->add('hidden', 'rule_action_id');
$this->add('select', 'group_id', ts('Group'), $this->getGroups(), true);
......@@ -72,12 +41,8 @@ class CRM_CivirulesActions_Form_GroupContact extends CRM_Core_Form {
* @access public
*/
public function setDefaultValues() {
$data = array();
$defaultValues = array();
$defaultValues['rule_action_id'] = $this->ruleActionId;
if (!empty($this->ruleAction->action_params)) {
$data = unserialize($this->ruleCondition->action_params);
}
$defaultValues = parent::setDefaultValues();
$data = unserialize($this->ruleAction->action_params);
if (!empty($data['group_id'])) {
$defaultValues['group_id'] = $data['group_id'];
}
......@@ -91,29 +56,9 @@ class CRM_CivirulesActions_Form_GroupContact extends CRM_Core_Form {
*/
public function postProcess() {
$data['group_id'] = $this->_submitValues['group_id'];
$ruleAction = new CRM_Civirules_BAO_RuleAction();
$ruleAction->id = $this->ruleActionId;
$ruleAction->action_params = serialize($data);
$ruleAction->save();
$session = CRM_Core_Session::singleton();
$session->setStatus('Action '.$this->action->label.' parameters updated to CiviRule '.CRM_Civirules_BAO_Rule::getRuleLabelWithId($this->ruleAction->rule_id),
'Action parameters updated', 'success');
$redirectUrl = CRM_Utils_System::url('civicrm/civirule/form/rule', 'action=update&id='.$this->ruleAction->rule_id, TRUE);
CRM_Utils_System::redirect($redirectUrl);
}
/**
* Method to set the form title
*
* @access protected
*/
protected function setFormTitle() {
$title = 'CiviRules Edit Action parameters';
$this->assign('ruleActionHeader', 'Edit action '.$this->action->label.' of CiviRule '.CRM_Civirules_BAO_Rule::getRuleLabelWithId($this->ruleAction->rule_id));
CRM_Utils_System::setTitle($title);
$this->ruleAction->action_params = serialize($data);
$this->ruleAction->save();
parent::postProcess();
}
}
\ No newline at end of file
......@@ -56,7 +56,7 @@
</item>
<item>
<path>civicrm/civirule/form/action/groupcontact</path>
<page_callback>CRM_CivirulesActions_Form_GroupContact</page_callback>
<page_callback>CRM_CivirulesActions_Form_GroupContact_GroupId</page_callback>
<title>Group contact</title>
<access_arguments>access CiviCRM</access_arguments>
</item>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment