Added contact offers.
[readifood.git] / lib / contact.php
index 25aab58..13456c8 100644 (file)
@@ -1,10 +1,12 @@
 <?php
 
   if (isset($_POST['show_add_contact'])) {
+    set_last_selected("city_id", $_POST['city_id']);
     $city_id = $_POST['city_id'];
     show_new_contact_form($city_id);
   }
   else if (isset($_POST['add_contact'])) {
+    set_last_selected("area_id", $_POST['area_id']);
     $id = add_contact($displayname);
     if ($id !== false) {
       echo "<p>Added contact.</p>\n";
     }
   }
   else if ($_POST['search_contact']) {
-    header(sprintf("Location: http%s://%s/%s/search/%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($_POST['search_contact'])));
+    $search_options = array();
+    if ($_POST['search_by_notes']) array_push($search_options, 'notes');
+    if ($_POST['search_by_phone']) array_push($search_options, 'phone');
+    if (count($search_options)) $search_param = sprintf("%s/", implode('+', $search_options));
+    else $search_param = '';
+    header(sprintf("Location: http%s://%s/%s/search/%s%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $search_param, urlencode($_POST['search_contact'])));
     exit;
   }
   else if ($_POST['show_in_area']) {
+    set_last_selected("area_id", $_POST['area_id']);
     $q = new AreaQuery;
     $area = $q->findOneById($_POST['area_id']);
     header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
     exit;
   }
   else if ($_POST['show_in_city']) {
+    set_last_selected("city_id", $_POST['city_id']);
     $q = new CityQuery;
     $city = $q->findOneById($_POST['city_id']);
     header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
     echo ", " . $city->getLink(get_city_displayname($city));
   }
 
-  function show_contacts($offset, $per_page, $address_ids) {
+  function show_contacts($address_ids) {
+    list($first_page, $per_page) = pagination();
     $q = new ContactQuery;
-    $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->find();
+    $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->orderById('desc')->paginate($first_page, $per_page);
     if (count($contacts)) {
       foreach ($contacts as $contact) show_contact_summary($contact);
+      show_pagination($contacts);
     }
     else echo " none";
   }
 
-  function search_contacts($offset, $per_page, $search) {
+  function search_contacts($search, $notes = false, $phone = false) {
+    list($first_page, $per_page) = pagination();
     $q = new ContactQuery;
-    $contacts = $q->filterByDisplayname("%$search%")->find();
-    echo "<p>Contacts matching '" . htmlspecialchars($search) . "':";
+    if ($phone) $q->filterByTelephone1("%$search%")->_or()->filterByTelephone2("%$search%");
+    else $q->filterByDisplayname("%$search%");
+    if ($notes) $q->_or()->filterByNotes("%$search%");
+    $contacts = $q->orderByForename()->orderBySurname()->orderById('desc')->paginate($first_page, $per_page);
+    echo "<p>Contacts matching " . ($notes ? "notes " : "") . ($phone ? "telephone " : "") . "'" . htmlspecialchars($search) . "':";
     if (count($contacts)) {
       foreach ($contacts as $contact) show_contact_summary($contact);
+      show_pagination($contacts);
     }
     else echo "none";
     echo "</p>\n";
+
+    $address_ids = array();
+    if (! $phone) {
+      $q = new AddressQuery;
+      $addresses = $q->filterByLine("%$search%")->find();
+      foreach ($addresses as $address) $address_ids[] = $address->getId();
+      $q = new AddressQuery;
+      $addresses = $q->filterByPostcode("%$search%")->find();
+      foreach ($addresses as $address) $address_ids[] = $address->getId();
+
+      $q = new ContactQuery;
+      $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->orderById('desc')->paginate($first_page, $per_page);
+      echo "<p>Contacts in address '" . htmlspecialchars($search) . "':";
+      if (count($contacts)) {
+        foreach ($contacts as $contact) show_contact_summary($contact);
+        show_pagination($contacts);
+      }
+      else echo "none";
+    }
+    echo "</p>\n";
   }
 
-  function show_city_contacts($offset, $per_page, $city_name, $city_id = null) {
+  function show_city_contacts($city_name, $city_id = null) {
     if (isset($city_id)) $city = get_city_by_id($city_id);
     else if ($city_name) $city = get_city_by_name($city_name);
     if ($city) {
       foreach ($addresses as $address) $address_ids[] = $address->getId();
 
       echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
-      return show_contacts($offset, $per_page, $address_ids);
+      return show_contacts($address_ids);
     }
     else echo "<p>No such city!</p>\n";
   }
 
-  function show_area_contacts($offset, $per_page, $area_name, $area_id = null) {
+  function show_area_contacts($area_name, $area_id = null) {
     if (isset($area_id)) $area = get_area_by_id($area_id);
     else if ($area_name) $area = get_area_by_name($area_name);
     if ($area) {
       foreach ($addresses as $address) $address_ids[] = $address->getId();
 
       echo "<p>Contacts in area " . $area->getLink() . ":";
-      return show_contacts($offset, $per_page, $address_ids);
+      return show_contacts($address_ids);
     }
     else echo "<p>No such area!</p>\n";
   }
   function show_contact_search_form() {
     echo "<p>Search for contacts:";
     input("search_contact");
+    echo "<input type=\"checkbox\" id=\"search_by_phone\" name=\"search_by_phone\">";
+    echo "<label for=\"search_by_phone\">by telephone</label>\n";
+    echo "<input type=\"checkbox\" id=\"search_by_notes\" name=\"search_by_notes\">";
+    echo "<label for=\"search_by_notes\">by notes</label>\n";
     echo "<input type=\"submit\" value=\"Search\">\n";
+    echo "</p>\n";
   }
 
   function show_contact_forms($city_id) {
   }
 
   function show_contact_form($contact = null, $new = false) {
-    global $contact_roles, $parcel_sizes, $parcel_contents;
+    global $contact_roles, $parcel_sizes, $parcel_contents, $offers;
 
     if (! $contact) $contact = new Contact;
     else if ($contact->getRole() & ($GLOBALS['ROLE_BENEFICIARY'] | $GLOBALS['ROLE_REQUESTER'])) {
     /* Postcode. */
     echo "<tr>\n";
     echo "  <td>Postcode</td>\n";
-    echo "  <td>"; input("postcode", $address->getPostcode()); echo get_address_map_link($address); echo "</td>\n";
+    $postcode = $address->getPostcode();
+    if (validate_postcode($postcode)) {
+      echo "  <td>"; input("postcode", $postcode); echo get_address_map_link($address); echo "</td>\n";
+    }
+    else {
+      echo "  <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
+    }
     echo "</tr>\n";
 
     /* Telephone. */
     echo "  <td>";
     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
       if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
-      echo "  <input type=\"checkbox\" name=\"parcel_$i\"";
+      echo "  <input type=\"checkbox\" id=\"parcel_$i\" name=\"parcel_$i\"";
       if ($contact->getParcel() & (1 << $i)) echo " checked";
-      echo ">$parcel_contents[$i]\n";
+      echo "><label for=\"parcel_$i\">$parcel_contents[$i]</label>\n";
     }
     echo "</td>\n";
     echo "</tr>\n";
     echo "  <td>Notes</td>\n";
     echo "  <td>"; textarea("notes", $contact->getNotes()); echo "</td>\n";
     echo "</tr>\n";
+
+    /* Offers. */
+    $available_offers = get_available_offers($contact);
+    if (count($available_offers)) {
+      echo "<tr>\n";
+      echo "  <td colspan=2><strong>Offers</strong></td>\n";
+      echo "</tr>\n";
+
+      $offer_states = Array();
+      foreach ($available_offers as $offer) {
+        $offer_id = $offer->getId();
+        $state = get_offer_state($contact, $offer);
+        $state_id = is_null($state) ? $GLOBALS['OFFER_ELIGIBLE'] : $state->getState();
+        $offer_states[$offer_id] = $state_id;
+        echo "<tr>\n";
+        echo "  <td>" . $offer->getDescription() . "</td>\n";
+        echo "  <td><select name=\"offer$offer_id\">\n";
+        for ($i = 0; $i < count($offers); $i++) {
+          option("offer$offer_id", 1 << $i, $offers[$i], $state_id == 1 << $i);
+        }
+        echo "</select></td>\n";
+        echo "</tr>\n";
+      }
+      hidden("offer_states", urlencode(json_encode($offer_states)));
+    }
+    else {
+      echo "<tr>\n";
+      echo "  <td colspan=2><strong>No available offers</strong></td>\n";
+      echo "</tr>\n";
+      hidden("offer_states", "{}");
+    }
   }
 
   function show_new_contact_form($city_id = null) {
     /* Get address. */
     $area_id = $_POST['area_id'];
     $line = $_POST['address'];
-    $postcode = $_POST['postcode'];
+    $postcode = trim($_POST['postcode']);
+    if ($postcode) {
+      $postcode = format_postcode($_POST['postcode'], true);
+      if (! $postcode) return false;
+    }
     $q = new AddressQuery;
     /* XXX: Finding by area properly? */
     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
       return false;
     }
 
+    if ($_POST['offer_states']) {
+      try {
+        $offer_states = json_decode(urldecode($_POST['offer_states']));
+      }
+      catch (Exception $e) {
+        echo "<p>Error getting offer states JSON.</p>\n";
+        return false;
+      }
+
+      $now = time();
+      foreach ($offer_states as $offer_id => $state_id) {
+        $new_state_id = strval($_POST["offer$offer_id"]);
+        if ($new_state_id == "") continue;
+        if ($new_state_id == strval($state_id)) continue;
+
+        $offer_state = new OfferState;
+        $offer_state->setUpdated($now);
+        $offer_state->setOfferId($offer_id);
+        $offer_state->setUserId($GLOBALS['user_id']);
+        $offer_state->setContactId($contact->getId());
+        $offer_state->setState($new_state_id);
+
+        try {
+          $offer_state->save();
+        }
+        catch (Exception $e) {
+          echo "<p>Error updating offer.</p>\n";
+          echo "<p>" . $e->getMessage() . "</p>\n";
+          return false;
+        }
+      }
+    }
+
     return true;
   }
 
 
     $contact = new Contact;
     if (! update_contact($contact, $area_id, true)) return false;
+    $name = $contact->getDisplayname();
     return $contact->getId();
   }
 
           $area = $q->findOneById($area_id);
           $city = get_area_city($area);
           if ($city) $city_id = $city->getId();
-          show_area_contacts(0, 10, $parameters[2], $area_id);
+          show_area_contacts($parameters[2], $area_id);
         break;
 
         case "city":
           $_POST['city_id'] = $city_id;
           $q = new CityQuery;
           $city = $q->findOneById($city_id);
-          show_city_contacts(0, 10, $parameters[2], $city_id);
+          show_city_contacts($parameters[2], $city_id);
         break;
       }
     }
     else if ($parameters[0] == "search") {
-      search_contacts(0, 10, $parameters[1]);
+      $available_search_options = array('notes', 'phone');
+      $search_options = explode('+', $parameters[1]);
+      if (count(array_intersect($available_search_options, $search_options)) == count($search_options)) {
+        $notes = in_array("notes", $search_options);
+        $phone = in_array("phone", $search_options);
+        search_contacts(urldecode($parameters[2]), $notes, $phone);
+      }
+      else search_contacts(urldecode($parameters[1]));
     }
   }
   list($name, $id, $args) = parse_parameters($parameters);