I ran the module through the coder module and tidied bits and pieces up. Here is the patch.

I converted the .123 to 0.123 as this appears to make things a bit more readable in the calculations, but otherwise it was fairly much just following the coder suggestions.

Comments

nhwebworker’s picture

Assigned: Unassigned » nhwebworker
alan d.’s picture

Title: Code clean up » Code clean up & removed date_api dependency
StatusFileSize
new37.12 KB

One of the changes created an error, this was reverted.

An unused hour variable was being passed around and this has been removed.

With the newer features of Drupal 7, the Date module should no longer be required. This patch removes the dependency.

Default timzone:

$timezone = variable_get('almanac_zoneoffset', date_default_timezone_get());

Current timestamp:

  $utc_time = date_make_date('now', 'UTC');
  $utc_time_array = date_array($utc_time);
  $utc_timestamp = $utc_time_array["0"];

// this should be the same as this. Potential PHP warning if  timezone is not set
  $utc_timestamp = date('U');

Current datetime object & offset

  $location_time = date_make_date('now', $timezone);

  // Equal to  

  // Create a DateTime object from the timestamp & set the timezone.
  $location_time = date_create();
  date_timezone_set($location_time, timezone_open($timezone));

  // Same object so....
  $gmtoffset = $location_time->getOffset();

Timezone options list

  $form['almanac_zoneoffset'] = array(
    '#title' => t('Location Time Zone'),
    '#options' => system_time_zones(),
  );

Finally, switching to format_date() rather than date_format_date()

  // Preserve the timezone as we call the native Drupal date function.
  $tz = $location_time->getTimezone();
  $formatted_ctime = format_date($location_time->format('U'), 'short', '', $tz->getName());
nhwebworker’s picture

Status: Active » Needs review

Thank you! Will review within next few days and update the release.

alan d.’s picture

Status: Needs review » Active
StatusFileSize
new41.62 KB

And theme handling improvements. Eg: Using theme('xyz') & template files

alan d.’s picture

No worries. Wanting to use this in a personal project.

Sorry for rolling everything into a huge patch, please make sure that I haven't broken anything in the output. The last patch requires update.php to be run as it defines new theming template files.

alan d.’s picture

StatusFileSize
new42.83 KB

Finally, fix to the page title:

  $items['almanac'] = array(
    'title' => 'Almanac',
    'title callback' => 'almanac_title',
    'page callback' => 'almanac_page',
    'access arguments' => array('access almanac'),
    // MENU_CALLBACK no longer sets the page title
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );

/**
 * Title callback for almanac page.
 *
 * @return
 *   The location name to be used as the page title.
 */
function almanac_title() {
  return t('Almanac for !location', array('!location' => variable_get('almanac_locationname', 'Bucuresti')));
}
alan d.’s picture

Title: Code clean up & removed date_api dependency » Code clean up, removed date_api dependency, moon phases, ....
Status: Active » Needs review
StatusFileSize
new52.79 KB

Added moon phases functionality, and more consistent timezone usage. Testing against dates / times from http://www.timeanddate.com/

alan d.’s picture

StatusFileSize
new10.86 KB

Changes are getting so massive, here is a zip of the above changes :) Maybe a bit easier to review.

Note that I'm using date_sun_info(), for PHP 5 >= 5.1.2. The Drupal 7 PHP min requirement is PHP 5.2.5

alan d.’s picture

Title: Code clean up, removed date_api dependency, moon phases, .... » New Drupal 7 version.
Priority: Normal » Critical

Bumping to critical as it would be best to implement before there are too many D7 users.....

I could list the changes, but it is a complete overhaul now. The attached zip & images should speak for themselves!! [Edit: The upload field isn't working, I'll upload individually or email them through]

alan d.’s picture

StatusFileSize
new22.59 KB
new64.04 KB
new44.84 KB
new53.77 KB

The files...

alan d.’s picture

StatusFileSize
new22.6 KB

Some minor bug fixes....

An example of the callback block. New Drupal 7 WTF, an object must be returned, otherwise the array values are merged.

function eta_almanac_callback_location() {
  $path = menu_get_item();
  switch($path['path']){
    case 'country/%':
      $country = $path['map'][1];
      $item = $country->cia_geo_coordinates_data['und'][0];
      $location = array(
        'name' => empty($item['data']['name']) ? $country->name : $item['data']['name'],
        'latitude' => $item['lat'],
        'longitude' => $item['lng'],
        // I haven't imported country timezones yet, so I am just using the default.
        'timezone' => date_default_timezone_get(),
      );
      return (object)$location;

    }
}
nhwebworker’s picture

Priority: Critical » Major

Thanks. Under review and will release soon.

alan d.’s picture

Cool. It is functional and running on a sandbox site of mine, but needs a second pair of eyes on it to make sure that nothing is broken. Almost all code, with the exception of the original moon rise code, has been tidyed, modified, or totally refactored!

The callback block hook seems strange, but it has allowed me to display data for any location automaticaly. This was the top level country page, a custom menu callback that uses data saved in the countries module. This same idea works on the sub-level pages for regions, etc.

nhwebworker’s picture

Status: Needs review » Fixed
StatusFileSize
new22.41 KB
new20.35 KB

This is an updated version of the Almanac module. A few minor updates have been made to correct the inputs to the moonrise/set algorithm. Posted here pending upload to CVS.

nhwebworker’s picture

Status: Fixed » Closed (fixed)