Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- #region Intitialization, globals, constants, and support functions
- error_reporting(E_ALL);
- ini_set('display_errors', '1');
- // DataTables PHP library
- require( "C:/wamp64/www/distLibs/phpLibs/DataTables-Editor/DataTables.php" );
- // OTLogger PHP library
- require( "C:/wamp64/www/distLibs/phpLibs/OTLogger.php" );
- use OTLogger\OTLogger;
- // Alias Editor classes so they are easy to use
- use
- DataTables\Editor,
- DataTables\Editor\Field,
- DataTables\Editor\Format,
- DataTables\Editor\Mjoin,
- DataTables\Editor\Options,
- DataTables\Editor\Upload,
- DataTables\Editor\Validate;
- // Constants
- const ACTION_NONE = "0";
- const ACTION_ADD = "1";
- const ACTION_DELETE = "10";
- const ACTION_DELETE_ALL = "11";
- const ACTION_DELETE_ONLY = "12";
- const ACTION_DELETE_FORWARD = "13";
- const ACTION_DELETE_IGNORE = "14";
- const ACTION_EDIT = "20";
- const ACTION_EDIT_ALL = "21";
- const ACTION_EDIT_ONLY = "22";
- const ACTION_EDIT_FORWARD = "23";
- const ACTION_EDIT_IGNORE = "24";
- // Logging support
- $log = new OTLogger();
- #endregion
- #region Support functions
- //--------------------------------------------------------------------------
- // Routine to insert new event(s)
- //--------------------------------------------------------------------------
- function doInsert($editor, $row, $useRFCID, $single, $log) {
- $save_rfcid = "";
- $log->debug("doInsert", "doInsert Starting");
- $log->debug("doInsert", "Incoming values -\n " . print_r($row, true));
- //-----------------------------------------------------------------------------------
- // clean-up and normalize input values
- //-----------------------------------------------------------------------------------
- // create work array for insert
- $wrow = $row;
- // clean out what isn't needed
- unset($wrow['DT_RowId']);
- unset($wrow['RID']);
- unset($wrow['whenHolder']);
- unset($wrow['locHolder']);
- // now make sure RID is null so MySql will provide it
- $wrow += ['RID' => null];
- // make sure the action code is clear
- $wrow['action'] = ACTION_NONE;
- // add some empty columns
- $wrow += ['whenHold' => null];
- $wrow += ['locHold' => null];
- // make the event id unique
- $wrow['id'] = uniqid("", true );
- // Put the formatted start date and time into "whenHold" for date searches
- $wrow['whenHold'] = $wrow['start'];
- // set start and end dates to proper format
- $wdate = strtotime($wrow['start']);
- $wrow['start'] = date('Y-m-d H:i:s', $wdate);
- // handle an allDay event and set 'end' accordingly
- if($wrow['allDay'] == "1") {
- $wrow['end'] = null;
- } else {
- $wdate = strtotime($wrow['end']);
- $wrow['end'] = date('Y-m-d H:i:s', $wdate);
- }
- // if not recurring, insert the event and return
- if($single || empty($wrow['rfc5545']) == true) {
- $log->debug("doInsert", "Inserted wrow -\n " . print_r($wrow, true));
- // insert the first and maybe only row
- $editor->db()->insert('calendar.events', $wrow);
- return;
- } else {
- //-------------------------------------------------------------------------------------------------------------
- // if this is a recurring event, set the recurring id for the initial event of the set and save it for the rest
- //-------------------------------------------------------------------------------------------------------------
- // if an rfcid is passed, use it else generate one
- if(empty($useRFCID ) == false) {
- $save_rfcid = $useRFCID;
- } else {
- $save_rfcid = $wrow['rfcid'] = uniqid("", true );
- }
- // get and decode start date
- $sdate = strtotime($row['start']);
- $format_start = date('Y-m-d H:i:s', $sdate);
- // get rfcfates
- $dates = explode("|", $row['rfcDates']);
- $sedate = explode("~", $dates[0]);
- // loop through dates and build inserts
- for($i = 0, $size = count($dates); $i < $size; ++$i) {
- // is the date to be skipped
- if($dates[0] == null) {
- continue;
- }
- // the repeat date is less than the start date of the row, skip it
- if($sedate[0] < $format_start) {
- continue;
- }
- // make MySql assign record id
- $wrow['RID'] = null;
- $wrow['action'] = ACTION_NONE;
- // set start date and end date from rfcdates array item
- $sedate = explode("~", $dates[$i]);
- $wrow['start'] = $sedate[0];
- $log->debug("doInsert", "start - " . $sedate[0]);
- // Put the formatted start date and time into "whenHold" for date searches
- //$xdate = strtotime($wrow['start']);
- $wrow['whenHold'] = date('l, F j, Y g:i A', strtotime($sedate[0]));
- // handle a recurring allDay event
- if($wrow['allDay'] == "1") {
- $wrow['end'] = null;
- } else {
- $wrow['end'] = $sedate[1];
- }
- // make the recurring event set id the same as the first
- $wrow['rfcid'] = $save_rfcid;
- // make the individual event id unique
- $wrow['id'] = uniqid("", true );
- // do INSERT
- $editor->db()->insert('calendar.events', $wrow);
- }
- }
- $log->debug("doInsert", "doInsert Ending");
- }
- #endregion
- #region Editor definition
- //---------------------------------------------------------------------
- // Build Editor instance and process the data coming from _POST
- //---------------------------------------------------------------------
- Editor::inst( $db, 'calendar.events', 'RID' )
- ->fields(
- Field::inst( 'RID' )
- ->set(false), // ID is automatically set by the database on create
- Field::inst( 'id' ),
- Field::inst( 'category' ),
- Field::inst( 'title' ),
- Field::inst( 'allDay' ),
- Field::inst( 'action' ),
- Field::inst( 'start' )
- ->validator( 'Validate::dateFormat', array(
- "format" => 'l, F j, Y g:i A',
- "message" => "Please enter a date in the correct format"
- ) )
- ->getFormatter( 'Format::datetime', array(
- 'from' => 'Y-m-d H:i:s',
- 'to' => 'l, F j, Y g:i A'
- ) )
- ->setFormatter( 'Format::datetime', array(
- 'from' => 'l, F j, Y g:i A',
- 'to' => 'Y-m-d H:i:s'
- ) ),
- Field::inst( 'end' )
- ->validator( 'Validate::dateFormat', array(
- "format" => 'l, F j, Y g:i A',
- "message" => "Please enter a date in the correct format"
- ) )
- ->getFormatter( 'Format::datetime', array(
- 'from' => 'Y-m-d H:i:s',
- 'to' => 'l, F j, Y g:i A'
- ) )
- ->setFormatter( 'Format::datetime', array(
- 'from' => 'l, F j, Y g:i A',
- 'to' => 'Y-m-d H:i:s'
- ) ),
- Field::inst( 'whenHold' ),
- Field::inst( 'locHold' )->setFormatter( 'Format::nullEmpty' ),
- Field::inst( 'rfc5545' ),
- Field::inst( 'rfcDates' ),
- Field::inst( 'rfcid' ),
- Field::inst( 'location' ),
- Field::inst( 'description' ),
- Field::inst( 'placeID' ),
- Field::inst( 'institution' ),
- Field::inst( 'address' ),
- Field::inst( 'city' ),
- Field::inst( 'state' ),
- Field::inst( 'country' ),
- Field::inst( 'zipcode' ),
- Field::inst( 'neighborhood' ),
- Field::inst( 'phone' ),
- Field::inst( 'webURL' ),
- Field::inst( 'mapURL' ),
- Field::inst( 'locPicURL' ),
- Field::inst( 'latitude' ),
- Field::inst( 'longitude' )
- )
- #endregion
- #region Event creation
- ->on( 'preCreate',
- function ( $editor, $values ) use($log) {
- $log->info("preCreate", "preCreate Starting - " . $values['action']);
- $editor
- ->field( 'action' )
- ->setValue( ACTION_NONE );
- doInsert($editor, $values, null, false, $log);
- $log->info("preCreate", "preCreate Ending - " . $values['action']);
- return(false);
- })
- #endregion
- #region Event editing
- ->on( 'preEdit',
- function ( $editor, $id, $values ) use($log) {
- $log->debug("preEdit", "preEdit Started - Action code = " . $values['action'] );
- $log->debug('preEdit', "New values - \n " . print_r($values, true) . PHP_EOL);
- //-----------------------------------------------------------------------------------
- // if ignore or none
- //-----------------------------------------------------------------------------------
- if($values['action'] == ACTION_EDIT_IGNORE || $values['action'] == ACTION_NONE ) {
- return(false);
- }
- //-----------------------------------------------------------------------------------
- // Get current row data
- //-----------------------------------------------------------------------------------
- // get the original row for deleting before using the new values for insert and updating it
- $rowdata = $editor->db()
- ->select( 'calendar.events', '*', array( 'id' => $values['id'] ) )
- ->fetchAll();
- $log->debug('preEdit', "Current Row - \n " . print_r($rowdata, true) . PHP_EOL);
- //-------------------------------------------------------------------------------------
- // Update this event only
- //-------------------------------------------------------------------------------------
- if($values['action'] == ACTION_EDIT_ONLY) {
- $editonly = "DELETE FROM calendar.events WHERE id = '" . $rowdata['0']['id'] . "';";
- $log->debug("postEdit", "editonly - " . $editonly);
- $editor->db()->raw()->exec($editonly);
- doInsert($editor, $values, $values['rfcid'], true, $log);
- return(false);
- //---------------------------------------------------------------------------------------
- // Update this event and all in the future for this set
- //---------------------------------------------------------------------------------------
- } else if($values['action'] == ACTION_EDIT_FORWARD) {
- $testtime = date('Y-m-d H:i:s', strtotime($rowdata['0']['start']));
- $delforw = "DELETE FROM calendar.events WHERE rfcid = '" . $rowdata['0']['rfcid'] . "'" .
- " AND start >= '" . $testtime . "';";
- $log->info("preRemove", "delforw - " . $delforw);
- $editor->db()->raw()->exec($delforw);
- doInsert($editor, $values, null, false, $log);
- return(false);
- //---------------------------------------------------------------------------------------
- // Update all events in this event set
- //---------------------------------------------------------------------------------------
- } else if($values['action'] == ACTION_EDIT_ALL) {
- $delall = "DELETE FROM calendar.events WHERE rfcid = '" . $rowdata['0']['rfcid'] . "';";
- $log->info("preRemove", "delall - " . $delall);
- $editor->db()->raw()->exec($delall);
- doInsert($editor, $values, null, false, $log);
- return(false);
- //---------------------------------------------------------------------------------------
- // The proverabal "should never get here"
- //---------------------------------------------------------------------------------------
- } else {
- return(false);
- }
- })
- #endregion
- #region Event deletion
- ->on( 'preRemove',
- function ( $editor, $id, $values ) use($log) {
- $log->info("preRemove", "preRemove Started - Action code = " . $values['action'] );
- // if ignore the delete
- if($values['action'] == ACTION_DELETE_IGNORE) {
- $log->info('preRemove', "'preRemove' entered with ACTION_DELETE_IGNORE");
- return(false);
- // if to delete only this event
- } else if($values['action'] == ACTION_DELETE_ONLY) {
- $delonly = "DELETE FROM calendar.events WHERE id = '" . $values['id'] . "';";
- $log->info("preRemove", "delonly - " . $delonly);
- $editor->db()->raw()->exec($delonly);
- return(false);
- // delete this event and all in the future for this set
- } else if($values['action'] == ACTION_DELETE_FORWARD) {
- $testtime = date('Y-m-d H:i:s', strtotime($values['start']));
- $delforw = "DELETE FROM calendar.events WHERE rfcid = '" . $values['rfcid'] . "'" .
- " AND start >= '" . $testtime . "';";
- $log->info("preRemove", "delforw - " . $delforw);
- $editor->db()->raw()->exec($delforw);
- return(false);
- // delete all events in this event set
- } else if($values['action'] == ACTION_DELETE_ALL) {
- $delall = "DELETE FROM calendar.events WHERE rfcid = '" . $values['rfcid'] . "';";
- $log->info("preRemove", "delall - " . $delall);
- $editor->db()->raw()->exec($delall);
- return(false);
- } else {
- return(false);
- }
- })
- ->on( 'postRemove',
- function ( $editor, $id, $values) use($log) {
- })
- #endregion
- #region Processing
- ->debug( true )
- ->process( $_POST )
- ->json();
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement