Commit 18aaa960 authored by jaapjansma's avatar jaapjansma
Browse files

improved and added action to retrieve repetition of an event

parent 5d27e7f8
......@@ -7,6 +7,7 @@ Version 1.4 (not yet released)
* Added action to create an organization
* Added action to retrieve the labels from option values
* Added action to repeat an event
* Added action to get repetition of an event
* Added action to create a case
Version 1.3
......
......@@ -43,7 +43,7 @@ class CreateRecurringEvent extends AbstractAction {
$specs->addSpecification(new Specification('event_id', 'Integer', E::ts('Event ID'), true, false));
$specs->addSpecification(new Specification('unit', 'String', E::ts('Frequency Unit'), true, false, null, \CRM_Core_SelectValues::getRecurringFrequencyUnits()));
$specs->addSpecification(new Specification('interval', 'Integer', E::ts('Frequency Interval'), true, 1));
$specs->addSpecification(new Specification('start_date', 'Timestamp', E::ts('Start Repating from'), true, null));
$specs->addSpecification(new Specification('start_date', 'Timestamp', E::ts('Start from (default to start of event)'), false, null));
$specs->addSpecification(new Specification('start_action_offset', 'Integer', E::ts('Ends after'), false, null));
$specs->addSpecification(new Specification('repeat_absolute_date', 'Date', E::ts('End on'), false, null));
$specs->addSpecification(new Specification('start_action_condition', 'String', E::ts('Repeats On (day of weeks)'), false, null, null, $weekDays, true));
......@@ -110,9 +110,16 @@ class CreateRecurringEvent extends AbstractAction {
$params['entity_table'] = 'civicrm_event';
$params['used_for'] = 'civicrm_event';
$config = \CRM_Core_Config::singleton();
$startDate = new \DateTime(\CRM_Utils_Date::customFormat($parameters->getParameter('start_date'), '%Y-%m-%d %H:%i'));
$params['repetition_start_date'] = \CRM_Utils_Date::customFormat($parameters->getParameter('start_date'), $config->dateformatFull);
$params['repetition_start_date_time'] = $startDate->format('H:i');
if ($parameters->doesParameterExists('start_date')) {
$startDate = new \DateTime(\CRM_Utils_Date::customFormat($parameters->getParameter('start_date'), '%Y-%m-%d %H:%i'));
$params['repetition_start_date'] = \CRM_Utils_Date::customFormat($parameters->getParameter('start_date'), $config->dateformatFull);
$params['repetition_start_date_time'] = $startDate->format('H:i');
} else {
$startDate = civicrm_api3('Event', 'getvalue', array('id' => $event_id, 'return' => 'start_date'));
$startDate = new \DateTime($startDate);
$params['repetition_start_date'] = \CRM_Utils_Date::customFormat($startDate->format('Ymd His'), $config->dateformatFull);
$params['repetition_start_date_time'] = $startDate->format('H:i');
}
$params['ends'] = $parameters->getParameter('ends');
if ($parameters->doesParameterExists('start_action_offset') && $parameters->getParameter('start_action_offset')) {
$params['ends'] = 1;
......@@ -133,11 +140,100 @@ class CreateRecurringEvent extends AbstractAction {
$recurobj = new \CRM_Core_BAO_RecurringEntity();
$dbParams = $recurobj->mapFormValuesToDB($params);
/*var_dump($params);
var_dump($dbParams);
exit();*/
//@Todo Find existing schedule.
$this->removeCurrentActionSchedule($event_id);
$actionScheduleObj = \CRM_Core_BAO_ActionSchedule::add($dbParams);
$recursion = new \CRM_Core_BAO_RecurringEntity();
$recursion->dateColumns = $params['dateColumns'];
$recursion->scheduleId = $actionScheduleObj->id;
if (!empty($params['intervalDateColumns'])) {
$recursion->intervalDateColumns = $params['intervalDateColumns'];
}
$recursion->entity_id = $params['entity_id'];
$recursion->entity_table = $params['entity_table'];
$linkedEntities = $this->getLinkedEntities($event_id);
if (!empty($linkedEntities)) {
$recursion->linkedEntities = $linkedEntities;
}
$recursion->generate();
}
/**
* Remove a current schedule
*
* @param $event_id
*/
protected function removeCurrentActionSchedule($event_id) {
$sql = "SELECT id FROM civicrm_action_schedule WHERE used_for = 'civicrm_event' AND 'entity_value' = %1";
$sqlParams[1] = arary($event_id, 'Integer');
$id = \CRM_Core_DAO::singleValueQuery($sql, $sqlParams);
\CRM_Core_BAO_ActionSchedule::del($id);
//If entity has any pre delete function, consider that first
if (\CRM_Utils_Array::value('pre_delete_func', \CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper['civicrm_event']) &&
\CRM_Utils_Array::value('helper_class', \CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper['civicrm_event'])
) {
$preDeleteResult = call_user_func_array(\CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper['civicrm_event']['pre_delete_func'], array($event_id));
if (!empty($preDeleteResult)) {
call_user_func(array(\CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper['civicrm_event']['helper_class'], $preDeleteResult));
}
}
//Ready to execute delete on entities if it has delete function set
if (\CRM_Utils_Array::value('delete_func', \CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper['civicrm_event']) &&
\CRM_Utils_Array::value('helper_class', \CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper['civicrm_event'])
) {
//Check if pre delete function has some ids to be deleted
if (!empty(\CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted)) {
foreach (\CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted as $eid) {
$result = civicrm_api3(
'Event',
\CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper['civicrm_event']['delete_func'],
array(
'sequential' => 1,
'id' => $eid,
)
);
if ($result['error']) {
throw new \Exception('Error during creating of repeating events');
}
}
}
else {
$getRelatedEntities = \CRM_Core_BAO_RecurringEntity::getEntitiesFor($event_id, 'civicrm_event', FALSE);
foreach ($getRelatedEntities as $key => $value) {
$result = civicrm_api3(
'Event',
\CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper['civicrm_event']['delete_func'],
array(
'sequential' => 1,
'id' => $value['id'],
)
);
if ($result['error']) {
throw new \Exception('Error during creating of repeating events');
}
}
}
}
// find all entities from the recurring set. At this point we 'll get entities which were not deleted
// for e.g due to participants being present. We need to delete them from recurring tables anyway.
$pRepeatingEntities = \CRM_Core_BAO_RecurringEntity::getEntitiesFor($event_id, 'civicrm_event');
foreach ($pRepeatingEntities as $val) {
\CRM_Core_BAO_RecurringEntity::delEntity($val['id'], $val['table'], TRUE);
}
}
/**
* Returns a list with linked entities
*
* @param $event_id
*
* @return array
*/
protected function getLinkedEntities($event_id) {
$linkedEntities = array(
array(
'table' => 'civicrm_price_set_entity',
......@@ -176,123 +272,7 @@ exit();*/
'isRecurringEntityRecord' => TRUE,
),
);
$actionScheduleObj = \CRM_Core_BAO_ActionSchedule::add($dbParams);
$excludeDateList = array();
if (\CRM_Utils_Array::value('exclude_date_list', $params) && \CRM_Utils_Array::value('parent_entity_id', $params) && $actionScheduleObj->entity_value) {
//Since we get comma separated values lets get them in array
$excludeDates = explode(",", $params['exclude_date_list']);
//Check if there exists any values for this option group
$optionGroupIdExists = \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
'civicrm_event_repeat_exclude_dates_' . $params['parent_entity_id'],
'id',
'name'
);
if ($optionGroupIdExists) {
\CRM_Core_BAO_OptionGroup::del($optionGroupIdExists);
}
$optionGroupParams = array(
'name' => 'civicrm_event_repeat_exclude_dates_' . $actionScheduleObj->entity_value,
'title' => 'civicrm_event recursion',
'is_reserved' => 0,
'is_active' => 1,
);
$opGroup = \CRM_Core_BAO_OptionGroup::add($optionGroupParams);
if ($opGroup->id) {
$oldWeight = 0;
$fieldValues = array('option_group_id' => $opGroup->id);
foreach ($excludeDates as $val) {
$optionGroupValue = array(
'option_group_id' => $opGroup->id,
'label' => \CRM_Utils_Date::processDate($val),
'value' => \CRM_Utils_Date::processDate($val),
'name' => $opGroup->name,
'description' => 'Used for recurring civicrm_event',
'weight' => \CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, \CRM_Utils_Array::value('weight', $params), $fieldValues),
'is_active' => 1,
);
$excludeDateList[] = $optionGroupValue['value'];
\CRM_Core_BAO_OptionValue::create($optionGroupValue);
}
}
}
//Delete relations if any from recurring entity tables before inserting new relations for this entity id
if ($params['entity_id']) {
//If entity has any pre delete function, consider that first
if (\CRM_Utils_Array::value('pre_delete_func', \CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]) &&
\CRM_Utils_Array::value('helper_class', \CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']])
) {
$preDeleteResult = call_user_func_array(\CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['pre_delete_func'], array($params['entity_id']));
if (!empty($preDeleteResult)) {
call_user_func(array(\CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['helper_class'], $preDeleteResult));
}
}
//Ready to execute delete on entities if it has delete function set
if (\CRM_Utils_Array::value('delete_func', \CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]) &&
\CRM_Utils_Array::value('helper_class', \CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']])
) {
//Check if pre delete function has some ids to be deleted
if (!empty(\CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted)) {
foreach (\CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted as $eid) {
$result = civicrm_api3(
'Event',
\CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'],
array(
'sequential' => 1,
'id' => $eid,
)
);
if ($result['error']) {
throw new \Exception('Error during creating of repeating events');
}
}
}
else {
$getRelatedEntities = \CRM_Core_BAO_RecurringEntity::getEntitiesFor($params['entity_id'], $params['entity_table'], FALSE);
foreach ($getRelatedEntities as $key => $value) {
$result = civicrm_api3(
'Event',
\CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'],
array(
'sequential' => 1,
'id' => $value['id'],
)
);
if ($result['error']) {
throw new \Exception('Error during creating of repeating events');
}
}
}
}
// find all entities from the recurring set. At this point we 'll get entities which were not deleted
// for e.g due to participants being present. We need to delete them from recurring tables anyway.
$pRepeatingEntities = \CRM_Core_BAO_RecurringEntity::getEntitiesFor($params['entity_id'], $params['entity_table']);
foreach ($pRepeatingEntities as $val) {
\CRM_Core_BAO_RecurringEntity::delEntity($val['id'], $val['table'], TRUE);
}
}
$recursion = new \CRM_Core_BAO_RecurringEntity();
$recursion->dateColumns = $params['dateColumns'];
$recursion->scheduleId = $actionScheduleObj->id;
if (!empty($excludeDateList)) {
$recursion->excludeDates = $excludeDateList;
$recursion->excludeDateRangeColumns = $params['excludeDateRangeColumns'];
}
if (!empty($params['intervalDateColumns'])) {
$recursion->intervalDateColumns = $params['intervalDateColumns'];
}
$recursion->entity_id = $params['entity_id'];
$recursion->entity_table = $params['entity_table'];
if (!empty($linkedEntities)) {
$recursion->linkedEntities = $linkedEntities;
}
$recursion->generate();
return $linkedEntities;
}
}
\ No newline at end of file
<?php
namespace Civi\ActionProvider\Action\Event;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Action\Contact\ContactActionUtils;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use \Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
use Dompdf\Exception;
class GetRecurringEvent extends AbstractAction {
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
$weekDays = array(
'sunday' => E::ts('Sunday'),
'monday' => E::ts('Monday'),
'tuesday' => E::ts('Tuesday'),
'wednesday' => E::ts('Wednesday'),
'thursday' => E::ts('Thursday'),
'friday' => E::ts('Friday'),
'saturday' => E::ts('Saturday'),
);
$daoOfWeekNo = array(
'first' => E::ts('First'),
'second' => E::ts('Second'),
'third' => E::ts('Third'),
'fourth' => E::ts('Fourth'),
'last' => E::ts('Last'),
);
$repeatsBy = array(
'1' => E::ts('Day of the month'),
'2' => E::ts('Day of the week'),
);
$ends = array(
'1' => ts('After'),
'2' => ts('On'),
);
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('event_id', 'Integer', E::ts('Event ID'), true, false));
$specs->addSpecification(new Specification('unit', 'String', E::ts('Frequency Unit'), true, false, null, \CRM_Core_SelectValues::getRecurringFrequencyUnits()));
$specs->addSpecification(new Specification('interval', 'Integer', E::ts('Frequency Interval'), true, 1));
$specs->addSpecification(new Specification('start_date', 'Timestamp', E::ts('Start from (default to start of event)'), false, null));
$specs->addSpecification(new Specification('start_action_offset', 'Integer', E::ts('Ends after'), false, null));
$specs->addSpecification(new Specification('repeat_absolute_date', 'Date', E::ts('End on'), false, null));
$specs->addSpecification(new Specification('start_action_condition', 'String', E::ts('Repeats On (day of weeks)'), false, null, null, $weekDays, true));
$specs->addSpecification(new Specification('limit_to_day_of_month', 'Integer', E::ts('Limit to day of the month'), false, null, null, \CRM_Core_SelectValues::getNumericOptions(1, 31)));
$specs->addSpecification(new Specification('limit_to_day_of_week', 'String', E::ts('Limit to day of week'), false, null, null, $weekDays));
$specs->addSpecification(new Specification('limit_to_day_of_week_no', 'String', E::ts('Limit to week number in month'), null, null, null, $daoOfWeekNo));
$specs->addSpecification(new Specification('repeats_by', 'Integer', E::ts("Repeats by"), false, null, null, $repeatsBy));
$specs->addSpecification(new Specification('ends', 'Integer', E::ts("Ends"), false, null, null, $ends));
return $specs;
}
/**
* Returns the specification of the output parameters of this action.
*
* This function could be overriden by child classes.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag(array(
new Specification('event_id', 'Integer', E::ts('Event ID')),
));
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag();
}
/**
* Run the action
*
* @param ParameterInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$event_id = $parameters->getParameter('event_id');
$output->setParameter('event_id', $event_id);
$entityId = $event_id;
$checkParentExistsForThisId = \CRM_Core_BAO_RecurringEntity::getParentFor($event_id, 'civicrm_event');
if ($checkParentExistsForThisId) {
$entityId = $checkParentExistsForThisId;
}
$_scheduleReminderDetails = \CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($entityId, 'civicrm_event');
if (property_exists($_scheduleReminderDetails, 'id')) {
$output->setParameter('unit', $_scheduleReminderDetails->repetition_frequency_unit);
$output->setParameter('interval', $_scheduleReminderDetails->repetition_frequency_interval);
$output->setParameter('start_action_offset', $_scheduleReminderDetails->start_action_offset);
$repeat_absolute_date = \CRM_Utils_Date::setDateDefaults($_scheduleReminderDetails->absolute_date);
$output->setParameter('repeat_absolute_date', $repeat_absolute_date[0]);
$output->setParameter('limit_to_day_of_month', $_scheduleReminderDetails->limit_to);
if ($_scheduleReminderDetails->entity_status) {
$explodeStartActionCondition = explode(" ", $_scheduleReminderDetails->entity_status);
$output->setParameter('limit_to_day_of_week_no', $explodeStartActionCondition[0]);
$output->setParameter('limit_to_day_of_week', $explodeStartActionCondition[1]);
}
$output->setParameter('repeats_by', 1);
if ($_scheduleReminderDetails->entity_status) {
$output->setParameter('repeats_by', 2);
}
$output->setParameter('ends', 1);
if ($_scheduleReminderDetails->absolute_date) {
$output->setParameter('ends', 2);
}
}
}
}
\ No newline at end of file
......@@ -191,6 +191,9 @@ class Provider {
$this->addActionWithoutFiltering('CreateRecurringEvent', '\Civi\ActionProvider\Action\Event\CreateRecurringEvent', E::ts('Repeat an event') , array(
AbstractAction::DATA_MANIPULATION_TAG,
));
$this->addActionWithoutFiltering('GetRecurringEvent', '\Civi\ActionProvider\Action\Event\GetRecurringEvent', E::ts('Get event repetition') , array(
AbstractAction::DATA_MANIPULATION_TAG,
));
$this->addActionWithoutFiltering('GetEvent', '\Civi\ActionProvider\Action\Event\GetEvent',E::ts('Get event data') , array(
AbstractAction::DATA_RETRIEVAL_TAG,
));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment