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 6, 2024
1 parent 3e89bf1 commit f3eef0b
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
4 changes: 4 additions & 0 deletions code/web/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@
//Modules are not installed yet
}

if (array_key_exists('Community', $enabledModules)) {
require_once ROOT_DIR . '/sys/Community/action-hooks.php';
}

$timer->logTime("Basic Initialization");
loadLibraryAndLocation();

Expand Down
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
31 changes: 31 additions & 0 deletions code/web/sys/Community/action-hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

require_once ROOT_DIR . '/sys/Community/Milestone.php';

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;
}

$milestone = new Milestone();
$milestone->find();
while ($milestone->fetch()) {
$milestone->setProperty('reward', $milestone->reward+1, null);
$milestone::update();
}

return;
// var_dump('its new biaaatch');
// die;
# Add entry to ce_milestone_goal_actions

});
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/Hooks/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/Hooks/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 f3eef0b

Please sign in to comment.