Skip to content

Commit

Permalink
WIP actions hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ammopt committed Aug 5, 2024
1 parent 3e89bf1 commit f753ba9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
4 changes: 4 additions & 0 deletions code/web/cron/updateCommunityEngagementMilestonesProgress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
require_once __DIR__ . '/../bootstrap.php';

#DO CODE HERE
7 changes: 6 additions & 1 deletion code/web/sys/DB/DataObject.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

require_once ROOT_DIR . '/sys/action-hooks.php';
/**
* Class DataObject
*
Expand Down Expand Up @@ -310,6 +310,8 @@ public function insert($context = '') {
$serializedFields = $this->getSerializedFieldNames();
$compressedFields = $this->getCompressedColumnNames();

do_action('before_object_insert', get_object_vars($this));

$properties = get_object_vars($this);
$propertyNames = '';
$propertyValues = '';
Expand Down Expand Up @@ -408,6 +410,9 @@ public function insert($context = '') {
}
}

#Call hook for post insert here
do_action('after_object_insert', $this);

return $response;
}

Expand Down
80 changes: 80 additions & 0 deletions code/web/sys/action-hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

$hook_actions = array();

/**
* Attach (or remove) multiple callbacks to an action and trigger those callbacks when that action is called.
*
* @param string $hook name
* @param string $action name
* @param mixed $callback the method or function to call - FALSE to remove all callbacks for action
*/

function add_action($hook_name, $action_name, $callback = NULL){
global $hook_actions;

if($callback)
{
$hook_actions[$hook_name][$action_name] = $callback;
}
else
{
unset($hook_actions[$hook_name][$action_name]);
}
}

/**
* Executes all callback functions attached to a given action.
*
* @param string $action The name of the action to execute.
* @param mixed $value (optional) The value to pass to each callback function. Defaults to NULL.
* @return void
*/

function do_action($hook_name, $value = NULL){
global $hook_actions;

if(isset($hook_actions[$hook_name])) // Fire a callback
{
foreach($hook_actions[$hook_name] as $function)
{
call_user_func($function, $value);
}
}
}

add_action('before_object_insert', 'before_checkout_insert', function($value){

$tableName = 'user_checkout';

# Bail if not the table we want
if($value['__table'] != $tableName) return;

# Bail if the newly created checkout already exists
$checkOuts = new CheckOut();
$checkOuts->query("SELECT sourceId from {$tableName} WHERE sourceId = {$value['sourceId']}");
if ($checkOuts->getNumResults() > 0) {
return;
}

# Add entry to ce_milestone_goal_actions

});

// Example of action hook
// add_action('after_object_insert', 'after_checkout_insert', function($value){

// $tableName = 'user_checkout';

// # Bail if not the table we want
// if($value->__table != $tableName) return;

// # Bail if the newly created checkout already exists
// $checkOuts = new CheckOut();
// $checkOuts->query("SELECT sourceId from {$tableName} WHERE sourceId = {$value->sourceId}");
// if ($checkOuts->getNumResults() > 0) {
// return;
// }


// });

0 comments on commit f753ba9

Please sign in to comment.