Skip to content

Commit ced2d7b

Browse files
committed
Patterns update
* Pass details to pattern's script/code * Formatting update
1 parent 2a481d5 commit ced2d7b

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

Diff for: lib/context.inc.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function context_clear($user_id=0)
9696
* @param mixed $history History (default '')
9797
* @return void
9898
*/
99-
function context_activate($id, $no_action = 0, $history = '', $user_id = 0)
99+
function context_activate($id, $no_action = 0, $history = '', $user_id = 0, $details = 0)
100100
{
101101
if (!$user_id) {
102102
$user_id = context_getuser();
@@ -129,7 +129,7 @@ function context_activate($id, $no_action = 0, $history = '', $user_id = 0)
129129
{
130130
include_once(DIR_MODULES . 'patterns/patterns.class.php');
131131
$pt = new patterns();
132-
$pt->runPatternAction((int)$context['ID']);
132+
$pt->runPatternAction((int)$context['ID'], array(), '', $details);
133133
}
134134
}
135135
else

Diff for: modules/patterns/patterns.class.php

+30-39
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ function __construct()
3838
function saveParams($data = 0)
3939
{
4040
$p = array();
41-
if (IsSet($this->id)) {
41+
if (isset($this->id)) {
4242
$p["id"] = $this->id;
4343
}
44-
if (IsSet($this->view_mode)) {
44+
if (isset($this->view_mode)) {
4545
$p["view_mode"] = $this->view_mode;
4646
}
47-
if (IsSet($this->edit_mode)) {
47+
if (isset($this->edit_mode)) {
4848
$p["edit_mode"] = $this->edit_mode;
4949
}
50-
if (IsSet($this->tab)) {
50+
if (isset($this->tab)) {
5151
$p["tab"] = $this->tab;
5252
}
5353
return parent::saveParams($p);
@@ -100,18 +100,18 @@ function run()
100100
} else {
101101
$this->usual($out);
102102
}
103-
if (IsSet($this->owner->action)) {
103+
if (isset($this->owner->action)) {
104104
$out['PARENT_ACTION'] = $this->owner->action;
105105
}
106-
if (IsSet($this->owner->name)) {
106+
if (isset($this->owner->name)) {
107107
$out['PARENT_NAME'] = $this->owner->name;
108108
}
109109
$out['VIEW_MODE'] = $this->view_mode;
110110
$out['EDIT_MODE'] = $this->edit_mode;
111111
$out['MODE'] = $this->mode;
112112
$out['ACTION'] = $this->action;
113113
$out['TAB'] = $this->tab;
114-
if (IsSet($this->script_id)) {
114+
if (isset($this->script_id)) {
115115
$out['IS_SET_SCRIPT_ID'] = 1;
116116
}
117117
if ($this->single_rec) {
@@ -250,18 +250,18 @@ function edit_patterns(&$out, $id)
250250
*
251251
* @access public
252252
*/
253-
function checkAllPatterns($from_user_id = 0)
253+
function checkAllPatterns($from_user_id = 0, $details = 0)
254254
{
255255
$current_context = context_getcurrent($from_user_id);
256256
//DebMes("current context:".$current_context);
257257
if ($from_user_id && preg_match('/^ext(\d+)/', $current_context, $m)) {
258-
$res = $this->checkExtPatterns($m[1],$from_user_id);
258+
$res = $this->checkExtPatterns($m[1], $from_user_id);
259259
} else {
260260
$patterns = SQLSelect("SELECT * FROM patterns WHERE 1 AND PARENT_ID='" . (int)$current_context . "' AND PATTERN_TYPE=0 ORDER BY PRIORITY DESC, TITLE");
261261
$total = count($patterns);
262262
$res = 0;
263263
for ($i = 0; $i < $total; $i++) {
264-
$matched = $this->checkPattern($patterns[$i]['ID'], $from_user_id);
264+
$matched = $this->checkPattern($patterns[$i]['ID'], $from_user_id, $details);
265265
if ($matched) {
266266
$res = 1;
267267
if ($patterns[$i]['IS_LAST']) {
@@ -313,7 +313,7 @@ function getAvailableActions()
313313
}
314314

315315

316-
function checkExtPatterns($ext_context_id, $user_id=0)
316+
function checkExtPatterns($ext_context_id, $user_id = 0)
317317
{
318318

319319
$message = SQLSelectOne("SELECT MESSAGE FROM shouts ORDER BY ID DESC LIMIT 1");
@@ -400,7 +400,7 @@ function checkExtPatterns($ext_context_id, $user_id=0)
400400
playMedia($data['MEDIA_URL']);
401401
}
402402

403-
context_activate_ext($data['NEW_CONTEXT'], (int)$data['TIMEOUT'], $data['TIMEOUT_CODE'], (int)$data['TIMEOUT_CONTEXT_ID'],$user_id);
403+
context_activate_ext($data['NEW_CONTEXT'], (int)$data['TIMEOUT'], $data['TIMEOUT_CODE'], (int)$data['TIMEOUT_CONTEXT_ID'], $user_id);
404404

405405
return $data['MATCHED_CONTEXT'];
406406

@@ -476,14 +476,17 @@ function getConnectDetails()
476476
}
477477

478478

479-
function runPatternAction($id, $matches = array(), $original = '', $from_user_id = 0)
479+
function runPatternAction($id, $matches = array(), $original = '', $from_user_id = 0, $details = 0)
480480
{
481481
$rec = SQLSelectOne("SELECT * FROM patterns WHERE ID='" . (int)$id . "'");
482482

483-
//global $noPatternMode;
484-
//$noPatternMode=1;
485483
if ($rec['SCRIPT_ID']) {
486-
runScriptSafe($rec['SCRIPT_ID'], $matches);
484+
if (is_array($details)) {
485+
$details['MATCHES'] = $matches;
486+
runScriptSafe($rec['SCRIPT_ID'], $details);
487+
} else {
488+
runScriptSafe($rec['SCRIPT_ID'], $matches);
489+
}
487490
} elseif ($rec['SCRIPT']) {
488491

489492
try {
@@ -496,7 +499,6 @@ function runPatternAction($id, $matches = array(), $original = '', $from_user_id
496499
$bases[$i] = $form_bases[0];
497500
}
498501
}
499-
500502
$code = $rec['SCRIPT'];
501503
$success = eval($code);
502504
if ($success === false) {
@@ -509,11 +511,9 @@ function runPatternAction($id, $matches = array(), $original = '', $from_user_id
509511
}
510512

511513
}
512-
//$noPatternMode=0;
513-
514514
}
515515

516-
function runPatternExitAction($id, $script_exit)
516+
function runPatternExitAction($id, $script_exit, $details = 0)
517517
{
518518

519519
if ($script_exit) {
@@ -587,13 +587,10 @@ function generate_combinations(array $data, array &$all = array(), array $group
587587
*
588588
* @access public
589589
*/
590-
function checkPattern($id, $from_user_id = 0)
590+
function checkPattern($id, $from_user_id = 0, $details = 0)
591591
{
592-
global $session;
593592
global $pattern_matched;
594593

595-
596-
$this_pattern_matched = 0;
597594
$condition_matched = 0;
598595

599596
if (!checkAccess('pattern', $id)) return 0;
@@ -643,7 +640,7 @@ function checkPattern($id, $from_user_id = 0)
643640
SQLUpdate('patterns', $rec);
644641

645642
if ($rec['SCRIPT_EXIT']) {
646-
$this->runPatternExitAction($rec['ID'], $rec['SCRIPT_EXIT']);
643+
$this->runPatternExitAction($rec['ID'], $rec['SCRIPT_EXIT'], $details);
647644
}
648645
//to-do: state exit script
649646

@@ -758,23 +755,17 @@ function checkPattern($id, $from_user_id = 0)
758755
$is_common = (int)$parent_rec['IS_COMMON_CONTEXT'];
759756
}
760757

761-
/*
762-
if (context_getcurrent()) {
763-
$history = context_get_history() . ' ' . $history;
764-
}
765-
*/
766-
767758
if ($rec['IS_CONTEXT']) {
768-
context_activate($rec['ID'], 1, $history,$from_user_id);
759+
context_activate($rec['ID'], 1, $history, $from_user_id, $details);
769760
} elseif ($rec['MATCHED_CONTEXT_ID']) {
770-
context_activate($rec['MATCHED_CONTEXT_ID'], 0, $history,$from_user_id);
761+
context_activate($rec['MATCHED_CONTEXT_ID'], 0, $history, $from_user_id, $details);
771762
} elseif (!$is_common) {
772-
context_activate(0,0,'',$from_user_id);
763+
context_activate(0, 0, '', $from_user_id, $details);
773764
}
774765

775766
$rec['LOG'] = date('Y-m-d H:i:s') . ' Pattern matched' . "\n" . $rec['LOG'];
776-
if (strlen($rec['LOG'])>65000) {
777-
$rec['LOG'] = substr($rec['LOG'],0,65000);
767+
if (strlen($rec['LOG']) > 65000) {
768+
$rec['LOG'] = substr($rec['LOG'], 0, 65000);
778769
}
779770
$rec['EXECUTED'] = time();
780771
SQLUpdate('patterns', $rec);
@@ -787,7 +778,7 @@ function checkPattern($id, $from_user_id = 0)
787778
$sub_patterns = SQLSelect("SELECT ID, IS_LAST FROM patterns WHERE PARENT_ID='" . $rec['ID'] . "' ORDER BY PRIORITY DESC, TITLE");
788779
$total = count($sub_patterns);
789780
for ($i = 0; $i < $total; $i++) {
790-
if ($this->checkPattern($sub_patterns[$i]['ID'], $from_user_id)) {
781+
if ($this->checkPattern($sub_patterns[$i]['ID'], $from_user_id, $details)) {
791782
$sub_patterns_matched = 1;
792783
if ($sub_patterns[$i]['IS_LAST']) {
793784
break;
@@ -797,7 +788,7 @@ function checkPattern($id, $from_user_id = 0)
797788
}
798789

799790
if (!$sub_patterns_matched) {
800-
$this->runPatternAction($rec['ID'], $matches, $history, $from_user_id);
791+
$this->runPatternAction($rec['ID'], $matches, $history, $from_user_id, $details);
801792
}
802793

803794
if ($rec['ONETIME']) {
@@ -848,7 +839,7 @@ function processSubscription($event, &$details)
848839
global $context_user_id;
849840
$context_user_id = $member_id;
850841

851-
$res = $this->checkAllPatterns($member_id);
842+
$res = $this->checkAllPatterns($member_id, $details);
852843
if ($event == 'COMMAND' && $res) {
853844
$details['BREAK'] = true;
854845
$details['PROCESSED'] = true;

0 commit comments

Comments
 (0)