--- /var/www/sugargoss/modules/notifications_location/notifications_location.module	2009-04-24 11:07:24.000000000 +0800
+++ /var/www/sugargoss/sites/all/modules/notifications_location/notifications_location.module	2009-09-09 19:37:14.000000000 +0800
@@ -70,10 +70,34 @@
       $types['location'] = array(
         'event_type' => 'node',
         'title' => t('Location'),
+        'description' => t('Subscribe to content 100km around the location.'),
         'access' => 'subscribe to locations',
         'fields' => array('lid', 'distance'),
       );
+      $types['city'] = array(
+        'event_type' => 'node',
+        'title' => t('City'),
+        'description' => t('Subscribe to content in a city.'),
+        'access' => 'subscribe to locations',
+        'fields' => array('city', 'province', 'country'),
+      );
+      $types['province'] = array(
+        'event_type' => 'node',
+        'title' => t('Province'),
+        'description' => t('Subscribe to content in a province.'),
+        'access' => 'subscribe to locations',
+        'fields' => array('province', 'country'),
+      );      
+      $types['country'] = array(
+        'event_type' => 'node',
+        'title' => t('Country'),
+        'description' => t('Subscribe to content in a country.'),
+        'access' => 'subscribe to locations',
+        'fields' => array('country'),
+      );      
       return $types;
+    case 'node options':
+      return _notifications_location_node_options($arg0, $arg1);
     case 'subscription fields':
       // Information about available fields for subscriptions
       $fields['lid'] = array(
@@ -86,8 +110,37 @@
         'field' => 'distance',
         'type' => 'int',
       );
+      $fields['city'] = array(
+        'name' => t('City'),
+        'field' => 'city',
+        'type' => 'string',
+      );
+      $fields['province'] = array(
+        'name' => t('Province'),
+        'field' => 'province',
+        'type' => 'string',
+      );
+      $fields['country'] = array(
+        'name' => t('Country'),
+        'field' => 'country',
+        'type' => 'string',
+      );
       return $fields;
     case 'query':
+/*      if ($arg0 == 'event' && $arg1 == 'node' && ($node = $arg2->node) ||
+          $arg0 == 'user' && $arg1 == 'node' && ($node = $arg2)) {
+        foreach ($node->locations as $location){
+          $query[]['fields'] = array(
+              'lid' => $location[lid],
+              'distance' => 10000,
+              'city' => $location[city],
+              'province' => $location[province],
+              'country' => $location[country]);
+        }
+        dprint_r($query);
+        return $query;
+      }
+      break;*/
       if ($arg0 == 'event' && $arg1 == 'node' && ($node = $arg2->node) || $arg0 == 'user' && $arg1 == 'node' && ($node = $arg2)) {
         if (isset($node->locations)) {
           $query[]['join'] = 'LEFT JOIN {location} l ON f.intval = l.lid AND f.field = "lid"';
@@ -95,19 +148,25 @@
             $query[]['fields sql'] = earth_distance_sql((float)$location['longitude'], (float)$location['latitude'], 'l') .
               ' < (SELECT intval from {notifications_fields} where field = "distance" AND sid = s.sid) '.
               'AND l.latitude IS NOT NULL AND l.longitude IS NOT NULL';
+            $query[]['fields'] = array(
+              'city' => $location[city],
+              'province' => $location[province],
+              'country' => $location[country]);
           }
+
           return $query;
         }
       }
       break;
+
     case 'insert':
       // TODO stopgap measure; need a UI for choosing distance
       // Tricky because notifications_user_form() doesn't yet handle multiple fields
       // distance is stored in meters
-      if ($arg0->type = 'location') {
+/*      if ($arg0->type = 'location') {
         db_query("INSERT INTO {notifications_fields} (sid, field, value, intval) ".
           "VALUES (%d, 'distance', '100000', 100000)", $arg0->sid);
-      }
+      }*/
       break;
   }
 }
@@ -122,4 +181,50 @@
       notifications_delete_subscriptions(array('type' => 'location'), array('lid' => $location['lid']));
       break;
   }
-}
\ No newline at end of file
+}
+
+/**
+ * Provide location subscriptions on nodes.
+ */
+function _notifications_location_node_options($account, $node) {
+  $options = array();
+  if (notifications_content_type_enabled($node->type, 'location') && !empty($node->locations)) {
+    foreach ($node->locations as $location) {
+      $options[] = array(
+        'name' => t('Posts 100km around %name', array('%name' => $location[name])),
+        'type' => 'location',
+        'fields' => array('lid'=>$location['lid'],'distance'=>'100000'),
+      );
+    }
+  }
+  if (notifications_content_type_enabled($node->type, 'city') && !empty($node->locations)) {
+    foreach ($node->locations as $location) {
+      $options[] = array(
+        'name' => t('Posts in the city %name', array('%name' => $location['city'])),
+        'type' => 'city',
+        'fields' => array('city'=>$location['city'],'province'=>$location['province'],'country'=>$location['country']),
+      );
+    }
+  }
+  if (notifications_content_type_enabled($node->type, 'province') && !empty($node->locations)) {
+    foreach ($node->locations as $location) {
+      $options[] = array(
+        'name' => t('Posts in the province %name', array('%name' => $location['province_name'])),
+        'type' => 'province',
+        'fields' => array('province'=>$location['province'],'country'=>$location['country']),
+      );
+    }
+  }
+  if (notifications_content_type_enabled($node->type, 'country') && !empty($node->locations)) {
+    foreach ($node->locations as $location) {
+      $options[] = array(
+        'name' => t('Posts in the country %name', array('%name' => $location[country_name])),
+        'type' => 'country',
+        'fields' => array('country'=>$location['country']),
+      );
+    }
+  }
+
+  return $options;
+}
+
