Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…asks into merge-calltask

* 'dev_14' of https://github.com/systopia/de.systopia.sqltasks:
  implementing #14 (WIP)
  implementing #14 (WIP)
  • Loading branch information
pfigel committed Jul 4, 2018
2 parents 5803335 + dc32b16 commit c457276
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 0 deletions.
1 change: 1 addition & 0 deletions CRM/Sqltasks/Action.php
Expand Up @@ -195,6 +195,7 @@ public static function getAllActions($task) {
if (function_exists('segmentation_civicrm_install')) {
$actions[] = new CRM_Sqltasks_Action_SegmentationExport($task);
}
$actions[] = new CRM_Sqltasks_Action_CallTask($task);
$actions[] = new CRM_Sqltasks_Action_ResultHandler($task, 'success', E::ts('Success Handler'));
$actions[] = new CRM_Sqltasks_Action_ResultHandler($task, 'error', E::ts('Error Handler'));
return $actions;
Expand Down
133 changes: 133 additions & 0 deletions CRM/Sqltasks/Action/CallTask.php
@@ -0,0 +1,133 @@
<?php
/*-------------------------------------------------------+
| SYSTOPIA SQL TASKS EXTENSION |
| Copyright (C) 2018 SYSTOPIA |
| Author: B. Endres (endres@systopia.de) |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/

use CRM_Sqltasks_ExtensionUtil as E;

/**
* This action will "simply" call another task
*
*/
class CRM_Sqltasks_Action_CallTask extends CRM_Sqltasks_Action {

/**
* Get identifier string
*/
public function getID() {
return 'task';
}

/**
* Get a human readable name
*/
public function getName() {
return E::ts('Run SQL Task(s)');
}

/**
* Build the configuration UI
*/
public function buildForm(&$form) {
parent::buildForm($form);

$form->add(
'select',
$this->getID() . '_tasks',
E::ts('SQL Tasks'),
$this->getTaskList(),
FALSE,
array('class' => 'crm-select2 huge', 'multiple' => 'multiple')
);

$form->add(
'select',
$this->getID() . '_categories',
E::ts('SQL Task Categories'),
$this->getTaskCategoryList(),
FALSE,
array('class' => 'crm-select2 huge', 'multiple' => 'multiple')
);
}


/**
* RUN this action
*/
public function execute() {
$this->resetHasExecuted();

$tasks = $this->getConfigValue('tasks');
$categories = $this->getConfigValue('categories');
if (empty($tasks) && empty($categories)) {
return;
}

// generate query for task selection
$query = "SELECT * FROM `civicrm_sqltasks` WHERE enabled=1 AND ";
$or_clauses = array();
if (!empty($tasks)) {
$or_clauses[] = '`id` IN (' . implode(',', $tasks) . ')';
}
if (!empty($categories)) {
$escaped_categories = array();
foreach ($categories as $category) {
$escaped_categories[] = "'" . mysql_escape_string($category) . "'";
}
$or_clauses[] = '`category` IN (' . implode(',', $escaped_categories) . ')';
}
$query .= '((' . implode(') OR (', $or_clauses). '))';
$query .= ' ORDER BY weight ASC';
$tasks2run = CRM_Sqltasks_Task::getTasks($query);

foreach ($tasks2run as $task) {
if ($task->getID() == $this->task->getID()) {
continue;
}
// all good: execute!
$task->execute();
$this->log("Executed task '" . $task->getAttribute('name') . "' [" . $task->getID() . ']');
}
}


/**
* Get a list of all SQL Tasks
*/
protected function getTaskList() {
$task_options = array();
$task_list = CRM_Sqltasks_Task::getExecutionTaskList();

// make sure this one is not in it
foreach ($task_list as $task) {
$task_id = $task->getID();
if ($task_id != $this->task->getID()) {
$task_options[$task_id] = "[{$task_id}] " . $task->getAttribute('name');
}
}

return $task_options;
}

/**
* Get a list of all SQL Task categories
*/
protected function getTaskCategoryList() {
$category_options = array();
$category_query = CRM_Core_DAO::executeQuery("SELECT DISTINCT(category) AS category FROM `civicrm_sqltasks`;");
while ($category_query->fetch()) {
$category_options[$category_query->category] = $category_query->category;
}
return $category_options;
}
}
27 changes: 27 additions & 0 deletions templates/CRM/Sqltasks/Action/CallTask.hlp
@@ -0,0 +1,27 @@
{*-------------------------------------------------------+
| SYSTOPIA SQL TASKS EXTENSION |
| Copyright (C) 2018 SYSTOPIA |
| Author: B. Endres (endres@systopia.de) |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+-------------------------------------------------------*}

{htxt id='id-task-tasks'}
<p>{ts domain="de.systopia.sqltasks"}You can select any tasks (except this one) to be run as an action of this task.{/ts}</p>
<p>{ts domain="de.systopia.sqltasks"}Only enabled tasks will be run, in the order defined by the task manager, <i>not</i> this list.{/ts}</p>
<p>{ts domain="de.systopia.sqltasks"}The tasks selected here will be run <i>together</i> with the ones selected by category below.{/ts}</p>
<p>{ts domain="de.systopia.sqltasks"}<strong>Be <id>very</id> careful with this! You can easily create circular execution patterns that <i>will</i> break the system.</strong>{/ts}</p>
{/htxt}

{htxt id='id-task-categories'}
<p>{ts domain="de.systopia.sqltasks"}You can select any number of task categories to be run as an action of this task.{/ts}</p>
<p>{ts domain="de.systopia.sqltasks"}Only enabled tasks of the selected categories will be run, in the order defined by the task manager, <i>not</i> this list.{/ts}</p>
<p>{ts domain="de.systopia.sqltasks"}The tasks selected here will be run <i>together</i> with the ones selected individually above.{/ts}</p>
<p>{ts domain="de.systopia.sqltasks"}<strong>Be <id>very</id> careful with this! You can easily create circular execution patterns that <i>will</i> break the system.</strong>{/ts}</p>
{/htxt}
27 changes: 27 additions & 0 deletions templates/CRM/Sqltasks/Action/CallTask.tpl
@@ -0,0 +1,27 @@
{*-------------------------------------------------------+
| SYSTOPIA SQL TASKS EXTENSION |
| Copyright (C) 2018 SYSTOPIA |
| Author: B. Endres (endres@systopia.de) |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+-------------------------------------------------------*}

<div class="sql-tasks">
<div class="crm-section">
<div class="label">{$form.task_tasks.label}&nbsp;<a onclick='CRM.help("{ts domain="de.systopia.sqltasks"}Tasks{/ts}", {literal}{"id":"id-task-tasks","file":"CRM\/Sqltasks\/Action\/CallTask"}{/literal}); return false;' href="#" title="{ts domain="de.systopia.sqltasks"}Help{/ts}" class="helpicon">&nbsp;</a></div>
<div class="content">{$form.task_tasks.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.task_categories.label}&nbsp;<a onclick='CRM.help("{ts domain="de.systopia.sqltasks"}Categories{/ts}", {literal}{"id":"id-task-categories","file":"CRM\/Sqltasks\/Action\/CallTask"}{/literal}); return false;' href="#" title="{ts domain="de.systopia.sqltasks"}Help{/ts}" class="helpicon">&nbsp;</a></div>
<div class="content">{$form.task_categories.html}</div>
<div class="clear"></div>
</div>
</div>

0 comments on commit c457276

Please sign in to comment.