You are here

function getlocations_fields_load_locations in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_fields/getlocations_fields.module \getlocations_fields_load_locations()

Parameters

int $id: Primary identifier of the content type the location is linked to

string $key: The name of the primary identifier

Return value

array $locations The locations associated with the content type

3 calls to getlocations_fields_load_locations()
getlocations_fields_field_formatter_view in modules/getlocations_fields/getlocations_fields.module
Implements hook_field_formatter_view(). Build a renderable array for a field value.
getlocations_fields_save_locations in modules/getlocations_fields/getlocations_fields.module
getlocations_load_locations in ./getlocations.module
Function to fetch locations

File

modules/getlocations_fields/getlocations_fields.module, line 2394
getlocations_fields.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_fields_load_locations($id, $key = 'vid', $field_name = '') {

  // $key can be vid, nid, uid, tid, cid
  global $user;
  $roles = $user->roles;
  $locations = array();
  if ($id) {
    module_load_include('inc', 'getlocations_fields', 'getlocations_fields.functions');
    $getlocations_fields_defaults = getlocations_fields_defaults();
    $fields = array(
      'glid',
      'name',
      'street',
      'additional',
      'city',
      'province',
      'postal_code',
      'country',
      'address',
      'latitude',
      'longitude',
      'marker',
    );
    if (getlocations_fields_column_check('data')) {
      $fields[] = 'data';
    }
    $query = db_select('getlocations_fields', 'f');
    $query
      ->fields('f', $fields);
    $query
      ->fields('e', array(
      'nid',
      'vid',
      'uid',
      'tid',
      'cid',
      'field_name',
    ));
    $query
      ->join('getlocations_fields_entities', 'e', 'f.glid=e.glid');

    // we need to limit on status for nid, vid, uid and cid but allow admin
    if (!in_array('administrator', $roles)) {
      if ($key == 'nid') {
        $query
          ->join('node', 'n', 'n.nid = e.nid');
        $query
          ->condition('e.nid', $id)
          ->condition('n.status', 0, '>');
      }
      elseif ($key == 'vid') {
        $query
          ->join('node_revision', 'n', 'n.vid = e.vid');
        $query
          ->condition('e.vid', $id)
          ->condition('n.status', 0, '>');
      }
      elseif ($key == 'uid') {
        $query
          ->join('users', 'n', 'n.uid = e.uid');
        $query
          ->condition('e.uid', $id)
          ->condition('n.status', 0, '>');
      }
      elseif (module_exists('comment') && $key == 'cid') {
        $query
          ->join('comment', 'n', 'n.cid = e.cid');
        $query
          ->condition('e.cid', $id)
          ->condition('n.status', 0, '>');
      }
    }
    if ($field_name) {
      $query
        ->join('field_config', 'c', 'e.field_name = c.field_name');
      $query
        ->condition('e.field_name', $field_name);
    }
    $query
      ->condition('e.' . $key, $id);
    $rows = $query
      ->execute();
    $ct = 0;
    foreach ($rows as $row) {
      $locations[$ct]['glid'] = $row->glid;
      $locations[$ct]['lid'] = $row->glid;
      $locations[$ct]['name'] = $row->name;
      $locations[$ct]['street'] = $row->street;
      $locations[$ct]['additional'] = $row->additional;
      $locations[$ct]['city'] = $row->city;
      $locations[$ct]['province'] = $row->province;
      $locations[$ct]['province_name'] = $row->province;
      $locations[$ct]['postal_code'] = $row->postal_code;
      if ($getlocations_fields_defaults['country_full'] && drupal_strlen($row->country) == 2) {
        $locations[$ct]['country_name'] = getlocations_get_country_name($row->country);
      }
      else {
        $locations[$ct]['country_name'] = $row->country;
      }
      $locations[$ct]['country'] = $row->country;
      $locations[$ct]['address'] = $row->address;
      $locations[$ct]['latitude'] = $row->latitude;
      $locations[$ct]['longitude'] = $row->longitude;
      $locations[$ct]['marker'] = $row->marker;
      $data = isset($row->data) && !empty($row->data) ? unserialize($row->data) : '';
      $keys = getlocations_fields_data_keys('d');
      if (is_array($data) && isset($data['data']['map_settings_allow'])) {
        $map_settings_allow = $data['data']['map_settings_allow'];
      }
      else {
        $map_settings_allow = getlocations_fields_map_settings_allow();
      }
      foreach ($keys as $key => $dval) {
        $locations[$ct][$key] = $dval;
        if (is_array($data) && isset($data['data'][$key])) {

          #$do = TRUE;
          if (!$map_settings_allow && ($key == 'mapzoom' || $key == 'map_maptype')) {

            #$do = FALSE;
            continue;
          }

          #if ($do) {
          $locations[$ct][$key] = $data['data'][$key];

          #}
        }
      }

      // what3words
      $what3words_lic = variable_get('getlocations_what3words_lic', array(
        'key' => '',
        'url' => 'http://api.what3words.com',
      ));
      if ($what3words_lic['key'] && is_array($data) && isset($data['data']['what3words'])) {
        $locations[$ct]['what3words'] = $data['data']['what3words'];
      }
      $locations[$ct]['nid'] = $row->nid;
      $locations[$ct]['vid'] = $row->vid;
      $locations[$ct]['uid'] = $row->uid;
      $locations[$ct]['tid'] = $row->tid;
      $locations[$ct]['cid'] = $row->cid;
      $locations[$ct]['field_name'] = $row->field_name;
      if ($row->nid) {
        $locations[$ct]['type'] = 'node';
      }
      elseif ($row->uid) {
        $locations[$ct]['type'] = 'user';
      }
      elseif ($row->tid) {
        $locations[$ct]['type'] = 'vocabulary';
      }
      elseif ($row->cid) {
        $locations[$ct]['type'] = 'comment';
      }
      $ct++;
    }
  }
  return $locations;
}