public static function update_feeds_daily()
 {
     $headers = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
     $feeds_ = ESS_Database::get(array('feed_mode' => ESS_Database::FEED_MODE_CRON));
     if (@count($feeds_) > 0 && $feeds_ != NULL) {
         foreach ($feeds_ as $feed) {
             ESS_Import::save($feed->feed_url, 'on');
             //mail( '*****@*****.**', 'DAILY count: '.@count( $feeds_ ), 'feed URL: '. $feed->feed_url . '<br><br>_POST:' . @htmlvardump( $_POST ) . '<br><br>POST_ : ' . @htmlvardump( ESS_Import::$POST_ ), $headers );
         }
     }
 }
    private static function import_page()
    {
        $view = strtolower(isset($_GET['view']) ? $_GET['view'] : 'all');
        $active_count = ESS_Database::count(array('feed_status' => ESS_Database::FEED_STATUS_ACTIVE));
        $trash_count = ESS_Database::count(array('feed_status' => ESS_Database::FEED_STATUS_DELETED));
        $efi = ESS_Database::get(array('feed_status' => $view == 'trash' ? ESS_Database::FEED_STATUS_DELETED : ESS_Database::FEED_STATUS_ACTIVE));
        $url_all_events = em_add_get_params($_SERVER['REQUEST_URI'], array('view' => 'all')) . "#import";
        $url_trash_events = em_add_get_params($_SERVER['REQUEST_URI'], array('view' => 'trash')) . "#import";
        if ($view == 'trash' && $trash_count <= 0) {
            @wp_redirect($url_all_events);
            exit;
        }
        //d( $efi );
        ?>
<div class="em-menu-import em-menu-group" style="display:none;">

			<!-- PAGES NAV -->
			<div class="subsubsub">
				<a href='<?php 
        echo $url_all_events;
        ?>
' <?php 
        echo empty($view) || $view == 'all' ? 'class="current"' : '';
        ?>
><?php 
        _e('All', 'dbem');
        ?>
 <span class="count">(<?php 
        echo $active_count;
        ?>
)</span></a>
				<?php 
        if ($trash_count > 0) {
            ?>
&nbsp;|&nbsp;
				<a href='<?php 
            echo $url_trash_events;
            ?>
' <?php 
            echo $view == 'trash' ? 'class="current"' : '';
            ?>
><?php 
            _e('Trash', 'dbem');
            ?>
 <span class="count">(<?php 
            echo $trash_count;
            ?>
)</span></a>
				<?php 
        }
        ?>
			</div><?php 
        // ACTIONS NAV
        self::get_nav_action();
        $rowno = 0;
        $next_cron = NULL;
        foreach (get_option('cron') as $timestamp => $date_) {
            if (isset($date_[ESS_IO::CRON_EVENT_HOOK])) {
                $next_cron = $timestamp;
                // d( ESS_Timezone::get_date_GMT( date('U') ), ESS_Timezone::get_date_GMT( $timestamp ) );
            }
        }
        ?>
<!-- LIST -->
			<input type="hidden" value="" id="selected_event_id" name="selected_event_id" />
			<table class='widefat'>
				<thead>
					<tr>
						<th class='manage-column column-cb check-column' scope='col'><input type='checkbox' class='select-all' value='1'/></th>
						<th width="18"></th>
						<th><?php 
        _e('Title', 'dbem');
        ?>
</th>
						<th><?php 
        _e('Nb Events', 'dbem');
        ?>
</th>
						<th><?php 
        _e('Owner', 'dbem');
        ?>
</th>
						<th width="90"><?php 
        _e('Update Daily', 'dbem');
        ?>
</th>
						<th width="100"><?php 
        _e('Last Update', 'dbem');
        ?>
</th>
						<th width="100"><?php 
        _e('Next Update', 'dbem');
        ?>
</th>
						<th width="45"><?php 
        _e('Update', 'dbem');
        ?>
</th>
						<th width="50"><?php 
        _e('View', 'dbem');
        ?>
</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<th class='manage-column column-cb check-column' scope='col'><input type='checkbox' class='select-all' value='1'/></th>
						<th></th>
						<th><?php 
        _e('Title', 'dbem');
        ?>
</th>
						<th><?php 
        _e('Nb Events', 'dbem');
        ?>
</th>
						<th><?php 
        _e('Owner', 'dbem');
        ?>
</th>
						<th><?php 
        _e('Update', 'dbem');
        ?>
</th>
						<th><?php 
        _e('Last Update', 'dbem');
        ?>
</th>
						<th><?php 
        _e('Next Update', 'dbem');
        ?>
</th>
						<th><?php 
        _e('Update Daily', 'dbem');
        ?>
</th>
						<th><?php 
        _e('View', 'dbem');
        ?>
</th>
					</tr>
				</tfoot>
				<tbody><?php 
        if (@count($efi) > 0) {
            foreach ($efi as $feed) {
                $user = get_userdata($feed->feed_owner);
                $owner = isset($user) ? $user->data->display_name : '';
                $event_ids = explode(',', $feed->feed_event_ids);
                $class = $rowno % 2 ? 'alternate' : '';
                $rowno++;
                ?>
<tr class="<?php 
                echo $class;
                ?>
">
							<th class="check-column" scope="row">
								<label class="screen-reader-text" for="cb-select-<?php 
                echo $feed->feed_id;
                ?>
">Select My first event</label>
								<input type='checkbox' class='row-selector' id="cb-select-<?php 
                echo $feed->feed_id;
                ?>
" value='<?php 
                echo $feed->feed_id;
                ?>
' name='feeds[]'/>
								<div class="locked-indicator"></div>
							</th>
							<td align="center">
					 			<img src="http://www.google.com/s2/favicons?domain=http://<?php 
                echo $feed->feed_host;
                ?>
" width="16" height="16" alt="<?php 
                echo $feed->feed_host;
                ?>
" />
					 		</td>
							<td>
								<strong class="row-title"><?php 
                echo $feed->feed_title;
                ?>
</strong>
							</td>
							<td align="left">
								<?php 
                echo @count($event_ids);
                ?>
							</td>
							<td class="author column-author">
								<a><?php 
                echo $owner;
                ?>
</a>
							</td>
							<td>
								<?php 
                ESS_Elements::button_checkbox(array('checked' => $feed->feed_mode == ESS_Database::FEED_MODE_CRON ? TRUE : FALSE, 'on' => __('ON', 'dbem'), 'off' => __('OFF', 'dbem'), 'id' => 'feed_mode_' . $feed->feed_id, 'onchecked' => "jQuery('#cb-select-" . $feed->feed_id . "').prop('checked',true);", 'onunchecked' => "jQuery('#cb-select-" . $feed->feed_id . "').prop('checked',true);"));
                ?>
							</td>
							<td>
								<h4><?php 
                echo sprintf(__("%s ago", 'dbem'), human_time_diff(strtotime($feed->feed_timestamp)));
                ?>
</h4>
							</td>
							<td>
								<h4><?php 
                echo $feed->feed_mode == ESS_Database::FEED_MODE_CRON && $next_cron != NULL ? sprintf(__("in %s", 'dbem'), human_time_diff($next_cron)) : '-';
                ?>
</h4>
							</td>
							<td>
								<button title="<?php 
                _e("Reimport the feed to update the event.", 'dbem');
                ?>
" onmousedown="ess_admin.set_event_id('<?php 
                echo $feed->feed_id;
                ?>
');" class="button-primary reload_box" name="update_once" value="<?php 
                echo urlencode($feed->feed_url);
                ?>
" style="background-image:url('<?php 
                echo EM_ESS_URL;
                ?>
/assets/img/reload_icon_24x24.png');background-position:7px 2px;background-repeat:no-repeat;"></button>
							</td>
							<td>
								<a href="<?php 
                echo $feed->feed_url;
                ?>
" target="_blank" title="<?php 
                _e("Download the ESS Feed.", 'dbem');
                ?>
">
									<div class="button-primary arrow_cont">
										<div class="arrow_box"></div>
									</div>
								</a>
							</td>
						</tr><?php 
            }
        } else {
            ?>
<tr>
							<th colspan="8" style="text-align:left;"><?php 
            echo _e("No Feeds Found", 'dbem');
            ?>
</th>
						</tr><?php 
        }
        ?>
</tbody>
			</table>

			<?php 
        //self::get_nav_action();
        ?>

		</div><?php 
    }
 public static function save($feed_url, $feed_update_daily = 'on')
 {
     $feed_url = strtr(str_replace('&push=1', '', str_replace('&download=1', '', urldecode(esc_html($feed_url)))), array("&lt;" => "<", "&gt;" => ">", "&quot;" => '""', "&apos;" => "'", "&amp;" => "&"));
     if (!is_user_logged_in() && $feed_update_daily != 'on') {
         return;
     }
     $feed_update_daily = !isset($feed_update_daily) ? 'off' : 'on';
     global $ESS_Notices;
     if (ESS_Import::is_feed_valid($feed_url)) {
         $RESULT_ = ESS_Import::get_feed_content($feed_url);
         //dd( $feed_url, $RESULT_ );
         if (strlen(@$RESULT_['error']) <= 0 || @$RESULT_['error'] == NULL) {
             //dd( $RESULT_ );
             if (@count(@$RESULT_['feeds']) > 0 && $RESULT_['feeds'] != NULL) {
                 //if ( trim($feed_url) != trim($RESULT_['link']) )
                 //{
                 //	$ESS_Notices->add_info(
                 //		__("The URL of the feed defined within the feed is not the same then the one you have aggregate.",'dbem') .
                 //		"<br/>".
                 //		"<a href='".$RESULT_['link']."' target='_blank'>".$RESULT_['link']."</a>" .
                 //		"<br/>".
                 //		__("and",'dbem').
                 //		"<br/>".
                 //		"<a href='".$feed_url."' target='_blank'>".$feed_url."</a>"
                 //	);
                 // Trust more the feed URL specified within the feed or the one entered by the user in the aggregating form (?)
                 //$feed_url = $RESULT_['link'];
                 //}
                 $feed = ESS_Database::get(array('feed_uuid' => $RESULT_['id']));
                 $feed_id = @count($feed) > 0 ? reset($feed)->feed_id : 0;
                 $stored_ids_ = @count($feed) > 0 ? explode(',', reset($feed)->feed_event_ids) : array();
                 $post_ids_ = array();
                 $real_event_ids_ = array();
                 $existing_events_ = array();
                 // -- Control if the event is not already in the DB (to update it)
                 if (@count($stored_ids_) > 0) {
                     foreach ($stored_ids_ as $event_id) {
                         if ($event_id > 0) {
                             $EM_Event_evaluate = em_get_event($event_id);
                             if (isset($EM_Event_evaluate->event_name)) {
                                 array_push($existing_events_, array('event_title' => $EM_Event_evaluate->event_name, 'event_id' => $EM_Event_evaluate->event_id, 'post_id' => $EM_Event_evaluate->post_id));
                             }
                         }
                     }
                 }
                 //dd( $existing_events_, $RESULT_['feeds'] );
                 foreach ($RESULT_['feeds'] as $i_feed => $FEED_) {
                     $event_id = 0;
                     foreach ($existing_events_ as $event_) {
                         if (htmlspecialchars_decode($event_['event_title']) == htmlspecialchars_decode($FEED_['generals']['title'])) {
                             $event_id = $event_['event_id'];
                         }
                     }
                     $EM_Event = ESS_Import::create_event_from_feed($FEED_, $event_id);
                     //dd( $EM_Event );
                     if ($EM_Event->event_id > 0) {
                         array_push($real_event_ids_, $EM_Event->event_id);
                         array_push($post_ids_, $EM_Event->post_id);
                     }
                 }
                 /*
                 mail( '*****@*****.**', 'POST: '. @count( $_POST ).' file: ' .__FILE__.':'.__LINE__ ,
                 	'Feed URL: '. 			$feed_url							. '<br/><br/>' .
                 	'RESULT_' .				@htmlvardump( $RESULT_ ) 			. '<br/><br/>' .
                 	'global var _POST: ' . 	@htmlvardump( $_POST 			 ) 	. '<br/><br/>' .
                 	'EM_Event: ' . 			@htmlvardump( $EM_Event 		 ) 	. '<br/><br/>' .
                 	'post_ids_ : '. 		@htmlvardump( $post_ids_ 		 ),
                 
                 	'MIME-Version: 1.0' . "\r\n" .'Content-type: text/html; charset=iso-8859-1' . "\r\n"
                 );
                 */
                 //dd( $real_event_ids_ );
                 // == SAVE FEED ===
                 // Only save the ESS Feed if, at least, one event have been saved.
                 if (@count($real_event_ids_) > 0) {
                     $dom_ = parse_url($feed_url);
                     if (ESS_Database::add(array('feed_id' => $feed_id, 'feed_uuid' => $RESULT_['id'], 'feed_owner' => intval($EM_Event->owner), 'feed_event_ids' => implode(',', $real_event_ids_), 'feed_post_ids' => implode(',', $post_ids_), 'feed_title' => $RESULT_['title'], 'feed_host' => $dom_['host'], 'feed_url' => $feed_url, 'feed_status' => ESS_Database::FEED_STATUS_ACTIVE, 'feed_mode' => $feed_update_daily == 'on' ? ESS_Database::FEED_MODE_CRON : ESS_Database::FEED_MODE_STANDALONE)) === TRUE) {
                         // -- OK: Feed crawled and inserted.
                         $ESS_Notices->add_confirm(sprintf(__("The ESS feed have been %s.", 'dbem'), __(intval($feed_id) > 0 ? "updated" : "created", 'dbem')));
                     } else {
                         $ESS_Notices->add_error(__("Impossible to insert the ESS Feed in the Data Base: ", 'dbem') . ESS_Elements::get_ahref($feed_url));
                     }
                 } else {
                     $ESS_Notices->add_error(__("No events found in the ESS Feed:", 'dbem') . ESS_Elements::get_ahref($feed_url));
                 }
             } else {
                 $ESS_Notices->add_error(__("Impossible to analyse the ESS Feed: ", 'dbem') . ESS_Elements::get_ahref($feed_url));
             }
         } else {
             $ESS_Notices->add_error($RESULT_['error'] . "<br/>" . ESS_Elements::get_ahref($feed_url));
         }
     }
 }