Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.33 KB | None | 0 0
  1. <?php
  2.  
  3. #region Intitialization, globals, constants, and support functions
  4. error_reporting(E_ALL);
  5. ini_set('display_errors', '1');
  6.  
  7. // DataTables PHP library
  8. require( "C:/wamp64/www/distLibs/phpLibs/DataTables-Editor/DataTables.php" );
  9.  
  10. // OTLogger PHP library
  11. require( "C:/wamp64/www/distLibs/phpLibs/OTLogger.php" );
  12.  
  13. use OTLogger\OTLogger;
  14.  
  15. // Alias Editor classes so they are easy to use
  16. use
  17. DataTables\Editor,
  18. DataTables\Editor\Field,
  19. DataTables\Editor\Format,
  20. DataTables\Editor\Mjoin,
  21. DataTables\Editor\Options,
  22. DataTables\Editor\Upload,
  23. DataTables\Editor\Validate;
  24.  
  25.  
  26. // Constants
  27. const ACTION_NONE = "0";
  28.  
  29. const ACTION_ADD = "1";
  30.  
  31. const ACTION_DELETE = "10";
  32. const ACTION_DELETE_ALL = "11";
  33. const ACTION_DELETE_ONLY = "12";
  34. const ACTION_DELETE_FORWARD = "13";
  35. const ACTION_DELETE_IGNORE = "14";
  36.  
  37. const ACTION_EDIT = "20";
  38. const ACTION_EDIT_ALL = "21";
  39. const ACTION_EDIT_ONLY = "22";
  40. const ACTION_EDIT_FORWARD = "23";
  41. const ACTION_EDIT_IGNORE = "24";
  42.  
  43. // Logging support
  44. $log = new OTLogger();
  45. #endregion
  46.  
  47. #region Support functions
  48. //--------------------------------------------------------------------------
  49. // Routine to insert new event(s)
  50. //--------------------------------------------------------------------------
  51. function doInsert($editor, $row, $useRFCID, $single, $log) {
  52. $save_rfcid = "";
  53.  
  54. $log->debug("doInsert", "doInsert Starting");
  55.  
  56. $log->debug("doInsert", "Incoming values -\n " . print_r($row, true));
  57.  
  58. //-----------------------------------------------------------------------------------
  59. // clean-up and normalize input values
  60. //-----------------------------------------------------------------------------------
  61. // create work array for insert
  62. $wrow = $row;
  63.  
  64. // clean out what isn't needed
  65. unset($wrow['DT_RowId']);
  66. unset($wrow['RID']);
  67. unset($wrow['whenHolder']);
  68. unset($wrow['locHolder']);
  69.  
  70. // now make sure RID is null so MySql will provide it
  71. $wrow += ['RID' => null];
  72.  
  73. // make sure the action code is clear
  74. $wrow['action'] = ACTION_NONE;
  75.  
  76. // add some empty columns
  77. $wrow += ['whenHold' => null];
  78. $wrow += ['locHold' => null];
  79.  
  80. // make the event id unique
  81. $wrow['id'] = uniqid("", true );
  82.  
  83. // Put the formatted start date and time into "whenHold" for date searches
  84. $wrow['whenHold'] = $wrow['start'];
  85.  
  86. // set start and end dates to proper format
  87. $wdate = strtotime($wrow['start']);
  88. $wrow['start'] = date('Y-m-d H:i:s', $wdate);
  89.  
  90. // handle an allDay event and set 'end' accordingly
  91. if($wrow['allDay'] == "1") {
  92. $wrow['end'] = null;
  93. } else {
  94. $wdate = strtotime($wrow['end']);
  95. $wrow['end'] = date('Y-m-d H:i:s', $wdate);
  96. }
  97.  
  98. // if not recurring, insert the event and return
  99. if($single || empty($wrow['rfc5545']) == true) {
  100. $log->debug("doInsert", "Inserted wrow -\n " . print_r($wrow, true));
  101.  
  102. // insert the first and maybe only row
  103. $editor->db()->insert('calendar.events', $wrow);
  104.  
  105. return;
  106.  
  107. } else {
  108. //-------------------------------------------------------------------------------------------------------------
  109. // if this is a recurring event, set the recurring id for the initial event of the set and save it for the rest
  110. //-------------------------------------------------------------------------------------------------------------
  111. // if an rfcid is passed, use it else generate one
  112. if(empty($useRFCID ) == false) {
  113. $save_rfcid = $useRFCID;
  114. } else {
  115. $save_rfcid = $wrow['rfcid'] = uniqid("", true );
  116. }
  117.  
  118. // get and decode start date
  119. $sdate = strtotime($row['start']);
  120. $format_start = date('Y-m-d H:i:s', $sdate);
  121.  
  122. // get rfcfates
  123. $dates = explode("|", $row['rfcDates']);
  124. $sedate = explode("~", $dates[0]);
  125.  
  126. // loop through dates and build inserts
  127. for($i = 0, $size = count($dates); $i < $size; ++$i) {
  128.  
  129. // is the date to be skipped
  130. if($dates[0] == null) {
  131. continue;
  132. }
  133.  
  134. // the repeat date is less than the start date of the row, skip it
  135. if($sedate[0] < $format_start) {
  136. continue;
  137. }
  138.  
  139. // make MySql assign record id
  140. $wrow['RID'] = null;
  141. $wrow['action'] = ACTION_NONE;
  142.  
  143. // set start date and end date from rfcdates array item
  144. $sedate = explode("~", $dates[$i]);
  145. $wrow['start'] = $sedate[0];
  146.  
  147. $log->debug("doInsert", "start - " . $sedate[0]);
  148.  
  149. // Put the formatted start date and time into "whenHold" for date searches
  150. //$xdate = strtotime($wrow['start']);
  151. $wrow['whenHold'] = date('l, F j, Y g:i A', strtotime($sedate[0]));
  152.  
  153. // handle a recurring allDay event
  154. if($wrow['allDay'] == "1") {
  155. $wrow['end'] = null;
  156. } else {
  157. $wrow['end'] = $sedate[1];
  158. }
  159.  
  160. // make the recurring event set id the same as the first
  161. $wrow['rfcid'] = $save_rfcid;
  162.  
  163. // make the individual event id unique
  164. $wrow['id'] = uniqid("", true );
  165.  
  166. // do INSERT
  167. $editor->db()->insert('calendar.events', $wrow);
  168. }
  169. }
  170. $log->debug("doInsert", "doInsert Ending");
  171.  
  172. }
  173. #endregion
  174.  
  175. #region Editor definition
  176. //---------------------------------------------------------------------
  177. // Build Editor instance and process the data coming from _POST
  178. //---------------------------------------------------------------------
  179. Editor::inst( $db, 'calendar.events', 'RID' )
  180.  
  181. ->fields(
  182. Field::inst( 'RID' )
  183. ->set(false), // ID is automatically set by the database on create
  184.  
  185. Field::inst( 'id' ),
  186.  
  187. Field::inst( 'category' ),
  188.  
  189. Field::inst( 'title' ),
  190.  
  191. Field::inst( 'allDay' ),
  192.  
  193. Field::inst( 'action' ),
  194.  
  195. Field::inst( 'start' )
  196. ->validator( 'Validate::dateFormat', array(
  197. "format" => 'l, F j, Y g:i A',
  198. "message" => "Please enter a date in the correct format"
  199. ) )
  200. ->getFormatter( 'Format::datetime', array(
  201. 'from' => 'Y-m-d H:i:s',
  202. 'to' => 'l, F j, Y g:i A'
  203. ) )
  204. ->setFormatter( 'Format::datetime', array(
  205. 'from' => 'l, F j, Y g:i A',
  206. 'to' => 'Y-m-d H:i:s'
  207. ) ),
  208.  
  209. Field::inst( 'end' )
  210. ->validator( 'Validate::dateFormat', array(
  211. "format" => 'l, F j, Y g:i A',
  212. "message" => "Please enter a date in the correct format"
  213. ) )
  214. ->getFormatter( 'Format::datetime', array(
  215. 'from' => 'Y-m-d H:i:s',
  216. 'to' => 'l, F j, Y g:i A'
  217. ) )
  218. ->setFormatter( 'Format::datetime', array(
  219. 'from' => 'l, F j, Y g:i A',
  220. 'to' => 'Y-m-d H:i:s'
  221. ) ),
  222.  
  223. Field::inst( 'whenHold' ),
  224.  
  225. Field::inst( 'locHold' )->setFormatter( 'Format::nullEmpty' ),
  226.  
  227. Field::inst( 'rfc5545' ),
  228.  
  229. Field::inst( 'rfcDates' ),
  230.  
  231. Field::inst( 'rfcid' ),
  232.  
  233. Field::inst( 'location' ),
  234.  
  235. Field::inst( 'description' ),
  236.  
  237. Field::inst( 'placeID' ),
  238.  
  239. Field::inst( 'institution' ),
  240.  
  241. Field::inst( 'address' ),
  242.  
  243. Field::inst( 'city' ),
  244.  
  245. Field::inst( 'state' ),
  246.  
  247. Field::inst( 'country' ),
  248.  
  249. Field::inst( 'zipcode' ),
  250.  
  251. Field::inst( 'neighborhood' ),
  252.  
  253. Field::inst( 'phone' ),
  254.  
  255. Field::inst( 'webURL' ),
  256.  
  257. Field::inst( 'mapURL' ),
  258.  
  259. Field::inst( 'locPicURL' ),
  260.  
  261. Field::inst( 'latitude' ),
  262.  
  263. Field::inst( 'longitude' )
  264.  
  265. )
  266. #endregion
  267.  
  268. #region Event creation
  269. ->on( 'preCreate',
  270. function ( $editor, $values ) use($log) {
  271.  
  272. $log->info("preCreate", "preCreate Starting - " . $values['action']);
  273.  
  274. $editor
  275. ->field( 'action' )
  276. ->setValue( ACTION_NONE );
  277.  
  278. doInsert($editor, $values, null, false, $log);
  279.  
  280. $log->info("preCreate", "preCreate Ending - " . $values['action']);
  281.  
  282. return(false);
  283. })
  284. #endregion
  285.  
  286. #region Event editing
  287. ->on( 'preEdit',
  288. function ( $editor, $id, $values ) use($log) {
  289.  
  290. $log->debug("preEdit", "preEdit Started - Action code = " . $values['action'] );
  291.  
  292. $log->debug('preEdit', "New values - \n " . print_r($values, true) . PHP_EOL);
  293.  
  294. //-----------------------------------------------------------------------------------
  295. // if ignore or none
  296. //-----------------------------------------------------------------------------------
  297. if($values['action'] == ACTION_EDIT_IGNORE || $values['action'] == ACTION_NONE ) {
  298. return(false);
  299. }
  300.  
  301. //-----------------------------------------------------------------------------------
  302. // Get current row data
  303. //-----------------------------------------------------------------------------------
  304. // get the original row for deleting before using the new values for insert and updating it
  305. $rowdata = $editor->db()
  306. ->select( 'calendar.events', '*', array( 'id' => $values['id'] ) )
  307. ->fetchAll();
  308.  
  309. $log->debug('preEdit', "Current Row - \n " . print_r($rowdata, true) . PHP_EOL);
  310.  
  311. //-------------------------------------------------------------------------------------
  312. // Update this event only
  313. //-------------------------------------------------------------------------------------
  314. if($values['action'] == ACTION_EDIT_ONLY) {
  315.  
  316. $editonly = "DELETE FROM calendar.events WHERE id = '" . $rowdata['0']['id'] . "';";
  317.  
  318. $log->debug("postEdit", "editonly - " . $editonly);
  319.  
  320. $editor->db()->raw()->exec($editonly);
  321.  
  322. doInsert($editor, $values, $values['rfcid'], true, $log);
  323.  
  324. return(false);
  325.  
  326. //---------------------------------------------------------------------------------------
  327. // Update this event and all in the future for this set
  328. //---------------------------------------------------------------------------------------
  329. } else if($values['action'] == ACTION_EDIT_FORWARD) {
  330.  
  331. $testtime = date('Y-m-d H:i:s', strtotime($rowdata['0']['start']));
  332.  
  333. $delforw = "DELETE FROM calendar.events WHERE rfcid = '" . $rowdata['0']['rfcid'] . "'" .
  334. " AND start >= '" . $testtime . "';";
  335.  
  336. $log->info("preRemove", "delforw - " . $delforw);
  337.  
  338. $editor->db()->raw()->exec($delforw);
  339.  
  340. doInsert($editor, $values, null, false, $log);
  341.  
  342. return(false);
  343.  
  344. //---------------------------------------------------------------------------------------
  345. // Update all events in this event set
  346. //---------------------------------------------------------------------------------------
  347. } else if($values['action'] == ACTION_EDIT_ALL) {
  348.  
  349. $delall = "DELETE FROM calendar.events WHERE rfcid = '" . $rowdata['0']['rfcid'] . "';";
  350.  
  351. $log->info("preRemove", "delall - " . $delall);
  352.  
  353. $editor->db()->raw()->exec($delall);
  354.  
  355. doInsert($editor, $values, null, false, $log);
  356.  
  357. return(false);
  358.  
  359. //---------------------------------------------------------------------------------------
  360. // The proverabal "should never get here"
  361. //---------------------------------------------------------------------------------------
  362. } else {
  363. return(false);
  364. }
  365. })
  366. #endregion
  367.  
  368. #region Event deletion
  369. ->on( 'preRemove',
  370. function ( $editor, $id, $values ) use($log) {
  371.  
  372. $log->info("preRemove", "preRemove Started - Action code = " . $values['action'] );
  373.  
  374. // if ignore the delete
  375. if($values['action'] == ACTION_DELETE_IGNORE) {
  376. $log->info('preRemove', "'preRemove' entered with ACTION_DELETE_IGNORE");
  377. return(false);
  378.  
  379. // if to delete only this event
  380. } else if($values['action'] == ACTION_DELETE_ONLY) {
  381.  
  382. $delonly = "DELETE FROM calendar.events WHERE id = '" . $values['id'] . "';";
  383.  
  384. $log->info("preRemove", "delonly - " . $delonly);
  385.  
  386. $editor->db()->raw()->exec($delonly);
  387.  
  388. return(false);
  389.  
  390. // delete this event and all in the future for this set
  391. } else if($values['action'] == ACTION_DELETE_FORWARD) {
  392.  
  393. $testtime = date('Y-m-d H:i:s', strtotime($values['start']));
  394.  
  395. $delforw = "DELETE FROM calendar.events WHERE rfcid = '" . $values['rfcid'] . "'" .
  396. " AND start >= '" . $testtime . "';";
  397.  
  398. $log->info("preRemove", "delforw - " . $delforw);
  399.  
  400. $editor->db()->raw()->exec($delforw);
  401.  
  402. return(false);
  403.  
  404. // delete all events in this event set
  405. } else if($values['action'] == ACTION_DELETE_ALL) {
  406.  
  407. $delall = "DELETE FROM calendar.events WHERE rfcid = '" . $values['rfcid'] . "';";
  408.  
  409. $log->info("preRemove", "delall - " . $delall);
  410.  
  411. $editor->db()->raw()->exec($delall);
  412.  
  413. return(false);
  414.  
  415. } else {
  416. return(false);
  417. }
  418. })
  419.  
  420. ->on( 'postRemove',
  421. function ( $editor, $id, $values) use($log) {
  422.  
  423. })
  424. #endregion
  425.  
  426. #region Processing
  427. ->debug( true )
  428.  
  429. ->process( $_POST )
  430.  
  431. ->json();
  432. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement