You are here

function workbench_scheduler_load_schedules in Workbench Scheduler 7

Retrieve an array of schedule data.

Parameters

mixed $names: Machine name of a schedule or array of machine names (optional).

Return value

mixed An array of schedule data or boolean FALSE.

6 calls to workbench_scheduler_load_schedules()
workbench_scheduler_admin_delete_schedule in ./workbench_scheduler.admin.inc
Confirmation form for deleting a schedule.
workbench_scheduler_admin_edit_schedule in ./workbench_scheduler.admin.inc
Display a form for adding/editing a schedule.
workbench_scheduler_admin_page in ./workbench_scheduler.admin.inc
Display a table of workbench schedule for administration.
workbench_scheduler_features_export_render in ./workbench_scheduler.features.inc
Implements COMPONENT_features_export_render().
workbench_scheduler_load_type_schedules in ./workbench_scheduler.module
Return a list of the schedules for a given content type.

... See full list

3 string references to 'workbench_scheduler_load_schedules'
workbench_scheduler_delete_schedules in ./workbench_scheduler.module
Delete schedule(s) add associated data from the database.
workbench_scheduler_features_rebuild in ./workbench_scheduler.features.inc
Implements COMPONENT_features_rebuild().
workbench_scheduler_save_schedule in ./workbench_scheduler.module
Inserts/updates a schedule.

File

./workbench_scheduler.module, line 911
Content scheduling for Workbench.

Code

function workbench_scheduler_load_schedules($names = FALSE) {
  $schedules =& drupal_static(__FUNCTION__);

  // Boolean to check if only retrieving a single schedule.
  $get_single = FALSE;
  if ($schedules) {

    // Create an array of the schedules names already retrieved.
    $retrieved_names = array_keys($schedules);
  }

  // Passed machine name(s) of schedules to retrieve?
  if ($names) {

    // Passed a single schedule name?
    if (!is_array($names)) {

      // Retrieving a single.
      $get_single = $names;

      // Make it an array.
      $names = array(
        $names,
      );
    }

    // Find out which of the schedules have not already been retrieved.
    if (isset($retrieved_names)) {
      $missing_names = array_diff($names, $retrieved_names);
    }
    else {
      $missing_names = $names;
    }
  }

  // Only need to query the DB if noe schedules, no names or missing names.
  if (!$schedules || count($schedules) < 1 || !isset($missing_names) || count($missing_names) > 0) {

    // Build query to retrieve schedules.
    $schedules_query = db_select('workbench_scheduler_schedules', 'wss');

    // Left join the types table to get related content types based on.
    // Schedule machine names.
    $schedules_query
      ->leftjoin('workbench_scheduler_types', 'wst', 'wss.name = wst.name');

    // Retrieve all the fields for a schedule.
    $schedules_query
      ->fields('wss')
      ->fields('wst', array(
      'type',
    ));

    // Have missing names we want to only retrieve?
    if (isset($missing_names)) {

      // Add condition to only retrieve these schedules.
      $schedules_query
        ->condition('wss.name', $missing_names, 'IN');
    }

    // Have previously retrieved schedules?
    if (isset($retrieved_names)) {

      // Add condition not to retrieve these.
      $schedules_query
        ->condition('wss.name', $retrieved_names, 'NOT IN');
    }

    // Perform the query.
    $retrieved_schedules = $schedules_query
      ->execute();

    // Were results returned?
    if ($retrieved_schedules
      ->rowCount()) {

      // Loop through the results.
      foreach ($retrieved_schedules as $schedule) {

        // Already retrieved the schedule?
        if (isset($schedules[$schedule->name])) {

          // Yes, then just add this content type to it.
          $schedules[$schedule->name]->types[] = $schedule->type;
        }
        else {

          // No, add to schedules array.
          $schedules[$schedule->name] = $schedule;

          // Add an array for the content types.
          $schedules[$schedule->name]->types = array(
            $schedule->type,
          );

          // Do not need the type attribute.
          unset($schedules[$schedule->name]->type);
        }
      }
    }
  }

  // Have schedules to return?
  if ($schedules && count($schedules) > 0) {

    // Have a specific sub set of schedules ?
    if ($names) {

      // Flip names to make it assoc.
      $names = array_flip($names);

      // Take only a sub section of the returned schedules.
      $return_schedules = array_intersect_key($schedules, $names);

      // Only want a single schedule?
      if ($get_single) {

        // Is it present in the array?
        if (isset($return_schedules[$get_single])) {

          // Return just that schedule.
          return $return_schedules[$get_single];
        }
      }
      elseif (count($return_schedules) > 0) {
        return $return_schedules;
      }
    }
    else {

      // Return all of the retrieved schedules.
      return $schedules;
    }
  }

  // No schedules fetched, return boolean FALSE.
  return FALSE;
}