/**
  * This will return a timestamp for the website timezone but ONLY when the current website timezone is different than the timezone set for the website.
  *
  * NOTE, this currently only works well with methods that return values.  If you use it with methods that echo values the $_timestamp property may not get reset to its original value and that could lead to some unexpected results!
  *
  * @access public
  * @param string               $field_name This is the name of the field on the object that contains the date/time value being returned.
  * @param string               $callback   must match a valid method in this class (defaults to get_datetime)
  * @param mixed (array|string) $args       This is the arguments that will be passed to the callback.
  * @param string               $prepend    You can include something to prepend on the timestamp
  * @param string               $append     You can include something to append on the timestamp
  * @throws EE_Error
  * @return string timestamp
  */
 public function display_in_my_timezone($field_name, $callback = 'get_datetime', $args = NULL, $prepend = '', $append = '')
 {
     EE_Registry::instance()->load_helper('DTT_Helper');
     $timezone = EEH_DTT_Helper::get_timezone();
     if ($timezone == $this->_timezone) {
         return '';
     }
     $original_timezone = $this->_timezone;
     $this->set_timezone($timezone);
     $fn = (array) $field_name;
     $args = array_merge($fn, (array) $args);
     if (!method_exists($this, $callback)) {
         throw new EE_Error(sprintf(__('The method named "%s" given as the callback param in "display_in_my_timezone" does not exist.  Please check your spelling', 'event_espresso'), $callback));
     }
     $args = (array) $args;
     $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append;
     $this->set_timezone($original_timezone);
     return $return;
 }
 /**
  * Writes some meta data to the CSV as a bunch of columns. Initially we're only
  * mentioning the version and timezone
  * @param resource $filehandle
  */
 public function write_metadata_to_csv($filehandle)
 {
     EE_Registry::instance()->load_helper('DTT_Helper');
     $data_row = array(EE_CSV::metadata_header);
     //do NOT translate because this exact string is used when importing
     $this->fputcsv2($filehandle, $data_row);
     EE_Registry::instance()->load_helper('DTT_Helper');
     $meta_data = array(0 => array('version' => espresso_version(), 'timezone' => EEH_DTT_Helper::get_timezone(), 'time_of_export' => current_time('mysql'), 'site_url' => site_url()));
     $this->write_data_array_to_csv($filehandle, $meta_data);
 }
 /**
  * This receives a timestring for a given field and ensures that it is setup to match what the internal settings
  * for the model are.  Returns a DateTime object.
  *
  * Note: a gotcha for when you send in unixtimestamp.  Remember a unixtimestamp is already timezone agnostic,
  * (functionally the equivalent of UTC+0).  So when you send it in, whatever timezone string you include is ignored.
  *
  * @param string $field_name The field being setup.
  * @param string $timestring   The date timestring being used.
  * @param string $incoming_format        The format for the time string.
  * @param string $timezone   By default, it is assumed the incoming timestring is in timezone for
  *                           		the blog.  If this is not the case, then it can be specified here.  If incoming format is
  *                           		'U', this is ignored.
  * @return DateTime
  */
 public function convert_datetime_for_query($field_name, $timestring, $incoming_format, $timezone = '')
 {
     //just using this to ensure the timezone is set correctly internally
     $this->get_formats_for($field_name);
     //load EEH_DTT_Helper
     EE_Registry::instance()->load_helper('DTT_Helper');
     $set_timezone = empty($timezone) ? EEH_DTT_Helper::get_timezone() : $timezone;
     $incomingDateTime = date_create_from_format($incoming_format, $timestring, new DateTimeZone($set_timezone));
     return $incomingDateTime->setTimeZone(new DateTimeZone($this->_timezone));
 }
 /**
  * _revenue_per_event_report
  * generates Business Report showing total revenue per event.
  *
  * @param string $period  The period (acceptable by PHP Datetime constructor) for which the report is generated.
  * @return int
  */
 private function _revenue_per_event_report($period = '-1 month')
 {
     $report_ID = 'txn-admin-revenue-per-event-report-dv';
     $TXN = EEM_Transaction::instance();
     $results = $TXN->get_revenue_per_event_report($period);
     $results = (array) $results;
     $revenue = array();
     $subtitle = '';
     if ($results) {
         $revenue[] = array(__('Event (only events that have a revenue greater than 1 are shown)', 'event_espresso'), __('Total Revenue', 'event_espresso'));
         foreach ($results as $result) {
             if ($result->revenue > 1) {
                 $event_name = stripslashes(html_entity_decode($result->event_name, ENT_QUOTES, 'UTF-8'));
                 $event_name = wp_trim_words($event_name, 5, '...');
                 $revenue[] = array($event_name, (double) $result->revenue);
             }
         }
         //setup the date range.
         EE_Registry::instance()->load_helper('DTT_Helper');
         $beginning_date = new DateTime('now' . $period, new DateTimeZone(EEH_DTT_Helper::get_timezone()));
         $ending_date = new DateTime('now', new DateTimeZone(EEH_DTT_Helper::get_timezone()));
         $subtitle = sprintf(_x('For the period: %s to %s', 'Used to give date range', 'event_espresso'), $beginning_date->format('Y-m-d'), $ending_date->format('Y-m-d'));
     }
     $report_title = __('Total Revenue per Event');
     $report_params = array('title' => $report_title, 'subtitle' => $subtitle, 'id' => $report_ID, 'revenue' => $revenue, 'noResults' => empty($revenue), 'noTxnMsg' => sprintf(__('%sThere are currently no transaction records in the last month for this report.%s', 'event_espresso'), '<h2>' . $report_title . '</h2><p>', '</p>'));
     wp_localize_script('ee-txn-reports-js', 'txnRevPerEvent', $report_params);
     return $report_ID;
 }
 /**
  * admin_footer_global
  * Anything triggered by the wp 'admin_footer' wp hook should be put in here. This particluar method will apply on ALL EE_Admin Pages.
  *
  * @access  public
  * @return  void
  */
 public function admin_footer_global()
 {
     //dialog container for dialog helper
     $d_cont = '<div class="ee-admin-dialog-container auto-hide hidden">' . "\n";
     $d_cont .= '<div class="ee-notices"></div>';
     $d_cont .= '<div class="ee-admin-dialog-container-inner-content"></div>';
     $d_cont .= '</div>';
     echo $d_cont;
     //help tour stuff?
     if (isset($this->_help_tour[$this->_req_action])) {
         echo implode('<br />', $this->_help_tour[$this->_req_action]);
     }
     //current set timezone for timezone js
     EE_Registry::instance()->load_helper('DTT_Helper');
     echo '<span id="current_timezone" class="hidden">' . EEH_DTT_Helper::get_timezone() . '</span>';
 }
 /**
  * Generates Business Report showing total registrations per event.
  * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated.
  *
  * @return string
  */
 private function _registrations_per_event_report($period = '-1 month')
 {
     $report_ID = 'reg-admin-registrations-per-event-report-dv';
     $results = EEM_Registration::instance()->get_registrations_per_event_and_per_status_report($period);
     $results = (array) $results;
     $regs = array();
     $subtitle = '';
     if ($results) {
         $column_titles = array();
         $tracker = 0;
         foreach ($results as $result) {
             $report_column_values = array();
             foreach ($result as $property_name => $property_value) {
                 $property_value = $property_name == 'Registration_Event' ? wp_trim_words($property_value, 4, '...') : (int) $property_value;
                 $report_column_values[] = $property_value;
                 if ($tracker === 0) {
                     if ($property_name == 'Registration_Event') {
                         $column_titles[] = __('Event', 'event_espresso');
                     } else {
                         $column_titles[] = EEH_Template::pretty_status($property_name, false, 'sentence');
                     }
                 }
             }
             $tracker++;
             $regs[] = $report_column_values;
         }
         //make sure the column_titles is pushed to the beginning of the array
         array_unshift($regs, $column_titles);
         //setup the date range.
         $DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone());
         $beginning_date = new DateTime("now " . $period, $DateTimeZone);
         $ending_date = new DateTime("now", $DateTimeZone);
         $subtitle = sprintf(_x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'), $beginning_date->format('Y-m-d'), $ending_date->format('Y-m-d'));
     }
     $report_title = __('Total Registrations per Event', 'event_espresso');
     $report_params = array('title' => $report_title, 'subtitle' => $subtitle, 'id' => $report_ID, 'regs' => $regs, 'noResults' => empty($regs), 'noRegsMsg' => sprintf(__('%sThere are currently no registration records in the last month for this report.%s', 'event_espresso'), '<h2>' . $report_title . '</h2><p>', '</p>'));
     wp_localize_script('ee-reg-reports-js', 'regPerEvent', $report_params);
     return $report_ID;
 }
 /**
  * @since 4.6.x
  */
 public function test_create_new_blank_datetime()
 {
     //if timezone is empty string then the setUp didn't work correctly.  For the purpose of this test
     //we want a high positive timezone, so let's force that if necessary
     if (get_option('timezone_string') != 'Australia/Sydney') {
         update_option('timezone_string', 'Australia/Sydney');
         EEM_Datetime::reset();
         EEM_Datetime::instance();
     }
     EE_Registry::instance()->load_helper('DTT_Helper');
     //make sure now is in the timezone we want to test with.
     $now = new Datetime('@' . (time() + DAY_IN_SECONDS * 30));
     $now->setTimeZone(new DateTimeZone(EEH_DTT_Helper::get_timezone()));
     $now->setTime('8', '0', '0');
     $now->setTimeZone(new DateTimeZone('America/Toronto'));
     //get the default datetime
     $default_date = EEM_Datetime::instance()->create_new_blank_datetime();
     $default_date = reset($default_date);
     //assert instance
     $this->assertInstanceOf('EE_Datetime', $default_date);
     //set its timezone to match our expected timezone
     $default_date->set_timezone('America/Toronto');
     $actual = $default_date->get_DateTime_object('DTT_EVT_start');
     $this->assertInstanceOf('DateTime', $actual);
     //assert timezones are the same
     $this->assertEquals($now->getTimezone(), $actual->getTimeZone());
     //assert that we have the correct values on the date... we'll do each part separately to verify.
     $this->assertEquals($now->format('Y'), $actual->format('Y'));
     $this->assertEquals($now->format('m'), $actual->format('m'));
     $this->assertEquals($now->format('d'), $actual->format('d'));
     $this->assertEquals($now->format('H'), $actual->format('H'));
     $this->assertEquals($now->format('i'), $actual->format('i'));
 }
 /**
  * retrieve  all payments from db between two dates,
  *
  * @param string $start_date incoming start date. If empty the beginning of today is used.
  * @param string $end_date   incoming end date. If empty the end of today is used.
  * @param string $format  	If you include $start_date or $end_date then you must include the format string
  *                         		for the format your date is in.
  * @param string $timezone   If your range is in a different timezone then the current setting on this
  *                           		WordPress install, then include it here.
  * @throws EE_Error
  *
  * @return EE_Payment[]
  */
 public function get_payments_made_between_dates($start_date = '', $end_date = '', $format = '', $timezone = '')
 {
     EE_Registry::instance()->load_helper('DTT_Helper');
     $timezone = empty($timezone) ? EEH_DTT_Helper::get_timezone() : $timezone;
     //if $start_date or $end date, verify $format is included.
     if ((!empty($start_date) || !empty($end_date)) && empty($format)) {
         throw new EE_Error(__('You included a start date and/or a end date for this method but did not include a format string.  The format string is needed for setting up the query', 'event_espresso'));
     }
     $now = new DateTime('now');
     // setup timezone objects once
     $modelDateTimeZone = new DateTimeZone($this->_timezone);
     $passedDateTimeZone = new DateTimeZone($timezone);
     // setup start date
     $start_date = !empty($start_date) ? date_create_from_format($format, $start_date, $passedDateTimeZone) : $now;
     $start_date->setTimeZone($modelDateTimeZone);
     $start_date = $start_date->format('Y-m-d') . ' 00:00:00';
     $start_date = strtotime($start_date);
     // setup end date
     $end_date = !empty($end_date) ? date_create_from_format($format, $end_date, $passedDateTimeZone) : $now;
     $end_date->setTimeZone($modelDateTimeZone);
     $end_date = $end_date->format('Y-m-d') . ' 23:59:59';
     $end_date = strtotime($end_date);
     // make sure our start date is the lowest value and vice versa
     $start = min($start_date, $end_date);
     $end = max($start_date, $end_date);
     //yes we generated the date and time string in utc but we WANT this start date and time used in the set timezone on the model.
     $start_date = $this->convert_datetime_for_query('PAY_timestamp', date('Y-m-d', $start) . ' 00:00:00', 'Y-m-d H:i:s', $this->get_timezone());
     $end_date = $this->convert_datetime_for_query('PAY_timestamp', date('Y-m-d', $end) . ' 23:59:59', 'Y-m-d H:i:s', $this->get_timezone());
     return $this->get_all(array(array('PAY_timestamp' => array('>=', $start_date), 'PAY_timestamp*' => array('<=', $end_date))));
 }
 /**
  * Generates Business Report showing total registrations per event.
  * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated.
  *
  * @return string
  */
 private function _registrations_per_event_report($period = '-1 month')
 {
     $report_ID = 'reg-admin-registrations-per-event-report-dv';
     $REG = EEM_Registration::instance();
     $results = $REG->get_registrations_per_event_report($period);
     $results = (array) $results;
     $regs = array();
     $subtitle = '';
     if ($results) {
         $regs[] = array(__('Event', 'event_espresso'), __('Total Registrations', 'event_espresso'));
         foreach ($results as $result) {
             $regs[] = array(wp_trim_words($result->event_name, 4, '...'), (int) $result->total);
         }
         //setup the date range.
         EE_Registry::instance()->load_helper('DTT_Helper');
         $beginning_date = new DateTime("now " . $period, new DateTimeZone(EEH_DTT_Helper::get_timezone()));
         $ending_date = new DateTime("now", new DateTimeZone(EEH_DTT_Helper::get_timezone()));
         $subtitle = sprintf(_x('For the period: %s to %s', 'Used to give date range', 'event_espresso'), $beginning_date->format('Y-m-d'), $ending_date->format('Y-m-d'));
     }
     $report_title = __('Total Registrations per Event', 'event_espresso');
     $report_params = array('title' => $report_title, 'subtitle' => $subtitle, 'id' => $report_ID, 'regs' => $regs, 'noResults' => empty($regs), 'noRegsMsg' => sprintf(__('%sThere are currently no registration records in the last month for this report.%s', 'event_espresso'), '<h2>' . $report_title . '</h2><p>', '</p>'));
     wp_localize_script('ee-reg-reports-js', 'regPerEvent', $report_params);
     return $report_ID;
 }
 /**
  * @since 4.6.12+
  */
 public function test_get_timestamp_with_offset()
 {
     //now in timezone currently set.
     $default_timezone = new DateTimeZone(EEH_DTT_Helper::get_timezone());
     $now = new DateTime("now", $default_timezone);
     $expected_offset = (int) $now->format('U') + (int) timezone_offset_get($default_timezone, $now);
     $this->assertEquals($expected_offset, EEH_DTT_Helper::get_timestamp_with_offset($now->format('U')));
     //this might fail because of execution time.
     $this->assertEquals(current_time('timestamp'), EEH_DTT_Helper::get_timestamp_with_offset());
     //now let's test with a different timezone for the incoming timestamp.
     $now->setTimeZone(new DateTimeZone('America/Toronto'));
     $expected_timestamp = (int) $now->format('U') + (int) timezone_offset_get(new DateTimeZone('America/Toronto'), $now);
     $this->assertEquals($expected_timestamp, EEH_DTT_Helper::get_timestamp_with_offset($now->format('U'), 'America/Toronto'));
 }