/**
  * Convert an Event feed object contains within the feed (previously parsed by ESS_Import::get_feed_content())
  * into the relevents $_POST[xxx] elements, to be ready to be aggregated by Wordpress.
  *
  * @param 	Array	FEED_	Array that contain the relevent elements of an event.
  * @return 	Boolean	result	return a boolean value.
  *
  */
 public static function set_post_from_feed($FEED_)
 {
     //dd( $FEED_ );
     $_REQUEST['action'] = 'event_save';
     $_REQUEST['event_id'] = 0;
     $_POST['action'] = $_REQUEST['action'];
     $_POST['event_id'] = $_REQUEST['event_id'];
     //dd( $FEED_[ 'generals' ] );
     $event_attr_ = explode(ESS_Feed::CUSTOM_ATTRIBUTE_SEPARATOR, $FEED_['generals']['description']);
     $_POST['event_attributes'] = @count($event_attr_) == 3 && $event_attr_ != NULL && strlen(ESS_Feed::CUSTOM_ATTRIBUTE_SEPARATOR) > 0 ? json_decode(@$event_attr_[1], TRUE) : array();
     $_POST['em_attributes'] = $_POST['event_attributes'];
     $_POST['event_excerpt'] = NULL;
     $_POST['event_slug'] = $FEED_['generals']['id'];
     $_POST['event_name'] = $FEED_['generals']['title'];
     $_POST['content'] = $FEED_['generals']['description'];
     $_POST['event_access'] = $FEED_['generals']['access'];
     //$FEED_[ 'generals' ][ 'uri' ];
     //$FEED_[ 'generals' ][ 'published' ];
     //dd( $_POST );
     $minpeople = 0;
     // can also be in custom attributes...
     $maxpeople = 0;
     // ==== PEOPLE ============================
     if (@count(@$FEED_['people']) > 0 && @$FEED_['people'] != NULL) {
         //dd( $FEED_[ 'people' ] );
         foreach ($FEED_['people'] as $people_) {
             if ($people_['type'] == 'organizer' || $people_['type'] == 'author') {
                 $_POST['event_owner_name'] = !isset($_POST['event_owner_name']) ? @$people_['name'] : $_POST['event_owner_name'];
                 $_POST['event_owner_email'] = !isset($_POST['event_owner_email']) ? @$people_['email'] : $_POST['event_owner_email'];
                 // === element available ===
                 //$people_[ 'firstname' ];
                 //$people_[ 'lastname' ];
                 //$people_[ 'organization' ];
                 //$people_[ 'logo' ];
                 //$people_[ 'icon' ];
                 //$people_[ 'uri' ];
                 //$people_[ 'address' ];
                 //$people_[ 'city' ];
                 //$people_[ 'zip' ];
                 //$people_[ 'state' ];
                 //$people_[ 'state_code' ];
                 //$people_[ 'country' ];
                 //$people_[ 'country_code' ];
                 //$people_[ 'phone' ];
             } else {
                 if ($people_['type'] == 'attendee') {
                     $maxpeople = @$people_['maxpeople'];
                     $minpeople = @$people_['minpeople'];
                     $_POST['event_excerpt'] = trim(@$people_['restriction'] . (intval(@$people_['minage']) > 0 ? " min age:" . $people_['minage'] : ''));
                 } else {
                     if ($people_['type'] == 'social') {
                         //$people_[ 'name' ];
                         //$people_[ 'uri' ];
                     }
                 }
             }
         }
     }
     // ==== DATES ============================
     if (@count(@$FEED_['dates']) > 0 && @$FEED_['dates'] != NULL) {
         $_POST['event_all_day'] = 0;
         //dd( $FEED_[ 'dates' ] );
         $date_ = $FEED_['dates'][0];
         //!\\ TAKE IN CONSIDERATION ONLY THE FIRST FEED DATE
         if (FeedValidator::isValidDate($date_['start'])) {
             $dur = floatval(@$date_['duration']);
             $unit = strtolower(strlen(@$date_['unit']) > 0 ? $date_['unit'] . "s" : ' seconds');
             if ($unit == 'hours' && $dur > 0) {
                 $dur = $dur * 60;
                 $unit = "minutes";
             }
             $myDateTime = new DateTime(@$date_['start'], new DateTimeZone('GMT'));
             $date_start = $myDateTime->format('Y-m-d H:i:s');
             $str_start = $date_start . ($dur > 0 ? " + " . $dur . " " . $unit : "");
             $_POST['event_start_date'] = date('Y-m-d', strtotime($date_start));
             $_POST['event_end_date'] = date('Y-m-d', strtotime($str_start));
             $_POST['event_start_time'] = date('H:i:s', strtotime($date_start));
             $_POST['event_end_time'] = date('H:i:s', strtotime($str_start));
             //dd( $dur, $str_start, strtotime( $str_start ) );
             if ($date_['type'] == 'recurrent') {
                 $sd = explode(',', @$date_['selected_day']);
                 // 'monday','tuesday','wednesday','thursday','friday','saturday','sunday' OR 1,2,3,4,5,6,7,8,9,10,11,12,1,3,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
                 $sw = explode(',', @$date_['selected_week']);
                 // 'first', 'second', 'third', 'fourth', 'last'
                 $u = @$date_['unit'];
                 // 'hour','day','week','month','year'
                 $l = @$date_['limit'];
                 // integer: number of time the recursivity will occure.
                 $delay = "+" . $l . " " . $unit;
                 //echo "delay: ". $delay . " => " .date( DateTime::ATOM, strtotime( @$date_[ 'start' ]. " + ".$dur." seconds" ) );
                 $_POST['recurring'] = TRUE;
                 $_POST['event_end_date'] = date('Y-m-d', strtotime($delay, strtotime($str_start)));
                 $_POST['event_end_time'] = date('H:i:s', strtotime($delay, strtotime($str_start)));
                 $_POST['recurrence_interval'] = @$date_['interval'];
                 $_POST['recurrence_freq'] = $u == 'day' ? 'daily' : ($u == 'week' ? 'weekly' : ($u == 'month' ? 'monthly' : ($u == 'year' ? 'yearly' : 'hourly')));
                 $_POST['recurrence_byweekno'] = "";
                 if (strlen(@$date_['selected_week']) > 0 && @count($sw) > 0) {
                     $_POST['recurrence_byweekno'] = array();
                     foreach ($sw as $i => $sw_v) {
                         array_push($_POST['recurrence_byweekno'], $sw_v == 'first' ? 1 : ($sw_v == 'second' ? 2 : ($sw_v == 'third' ? 3 : ($sw_v == 'fourth' ? 4 : -1))));
                     }
                     $_POST['recurrence_byweekno'] = implode(',', $_POST['recurrence_byweekno']);
                 } else {
                     // if no week number have been specified in the feed attribute "selected_week", get the week number of the event as the default recurent value.
                     if ($u == 'month' && isset($date_['start']) && !isset($date_['selected_day'])) {
                         $ed = $_POST['event_end_date'];
                         $weekno = FeedValidator::getDateDiff('ww', $ed, date(DateTime::ATOM, strtotime('01/' . date('m', $ed) . '/' . date('Y', $ed)))) + 1;
                         $_POST['recurrence_byweekno'] = array($weekno > 4 ? -1 : $weekno);
                     }
                 }
                 $_POST['recurrence_byday'] = $_POST['recurrence_bydays'];
                 $_POST['recurrence_days'] = "";
                 $_POST['recurrence_bydays'] = "";
                 $_POST['recurrence_byday'] = "";
                 if (strlen(@$date_['selected_day']) > 0 && @count($sd) > 0) {
                     $_POST['recurrence_days'] = array();
                     $_POST['recurrence_bydays'] = array();
                     foreach ($sd as $i => $sd_v) {
                         if (intval($sd_v) > 1) {
                             array_push($_POST['recurrence_days'], $sd_v);
                         } else {
                             array_push($_POST['recurrence_bydays'], $sd_v == 'monday' ? 1 : ($sd_v == 'tuesday' ? 2 : ($sd_v == 'wednesday' ? 3 : ($sd_v == 'thursday' ? 4 : ($sd_v == 'friday' ? 5 : ($sd_v == 'saturday' ? 6 : 0))))));
                         }
                     }
                     $_POST['recurrence_bydays'] = implode(',', $_POST['recurrence_bydays']);
                     $_POST['recurrence_days'] = implode(',', $_POST['recurrence_days']);
                 } else {
                     // if no weekday have been specified in the feed attribute "selected_day", get the weekday of the event as the default recurrent day.
                     if ($u == 'week' && isset($date_['start'])) {
                         $weekday = date('N', @$date_['start']);
                         $_POST['recurrence_bydays'] = array($weekday == 7 ? 0 : $weekday);
                     }
                 }
                 $_POST['recurrence_byday'] = $_POST['recurrence_bydays'];
             }
         }
         //dd($_POST);
     }
     // ==== PRICES ===========================
     if (@count(@$FEED_['prices']) > 0 && @$FEED_['prices'] != NULL) {
         //dd( $FEED_[ 'prices' ] );
         $_POST['em_tickets'] = array();
         foreach ($FEED_['prices'] as $price_) {
             $date_start = '';
             $date_end = '';
             $dur = floatval(@$price_['duration']);
             $price_description = trim($price_['value'] . ' ' . @$price_['currency'] . ' ' . @$price_['uri']);
             if (strlen(@$price_['start']) > 0 && $dur > 0) {
                 $date_start = date('Y-m-d H:i:s', strtotime(@$price_['start']));
                 $date_end = date('Y-m-d H:i:s', strtotime(@$price_['start'] . " + " . $dur . " seconds"));
                 //echo "Date Range: ". $date_start ." => " .$date_end;
                 if ($price_['type'] == 'recurrent') {
                     $u = @$price_['unit'];
                     // 'hour','day','week','month','year'
                     $l = @$price_['limit'];
                     // integer: number of time the recurcivity will occure.
                     $price_description .= trim("<br/>Reccurent billing for " . $l . " " . $u);
                     $delay = "+" . $l . " " . $u . ($l > 1 ? 's' : '');
                     //echo "delay: ". $delay . " => " .date( DateTime::ATOM, strtotime( @$price_[ 'start' ]. " + ".$dur." seconds" ) );
                     // Defines the ticket's end date at the end of the recurcivity
                     $date_end = date('Y-m-d H:i:s', strtotime($delay, strtotime($date_start . " + " . $dur . " seconds")));
                 }
             }
             array_push($_POST['em_tickets'], array('event_rsvp_date' => !empty($date_start) ? date('Y-m-d', strtotime($date_start)) : '', 'event_rsvp_time' => !empty($date_start) ? date('H:i:s', strtotime($date_start)) : '', 'ticket_id' => 0, 'ticket_name' => $price_['name'], 'ticket_description' => $price_description, 'booking_comment' => $price_description, 'ticket_price' => $price_['value'], 'ticket_start' => $date_start, 'ticket_end' => $date_end, 'ticket_min' => $minpeople, 'ticket_max' => $maxpeople, 'ticket_spaces' => $maxpeople, 'event_spaces' => $maxpeople, 'ticket_members' => FALSE, 'ticket_guests' => $price_['mode'] == 'invitation' ? TRUE : FALSE, 'ticket_required' => $price_['mode'] != 'free' ? TRUE : FALSE, 'event_rsvp' => TRUE));
             //$_POST['booking_tax_rate'] = '';
             // -- available values:
             //$price[ 'type' ];
             //$price[ 'mode' ];
             //$price[ 'unit' ];
             //$price[ 'interval' ];
             //$price[ 'selected_day' ];
             //$price[ 'selected_week' ];
             //$price[ 'limit' ];
             //$price[ 'value' ];
             //$price[ 'name' ];
             //$price[ 'start' ];
             //$price[ 'duration' ];
             //$price[ 'currency' ];
             //$price[ 'uri' ];
         }
     }
     // ==== PLACES ===========================
     if (@count(@$FEED_['places']) > 0 && @$FEED_['places'] != NULL) {
         //dd( $FEED_[ 'places' ] );
         $place_ = $FEED_['places'][0];
         //!\\ TAKE IN CONSIDERATION ONLY THE FIRST FEED'S PLACE
         if (empty($place_['name'])) {
             $_POST['no_location'] = TRUE;
         } else {
             $_POST['no_location'] = FALSE;
             $_POST['location_name'] = $place_['name'];
             $_POST['location_address'] = strlen(@$place_['address']) > 0 ? $place_['address'] : $place_['name'];
             $_POST['location_town'] = @$place_['city'];
             $_POST['location_state'] = @$place_['state_code'];
             $_POST['location_postcode'] = @$place_['zip'];
             $_POST['location_region'] = @$place_['state'];
             $_POST['location_country'] = @$place_['country_code'];
             $_POST['location_latitude'] = @$place_['latitude'];
             $_POST['location_longitude'] = @$place_['longitude'];
         }
     } else {
         $_POST['no_location'] = TRUE;
     }
     // ==== TAGS =============================
     if (@count(@$FEED_['tags']) > 0 && @$FEED_['tags'] != NULL) {
         //dd( $FEED_[ 'tags' ] );
         $tags_ = array();
         foreach ($FEED_['tags'] as $i => $tag) {
             if (strlen($tag) > 1 && is_numeric($tag) == FALSE) {
                 array_push($tags_, $tag);
             }
         }
         if (count($tags_) > 0) {
             $_POST['event_tags'] = $tags_;
         }
     }
     // ==== CATEGORIES ==========================
     if (@count(@$FEED_['categories']) > 0 && @$FEED_['categories'] != NULL) {
         //dd( $FEED_[ 'categories' ] );
         $_POST['event_categories'] = array();
         foreach ($FEED_['categories'] as $i => $cat) {
             array_push($_POST['event_categories'], $cat['name']);
         }
     }
     // ==== MEDIA ============================
     if (@count(@$FEED_['media']) > 0 && @$FEED_['media'] != NULL) {
         //dd( $FEED_[ 'media' ] );
         $_POST['event_media'] = array();
         foreach ($FEED_['media'] as $i => $media_) {
             //dd( $media_ );
             if (FeedValidator::isValidURL($media_['uri'])) {
                 array_push($_POST['event_media'], array('uri' => $media_['uri'], 'name' => $media_['name'], 'type' => FeedValidator::getMediaType($media_['uri']), 'priority' => $media_['priority']));
             }
         }
     }
     return TRUE;
 }
 private function controlNodeContent($name, $value)
 {
     if (FeedValidator::isNull($value) == FALSE) {
         switch (strtolower($name)) {
             case 'start':
             case 'published':
             case 'updated':
                 return FeedValidator::isValidDate(FeedWriter::getISODate($value));
                 break;
             case 'name':
                 return FeedValidator::isNull($value) == FALSE ? TRUE : FALSE;
                 break;
             case 'email':
                 return FeedValidator::isValidEmail($value);
                 break;
             case 'logo':
             case 'icon':
             case 'uri':
                 return FeedValidator::isValidURL($value);
                 break;
             case 'latitude':
                 return FeedValidator::isValidLatitude($value);
                 break;
             case 'longitude':
                 return FeedValidator::isValidLongitude($value);
                 break;
             case 'country_code':
                 return FeedValidator::isValidCountryCode($value);
                 break;
             case 'currency':
                 return FeedValidator::isValidCurrency($value);
                 break;
             default:
                 return TRUE;
                 break;
         }
     }
     return TRUE;
 }
 private static function controlChannelElements($elmName, $val = NULL)
 {
     switch ($elmName) {
         default:
             return FeedValidator::isNull($val) ? FALSE : TRUE;
             break;
         case 'link':
             return FeedValidator::isValidURL($val) ? TRUE : FALSE;
             break;
         case 'updated':
         case 'published':
             return FeedValidator::isValidDate($val) ? TRUE : FeedValidator::isValidDate(self::getISODate($val));
             break;
     }
 }