+++ /dev/null
-<?php
-
-/**
- * Gaslight Media Members Database
- * Admin Shortcodes Reference
- *
- * PHP version 5.5
- *
- * @category glmWordPressPlugin
- * @package glmMembersDatabase
- * @author Chuck Scott <cscott@gaslightmedia.com>
- * @license http://www.gaslightmedia.com Gaslightmedia
- * @release admin.php,v 1.0 2014/10/31 19:31:47 cscott Exp $
- * @link http://dev.gaslightmedia.com/
- */
-
-/*
- * This model is called when the "Shortcodes" menu is selected
- *
- */
-class GlmMembersAdmin_cron_icalFeed
-{
-
- /**
- * WordPress Database Object
- *
- * @var $wpdb
- * @access public
- */
- public $wpdb;
-
- /*
- * Constructor
- *
- * This contructor performs the work for this model. This model returns
- * an array containing the following.
- *
- * 'status'
- *
- * True if successfull and false if there was a fatal failure.
- *
- * 'view'
- *
- * A suggested view name that the contoller should use instead of the
- * default view for this model or false to indicate that the default view
- * should be used.
- *
- * 'data'
- *
- * Data that the model is returning for use in merging with the view to
- * produce output.
- *
- * @wpdb object WordPress database object
- *
- * @return array Array containing status, suggested view, and any data
- */
- public function __construct ($wpdb, $config)
- {
-
- // Save WordPress Database object
- $this->wpdb = $wpdb;
-
- // Save plugin configuration object
- $this->config = $config;
-
- }
-
- public function modelAction( $actionData = false ) {
- if ( $id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ) ) {
- $this->importIcalFeed( $id );
- }
-
- // Return status, any suggested view, and any data to controller
- return array(
- 'status' => true,
- 'modelRedirect' => false,
- 'view' => 'admin/shortcodes/index.html',
- 'data' => array(
- 'addOns' => $this->config['addOns']
- )
- );
-
- }
- public function clearData()
- {
- $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "events" );
- $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "recurrences" );
- $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "times" );
- $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "event_categories" );
- $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "locations" );
- }
- public function importIcalFeed( $feed_id )
- {
- $debug = false;
- $clear_data = false;
- $number_events = 0;
- if ( $clear_data ) {
- $this->clearData();
- }
- $feed_content = $out = '';
- $event_data = array();
-
- $feed_url = $this->wpdb->get_var(
- $this->wpdb->prepare(
- "SELECT feed_url
- FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX. "feed_import
- WHERE id = %d",
- $feed_id
- )
- );
- // Set new updated date for feed
- $this->wpdb->update(
- GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'feed_import',
- array( 'updated' => current_time( 'mysql' ) ),
- array( 'id' => $feed_id ),
- '%s',
- '%d'
- );
-
- $Recurrences = new GlmDataEventsRecurrences($this->wpdb, $this->config);
- require_once GLM_MEMBERS_PLUGIN_PATH.'/models/admin/ajax/imageUpload.php';
- $image_upload = new GlmMembersAdmin_ajax_imageUpload($this->wpdb, $this->config);
-
- // Get the feed data
- $filename = $this->fetchIcalFile( $feed_url );
- require GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/classes/icalReader.php';
- $ical = new ical($filename);
- $events = $ical->events();
- if ( $debug ) {
- $out .= '<pre>$events: ' . print_r( $events, true ) . '</pre>';
- }
- if ( $events ) {
- foreach ( $events as $event ) {
- $contact = array();
- $image = '';
- $event['DESCRIPTION']
- = str_replace( '\n', "<br>", $event['DESCRIPTION'] );
- $intro = substr( strip_tags( $event['DESCRIPTION'] ), 0 ,150);
- if ( isset( $event['CONTACT'] ) ) {
- $contact_data = explode( ',', str_replace( '\\', '', $event['CONTACT'] ) );
- if ( $debug ) {
- //$out .= '<pre>$contact_data: ' . print_r( $contact_data, true ) . '</pre>';
- }
- $contact = array(
- 'name' => $contact_data[0],
- 'email' => $contact_data[1],
- 'phone' => $contact_data[2],
- );
- } else {
- $contact = array(
- 'name' => '',
- 'email' => '',
- 'phone' => '',
- );
- }
- if ( isset( $event['ATTACH'] ) ) {
- $img_url = preg_replace( '%FMTTYPE=image/(jpeg|gif|png)[:]%', '', $event['ATTACH'] );
- if ( $img_url ) {
- $res = $image_upload->storeImage( $img_url );
- $image = $res['newFileName'];
- }
- }
- $event_data = array(
- 'status' => $this->config['status_numb']['Active'],
- 'ref_type' => 10,
- 'created' => $ical->iCalDateToUnixTimestamp( $event['CREATED'] ),
- 'updated' => $ical->iCalDateToUnixTimestamp( $event['CREATED'] ),
- 'approved' => null,
- 'ical_uid' => $event['UID'],
- 'name' => $event['SUMMARY'],
- 'intro' => $intro,
- 'descr' => $event['DESCRIPTION'],
- 'image' => $image,
- 'url' => ( isset( $event['URL'] ) ? $event['URL']: '' ),
- 'contact_email' => $contact['name'],
- 'contact_name' => $contact['email'],
- 'contact_phone' => $contact['phone'],
- );
- if ( $debug ) {
- //$out .= '<pre>$event_data: ' . print_r( $event_data, true ) . '</pre>';
- }
- $event_data_format = array(
- '%d',
- '%d',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- );
- // check for this event record first to see if it can be
- // updated instead of insert
- $event_id = $this->wpdb->get_var(
- $this->wpdb->prepare(
- "SELECT id
- FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "events
- WHERE ical_uid = %s",
- $event['UID']
- )
- );
- if ( $event_id ) {
- $this->wpdb->update(
- GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'events',
- $event_data,
- array( 'id' => $event_id ),
- $event_data_format,
- '%d'
- );
- } else {
- $this->wpdb->insert(
- GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'events',
- $event_data,
- $event_data_format
- );
- $event_id = $this->wpdb->insert_id;
- }
- if ( !$event_id ) {
- die('something is wrong no event_id');
- }
- // generate the slug name for this new event
- $event_abstract = new GlmDataEvents( $this->wpdb, $this->config );
- $event_abstract->updateSlug( $event_id );
- // Location Data
- if ( isset( $event['LOCATION'] ) ) {
- $location_data = explode( ',', str_replace( '\\', '', $event['LOCATION'] ) );
- $place = ( isset( $location_data[0] ) ? $location_data[0]: '');
- $address = ( isset( $location_data[1] ) ? $location_data[1]: '');
- $city = ( isset( $location_data[2] ) ? $location_data[2]: '');
- $state = ( isset( $location_data[3] ) ? $location_data[3]: '');
- $zip = ( isset( $location_data[4] ) ? $location_data[4]: '');
- $location = array(
- 'event' => $event_id,
- 'name' => $place,
- 'address' => $address,
- 'city' => $this->getCityId( $city ),
- 'state' => $state,
- 'zip' => $zip,
- );
- $location_format = array(
- '%d', '%s', '%s', '%d', '%s', '%s'
- );
- if ( isset( $event['GEO'] ) ) {
- $geo_data = explode( ';', $event['GEO'] );
- $location['lat'] = $geo_data[0];
- $location['lon'] = $geo_data[1];
- $location_format[] = '%s';
- $location_format[] = '%s';
- }
- // Remove any old location data for this event_id.
- $this->wpdb->delete(
- GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'locations',
- array( 'event' => $event_id ),
- '%d'
- );
- $this->wpdb->insert(
- GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'locations',
- $location,
- $location_format
- );
- }
- // Remove any old recurrences data for this event_id.
- $this->wpdb->delete(
- GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'recurrences',
- array( 'event' => $event_id ),
- '%d'
- );
- // Remove any old times data for this event_id.
- $this->wpdb->delete(
- GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'times',
- array( 'event' => $event_id ),
- '%d'
- );
- $serialized_times = null;
- $day_of_week =
- $day_of_month =
- $month_of_year =
- $week_of_month =
- $by_day_of_month =
- $last_day_of_month = null;
- $start_time_only =
- $all_day =
- $recurring_event = 0;
- $day_of_week = 127;
- $month_of_year = 4095;
- $week_of_month = 63;
- $starting = $ical->iCalDateToUnixTimestamp( $event['DTSTART'] );
- $ending = $ical->iCalDateToUnixTimestamp( $event['DTEND'] );
- $from_date = date( 'Y-m-d', $starting );
- $to_date = date( 'Y-m-d', $ending );
- $btime = date( 'H:i', $starting );
- $etime = date( 'H:i', $ending );
- $freq = $byday = $until = '';
- if ( isset( $event['RRULE'] ) ) {
- $recurring_event = 1;
- $rrule_data = explode( ';', $event['RRULE'] );
- //echo '<pre>$rrule_data: ' . print_r( $rrule_data, true ) . '</pre>';
- if ( $rrule_data ) {
- foreach ( $rrule_data as $rule ) {
- if ( preg_match( '%FREQ=(.*)%', $rule, $matches ) ) {
- $freq = trim( $matches[1] );
- }
- if ( preg_match( '%BYDAY=(.*)%', $rule, $matches ) ) {
- $byday = trim( $matches[1] );
- }
- if ( preg_match( '%UNTIL=(.*)%', $rule, $matches ) ) {
- $until = trim( $matches[1] );
- }
- }
- }
- if ( $freq === 'WEEKLY' && $byday ) {
- // separated by commas
- $days = explode( ',',$byday );
- //echo '<pre>$days: ' . print_r( $days, true ) . '</pre>';
- if ( $days ) {
- $day_of_week = 0;
- foreach ( $days as $day ) {
- if ( preg_match( '%([0-9])?([A-Z]{2})%', $day, $d_matches ) ) {
- //echo '<pre>$d_matches: ' . print_r( $d_matches, true ) . '</pre>';
- if ( $d_matches[1] ) {
- $week_of_month = pow(2, $d_matches[1] );
- }
- switch ( $d_matches[2] ) {
- case "SU":
- $day_of_week += pow(2, 0);
- break;
- case "MO":
- $day_of_week += pow(2, 1);
- break;
- case "TU":
- $day_of_week += pow(2, 2);
- break;
- case "WE":
- $day_of_week += pow(2, 3);
- break;
- case "TH":
- $day_of_week += pow(2, 4);
- break;
- case "FR":
- $day_of_week += pow(2, 5);
- break;
- case "SA":
- $day_of_week += pow(2, 6);
- break;
- }
- }
- }
- }
- }
- if ( $until != '' ) {
- //echo '<pre>$until: ' . print_r( $until, true ) . '</pre>';
- $test_date = $ical->iCalDateToUnixTimestamp( $until );
- if ( $test_date ) {
- $to_date = date( 'Y-m-d', $test_date );
- }
- //echo '<pre>$test_date: ' . print_r( date( 'm/d/Y', $test_date), true ) . '</pre>';
- }
- }
- if ( $btime === $etime ) {
- if ( $btime === '00:00' ) {
- $all_day = true;
- } else {
- $start_time_only = true;
- }
- }
- if ( $btime && $etime && $etime === '00:00' ) {
- $start_time_only = true;
- }
- // for the recurrences part
- $recur_data = array(
- 'event' => $event_id,
- 'name' => 'Imported',
- 'start_time' => $btime,
- 'end_time' => $etime,
- 'start_time_only' => $start_time_only,
- 'from_date' => $from_date,
- 'to_date' => $to_date,
- 'all_day' => $all_day,
- 'recurring' => $recurring_event,
- 'month_of_year' => $month_of_year,
- 'week_of_month' => $week_of_month,
- 'day_of_week' => $day_of_week,
- 'day_of_month' => $day_of_month,
- 'by_day_of_month' => $by_day_of_month,
- 'last_day_of_month' => $last_day_of_month,
- 'specific_dates' => $serialized_times
- );
- $this->wpdb->insert(
- GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'recurrences',
- $recur_data,
- array(
- '%d',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%d',
- '%d',
- '%d',
- '%d',
- '%d',
- '%d',
- '%d',
- '%d',
- '%s'
- )
- );
- $recurr_id = $this->wpdb->insert_id;
- $Recurrences->createRecurrenceTimesEntries( $recurr_id, true, true );
- ++$number_events;
- }
- }
- if ( !$debug ) {
- $out = $number_events . ' events imported';
- }
- return $out;
- }
- public function fetchIcalFile( $url )
- {
- $path = '/tmp';
- $filename = tempnam( $path, "ICAL" );
- $fp = fopen( $filename, 'w+' );
- $ch = curl_init( $url );
- curl_setopt( $ch, CURLOPT_TIMEOUT, 50 );
- curl_setopt( $ch, CURLOPT_FILE, $fp );
- curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
- $fileData = curl_exec( $ch );
- $httpCode = curl_getinfo( $ch );
- curl_close( $ch );
- fclose( $fp );
- // Set correct file permissions
- $oldUmask = umask( 0 );
- chmod( $filename, 0660 );
- umask( $oldUmask );
- return $filename;
- }
- /**
- * getCityId
- *
- * Given a city name find and return the id of the city.
- * If a city cannot be found then create city with that name.
- *
- * @param mixed $city_name Name of the city
- *
- * @access public
- * @return id of city
- */
- public function getCityId($city_name)
- {
- if ( !$city_name ) {
- return false;
- }
- $sql = "
- SELECT id
- FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
- WHERE name like '" . esc_sql( trim( $city_name ) ) . "'";
- $cityId = $this->wpdb->get_row($sql, ARRAY_A);
- if ( !$cityId ) {
- // then add the city to the city table
- $this->wpdb->insert(
- GLM_MEMBERS_PLUGIN_DB_PREFIX . 'cities',
- array( 'name' => trim( $city_name ) ),
- array( '%s' )
- );
- return $this->wpdb->insert_id;
- } else {
- return $cityId['id'];
- }
- }
-
-}
-
-?>