-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessItems.php
561 lines (499 loc) · 21.8 KB
/
processItems.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
//INCLUDES
require_once('headerDB.inc.php');
$updateGlobals=array();
$html=false; // indicates if we are outputting html
$updateGlobals['captureOutput']=(isset($_POST['output']) && $_POST['output']==='xml');
if ($updateGlobals['captureOutput']) {
ob_start();
}
// get core variables first
$values=array(); // ensures that this is a global variable
$values['itemId'] = isset($_REQUEST['itemId'])?(int) $_REQUEST['itemId']:null;
$values['type'] = (isset($_REQUEST['type']))?$_REQUEST['type']:null;
$values['pId'] = (isset($_REQUEST['pId']))?$_REQUEST['pId']:null;
$action = $_REQUEST['action'];
//$updateGlobals['referrer'] = $_REQUEST['referrer'];
if (isset($_REQUEST['matrix'])) $updateGlobals['referrer'] = $_SERVER['HTTP_REFERER']; //'matrix.php';
$updateGlobals['multi'] = (isset($_POST['multi']) && $_POST['multi']==='y');
$updateGlobals['parents'] = (isset($_POST['parentId']))?$_POST['parentId']:array();
if (!is_array($updateGlobals['parents'])) $updateGlobals['parents']=array($updateGlobals['parents']);
if (isset($_POST['wasNAonEntry'])) { // toggling next action status on several items
$updateGlobals['wasNAonEntry'] = explode(' ',$_POST['wasNAonEntry']);
$updateGlobals['isNA']=array();
if (isset($_POST['isNAs'])) $updateGlobals['isNA']=$_POST['isNAs'];
}
if (isset($_POST['isMarked'])) { // doing a specific action on several items (currently, the only option is to complete them)
$updateGlobals['isMarked']=array();
$updateGlobals['isMarked']=array_unique($_POST['isMarked']); // remove duplicates
}
// some debugging - if debug is set to halt, dump all the variables we've got
if ($config['debug'] & _GTD_DEBUG) {
include 'headerHtml.inc.php';
echo "</head><body><div id='container'>\n";
$html=true;
// debugging text - simply dump the variables, and quit, without processing anything
literaldump('$_GET');
literaldump('$_POST');
literaldump('$_SESSION');
literaldump('$action');
literaldump('$config');
literaldump('$values');
literaldump('$updateGlobals');
if (isset($updateGlobals['isNA'])) {
echo '<hr /><pre>array_diff(wasNAonEntry,isNA)';
print_r(array_diff($updateGlobals['wasNAonEntry'],$updateGlobals['isNA']));
echo '<br /><hr />array_diff(isNA,wasNAonEntry)';
print_r(array_diff($updateGlobals['isNA'],$updateGlobals['wasNAonEntry']));
echo '</pre>';
}
} // END OF debugging text
$title='';
if ($updateGlobals['multi']) {
// recursively do actions, looping over items
if (isset($updateGlobals['wasNAonEntry']) && isset($updateGlobals['isNA'])) { // toggling next action status on several items
foreach (array_diff($updateGlobals['wasNAonEntry'],$updateGlobals['isNA']) as $values['itemId']) if ($values['itemId']) doAction('removeNA');
foreach (array_diff($updateGlobals['isNA'],$updateGlobals['wasNAonEntry']) as $values['itemId']) if ($values['itemId']) doAction('makeNA');
}
if (isset($updateGlobals['isMarked'])) { // doing a specific action on several items (currently, the only option is to complete them)
foreach ($updateGlobals['isMarked'] as $nextItem) {
$values=array('itemId'=>$nextItem); // reset the $values array each time, so that it only contains itemId
doAction($action);
}
}
} else {
if (isset($_POST['delete']) && $_POST['delete']==='y') $action='delete'; // override item-update if we are simply deleting
doAction($action);
}
nextPage();
if ($html)
include_once('footer.php');
else
echo '</head></html>';
return;
/*========================================================================================
main program finished - utility functions from here, below
========================================================================================*/
function doAction($localAction) { // do the current action on the current item; returns TRUE if succeeded, else returns FALSE
global $config,$values,$updateGlobals,$title;
if ($values['itemId']) {
$result=query('getitembrief',$config,$values);
$title=($result)?$result[0]['title']:'title unknown';
} elseif (isset($_POST['title'])) {
$title=$_POST['title'];
} else {
$title='';
}
if ($title=='') $title='item '.$values['itemId'];
if ($config['debug'] & _GTD_DEBUG) echo "<p><b>Action here is: $localAction item {$values['itemId']}</b></p>";
if ($config['debug'] & _GTD_FREEZEDB) return TRUE;
switch ($localAction) {
case 'makeNA':
makeNextAction();
$msg="'$title' is now a next action";
$updateGlobals['referrer'] = str_replace('http://localhost/fluid/', '', $_SERVER['HTTP_REFERER']);
break;
case 'removeNA':
removeNextAction();
$msg="'$title' is no longer a next action";
$updateGlobals['referrer'] = str_replace('http://localhost/fluid/', '', $_SERVER['HTTP_REFERER']);
break;
case 'deleteparlookup':
deleteparlookup();
$updateGlobals['referrer'] = 'itemReport.php?itemId=' . $_GET['itemId'];
$msg="Severed";
$updateGlobals['referrer']="{$updateGlobals['referrer']}";
break;
case 'parentupdate':
if (!isset($_POST['type'])) $_POST = $_GET;
if (!isset($_POST['type'])) die;
$sort['getitems'] = 'NULL';
$values['filterquery'] = " WHERE ia.type = '" . $_POST['type'] . "' ";
if (isset($_POST['categoryId'])) $values['filterquery'] .= " AND ia.categoryId = '" . $_POST['categoryId'] . "'";
$result = query("getitems",$config,$values,$sort);
// echo '<pre>';var_dump($result);die;
// echo '<pre>';var_dump($values['filterquery']);die;
// echo '<pre>';var_dump($_POST);die;
$values['parentId'] = $values['pId'];
# iterate all items of type defined (includes those not linked to vision if defined)
foreach ($result as $res) {
$values['itemId'] = $res['itemId'];
# check if we have a vision defined
if ($_POST['visId'] > 0) {
$valuesV = array();
$valuesV['parentId'] = $_POST['visId'];
$valuesV['itemId'] = $values['itemId'];
$resultV = query("checklookup",$config,$valuesV,$sort);
# if the item is linked to vision then unlink
if (count($resultV) > 0 && is_array($resultV)) {
deleteparlookup();
}
}
}
$cnt=0;
# addedItem call only used by childrenUpdate.php script, called from matrix
# assumes $_POST['visId'] is set
# add all items to vision
if (isset($_POST['addedItem'])) {
foreach ($_POST['addedItem'] as $id) {
$values['newitemId'] = $id;
query("newparent",$config,$values);
$cnt++;
}
}
// caution the logic here is finnicky to change
if (isset($_POST['visId'])) {
$values['parentId'] = $_POST['visId'];
if (isset($_POST['addedItem'])) {
foreach ($_POST['addedItem'] as $id) {
$values['newitemId'] = $id;
query("newparent",$config,$values);
}
} else {
$values['newitemId'] = $_POST['itemId'];
query("newparent",$config,$values);
$updateGlobals['referrer'] = 'itemReport.php?itemId=' . $_POST['itemId'];
}
}
$msg = "$cnt child";
if ($cnt!==1) $msg .= 'ren';
//$_SESSION['message'][]=$msg;
if (isset($updateGlobals['referrer'])) $updateGlobals['referrer']="{$updateGlobals['referrer']}";
break;
case 'changeType':
changeType();
$newtype=getTypes($values['type']);
$msg="Updated $newtype";
// $updateGlobals['referrer'] = str_replace('http://localhost/fluid/', '', $_SERVER['HTTP_REFERER']);
// $updateGlobals['referrer']="item.php?itemId={$values['itemId']}&referrer={$updateGlobals['referrer']}";
if (isset($updateGlobals['referrer'])) {
$updateGlobals['referrer']="{$updateGlobals['referrer']}";
} else {
$updateGlobals['referrer']="";
}
break;
case 'create':
retrieveFormVars();
createItem();
$msg="Created item: '$title'";
break;
case 'complete':
completeItem();
$msg="Completed '$title'";
$updateGlobals['referrer'] = str_replace('http://localhost/fluid/', '', $_SERVER['HTTP_REFERER']);
break;
case 'fullUpdate':
retrieveFormVars();
updateItem();
$msg="Updated '$title'";
break;
case 'delete':
deleteItem();
$msg="Deleted '$title'";
break;
case 'createbasic': // not in use yet. added for future use, when only title and type are set.
createItemQuickly();
$msg="Created item: '$title'";
break;
default: // failed to identify which action we should be taking, so quit
return FALSE;
}
$_SESSION['message'][] = $msg;
return TRUE; // we have successfully carried out some action
}
/* ===========================================================================================
primary action functions
================================= */
function deleteItem() { // delete all references to a specific item
global $config,$values;
query("deleteitemstatus",$config,$values);
query("deleteitemattributes",$config,$values);
query("deleteitem",$config,$values);
query("deletelookup",$config,$values);
query("deletelookupparents",$config,$values);
removeNextAction();
query("deletenextactionparents",$config,$values);
$values['itemType'] = $values['type'];
query("deletequalities",$config,$values);
}
function createItem() { // create an item and its parent-child relationships
global $config,$values,$updateGlobals,$title;
//Insert new records
$result = query("newitem",$config,$values);
$values['newitemId'] = $GLOBALS['lastinsertid'];
$result = query("newitemattributes",$config,$values);
$result = query("newitemstatus",$config,$values);
setParents('new');
$title=$values['title'];
}
function createItemQuickly() {// create an item when we only know its type and title - not yet in use - TOFIX still to check
global $config,$values,$updateGlobals,$title;
//Insert new records
$result = query("newitem",$config,$values);
$values['newitemId'] = $GLOBALS['lastinsertid'];
setParents('new');
$title=$values['title'];
}
function updateItem() { // update all the values for the current item
global $config,$values,$updateGlobals,$title;
query("deletelookup",$config,$values);
removeNextAction();
query("updateitemattributes",$config,$values);
query("updateitem",$config,$values);
if ($values['type'] === $values['oldtype'])
setParents('update');
else
// changing item type - sever child links
query("deletelookupparents",$config,$values);
if ($values['dateCompleted']==='NULL')
query('completeitem',$config,$values);
else
completeItem();
$title=$values['title'];
}
function completeItem() { // mark an item as completed, and recur if required
global $config,$values;
if (!isset($values['dateCompleted'])) $values['dateCompleted']="'".date('Y-m-d')."'";
if (!isset($values['repeat']) || !isset($values['old']['dateCompleted'])) {
$testrow = query("testitemrepeat",$config,$values);
if (!isset($values['repeat'])) $values['repeat']=$testrow[0]['repeat'];
if (!isset($values['old'])) $values['old']=array();
$values['old']['dateCompleted']=$testrow[0]['dateCompleted'];
}
if ($values['repeat'] && empty($values['old']['dateCompleted'])) recurItem(); else makeComplete();
}
function makeNextAction() { // mark the current item as a next action
global $config,$values;
$thisquery='updatenextaction';
$parentresult = query("lookupparent",$config,$values);
if ($parentresult) {
foreach ($parentresult as $parent) {
$values['parentId']=$parent['parentId'];
query($thisquery,$config,$values);
}
} else {
$values['parentId']=0;
query($thisquery,$config,$values);
}
query("touchitem",$config,$values);
}
function removeNextAction() { // remove the next action reference for the current item
global $config,$values;
query("deletenextaction",$config,$values);
query("touchitem",$config,$values); // TOFIX - doing this too often - probably move all occurrences into function doAction
}
function changeType() {
global $config,$values;
$values['isSomeday']=isset($_REQUEST['isSomeday'])?$_REQUEST['isSomeday']:'n';
query("updateitemtype",$config,$values);
if ($values['type'] == 'v') {
$values['itemType'] = $_REQUEST['oldType'];
query("deletequalities",$config,$values);
query("deletelookup",$config,$values);
}
$values['oldType']=$_REQUEST['oldType'];
query("updateitemtypequalities",$config,$values);
if (empty($_REQUEST['safe'])) {
query("deletelookup",$config,$values);
query("deletelookupparents",$config,$values);
removeNextAction();
query("deletenextactionparents",$config,$values);
}
}
function deleteparlookup() {
global $config,$values;
$values['itemId'] = $values['itemId'];
$values['parentId'] = $values['pId'];
query("deleteparlookup",$config,$values);
}
/* ===========================================================================================
utility functions for the primary actions
=========================================== */
function retrieveFormVars() {
global $config,$values;
// TOFIX - what we should really do here is retrieve the item, and then over-write with $_POST variables if and only if they are available
// although we'd need to check that unticked checkboxes came through ok - that could be tricky
// key variables
if (!empty($_POST['oldtype'])) $values['oldtype'] = $_POST['oldtype'];
foreach (
array('type'=>'i','title'=>'untitled','description'=>''
,'premiseA'=>'','premiseB'=>'','conclusion'=>'','behaviour'=>'','standard'=>'','conditions'=>'','metaphor'=>'','hyperlink'=>'','categoryId'=>0,'contextId'=>0
,'timeframeId'=>0)
as $field=>$default)
$values[$field] = (empty($_POST[$field])) ? $default : $_POST[$field];
// binary yes/no
$values['nextAction'] = (isset($_POST['nextAction']) && $_POST['nextAction']==="y")?'y':'n';
$values['isSomeday'] = (isset($_POST['isSomeday']) && $_POST['isSomeday']==='y')?'y':'n';
$values['suppress'] = (isset($_POST['suppress']) && $_POST['suppress']==='y')?'y':'n';
$values['suppressIsDeadline'] = (isset($_POST['suppressIsDeadline']) && $_POST['suppressIsDeadline']==='y')?'y':'n';
$values['delete'] = (isset($_POST['delete']) && $_POST['delete']==='y')?'y':NULL;
// integers
$values['suppressUntil'] = empty($_POST['suppressUntil'])?0:(int) $_POST['suppressUntil'];
$values['repeat'] = empty($_POST['repeat'])?0:(int) $_POST['repeat'];
// dates
$values['dateCompleted'] = (empty($_POST['dateCompleted'])) ? "NULL" : "'{$_POST['dateCompleted']}'";
$values['deadline'] = (empty($_POST['deadline'])) ? "NULL" : "'{$_POST['deadline']}'";
if ($config['debug'] & _GTD_DEBUG) {
echo '<hr /><pre><b>retrieved form vars</b><br />';
literaldump('$values');
echo '</pre>';
}
}
function getItemCopy() { // retrieve all the values for the current item, and store in the $values array
global $config,$values,$updateGlobals;
$copyresult = query("selectitem",$config,$values,array());
foreach ($copyresult[0] as $key=>$thisvalue) $values[$key]=$thisvalue;
// now get parents
$result=query("lookupparent",$config,$values,array());
$updateGlobals['parents']=array();
if (is_array($result))
foreach ($result as $parent)
$updateGlobals['parents'][]=$parent['parentId'];
if ($config['debug'] & _GTD_DEBUG) {
echo '<pre>Retrieved record for copying: </pre>';
literaldump('$values');
echo '<pre>Parents:',print_r($updateGlobals['parents'],true),'</pre>';
}
}
function setParents($new) {
global $config,$values,$updateGlobals;
if($config['debug'] & _GTD_DEBUG) echo '<pre>',print_r($updateGlobals['parents'],true),'</pre>';
$markedna=false;
foreach ($updateGlobals['parents'] as $values['parentId']) if ($values['parentId']) {
$result = query($new."parent",$config,$values);
if(!empty($values['nextAction']) && $values['nextAction']==='y') {
$result = query($new."nextaction",$config,$values);
$markedna=true;
}
}
if (!$markedna && $values['nextAction']==='y') {
$values['parentId']=0;
$result = query($new."nextaction",$config,$values);
}
}
function recurItem() { // mark a recurring item completed, and set up the recurrence
global $config,$values,$updateGlobals;
// calculate date to recur to, based on: date completed + number of days between recurrences
$dateArray=explode("-", str_replace("'",'',$values['dateCompleted']));
$unixdateCompleted=mktime(12,0,0,$dateArray[1],$dateArray[2],$dateArray[0]);
$nextdue=strtotime("+".$values['repeat']." day",$unixdateCompleted);
if ($config['storeRecurrences']) {
makeComplete();
getItemCopy();
if (isset($updateGlobals['isNA']) && in_array($values['itemId'],$updateGlobals['isNA']))
$values['nextAction']='y';
}
$values['dateCompleted']="NULL";
$values['deadline']="'".gmdate("Y-m-d", $nextdue)."'";
if ($config['storeRecurrences'])
createItem();
else {
query("updatedeadline",$config,$values);
query("completeitem",$config,$values); // reset completed date to null, and touch the last modified date
}
}
function makeComplete() { // mark an action as completed, and removes next action marker for it
global $config,$values;
query("completeitem",$config,$values);
removeNextAction();
}
/* ===========================================================================================
general utility functions that don't modify the database
========================================================= */
function nextPage() { // set up the forwarding to the next page
global $config,$values,$updateGlobals,$action;
$t = (isset($values['oldtype']))?$values['oldtype']:((isset($values['type']))?$values['type']:null);
$key='afterCreate'.$t;
$id=(empty($values['newitemId']))?$values['itemId']:$values['newitemId'];
$nextURL='';
if (!empty($_POST['afterCreate'])) {
$tst=$_POST['afterCreate'];
$_SESSION[$key]=$_POST['afterCreate'];
}elseif (!empty($updateGlobals['referrer']))
$tst=$updateGlobals['referrer'];
else
$tst=$_SESSION[$key];
if ($action=='delete' && $tst=='item') $tst='list';
switch ($tst) {
case "another" :
$nextURL="item.php?type=$t";
if (!empty($updateGlobals['parents'])) {
$parentlist= (is_array($updateGlobals['parents']))
?implode(',',$updateGlobals['parents'])
:$updateGlobals['parents'];
if ($parentlist!='') $nextURL.="&parentId=$parentlist";
}
foreach ( array(
'categoryId'=>'categoryId','contextId'=>'contextId',
'timeframeId'=>'timeframeId','nextAction'=>'nextonly',
'suppress'=>'suppress','deadline'=>'deadline',
'isSomeday'=>'isSomeday','suppressUntil'=>'suppressUntil'
) as $key=>$cat )
if (!empty($values[$key]) && $values[$key]!='NULL') $nextURL.="&$cat=".str_replace("'","",$values[$key]);
break;
case 'child' :
$child=getChildType($values['type']);
$nextURL="item.php?parentId=$id&type={$child[0]}";
if ($child[0]==='a') $nextURL.='&nextonly=true';
foreach (array('categoryId','contextId','timeframeId') as $field)
if (!empty($values[$field])) $nextURL.="&$field=".$values[$field];
break;
case "item" :
$nextURL="itemReport.php?itemId=$id";
break;
case "list" :
$nextURL="listItems.php?type=$t";
if (!empty($values['isSomeday']) && $values['isSomeday']==='y') {
$nextURL.='&someday=true';
} else if (!empty($values['suppress']) && $values['suppress']==='y' and
time() < getTickleDate(str_replace("'",'',$values['deadline']),$values['suppressUntil'])
) {
$nextURL.='&tickler=true';
}
break;
case "parent" :
$nextURL=(count($updateGlobals['parents']))
?('itemReport.php?itemId='.$updateGlobals['parents'][0])
: $updateGlobals['referrer'];
break;
case "referrer":
$nextURL=(empty($updateGlobals['referrer']) )
? $_SESSION["lastfilter$t"]
: $updateGlobals['referrer'];
break;
default :
$nextURL=$tst;
break;
}
if ($config['debug'] & _GTD_DEBUG) {
echo '<pre>$referrer=',print_r($updateGlobals['referrer'],true),'<br />'
,"type={$values['type']}<br />"
,'session=',print_r($_SESSION,true),'<br />'
,'</pre>';
}
if ($nextURL=='') $nextURL="item.php?itemId=".$values['itemId'];
$_SESSION[$key]=$tst;
$nextURL=html_entity_decode($nextURL);
if ($updateGlobals['captureOutput']) {
$logtext=ob_get_contents();
ob_end_clean();
echo '<?xml version="1.0" ?',"><gtdphp><result>\n";
$outtext=$_SESSION['message'];
foreach ($outtext as $line)
echo "<line><![CDATA[$line]]></line>\n";
echo "</result><nextURL><![CDATA[$nextURL]]></nextURL>\n";
if (!empty($outtext)) echo "<log><![CDATA[$logtext]]></log>\n";
echo "</gtdphp>";
$_SESSION['message']=array();
exit;
} else nextScreen($nextURL);
}
function literaldump($varname) { // dump a variable name, and its contents
echo "<pre><b>$varname</b>=";
$tst="print_r((isset($varname))?($varname):(\$GLOBALS['".substr($varname,1)."']));return 1;";
if (eval($tst))
echo '</pre>';
else
echo "<br />Failed to display variable value: $tst <br />";
}
// php closing tag has been omitted deliberately, to avoid unwanted blank lines being sent to the browser