Support Auth0.
[readifood.git] / lib / functions.php
index fe442f8..eda4153 100644 (file)
     return array($name, $id, $args);
   }
 
+  function pagination() {
+    $first_page = 1;
+    $per_page = $GLOBALS['default_page_size'];
+
+    parse_str($_SERVER['QUERY_STRING'], $params);
+    if (array_key_exists('page', $params)) if (is_numeric($params['page'])) $first_page = $params['page'];
+    if (array_key_exists('size', $params)) if (is_numeric($params['size'])) $per_page = $params['size'];
+
+    return array($first_page, $per_page);
+  }
+
+  function page_link($alt, $n, $cur, $max, $size) {
+    $links = array();
+    if ($n < 1 || $n == $cur || $n > $max) return $alt;
+    $params = array('page' => $n);
+    if ($size != $GLOBALS['default_page_size']) $params['size'] = $size;
+    $url = http_build_query($params);
+    return "<a href=\"?$url\">$alt</a> ";
+  }
+
+  function show_pagination($pager, $n = 5) {
+    if (! $pager->haveToPaginate()) return;
+
+    list($first_page, $per_page) = pagination();
+
+    $pages = ceil($pager->getNbResults() / $per_page);
+
+    /* Highlight the fact we skipped some pages. */
+    $linked_pages = $pager->getLinks($n);
+    $first_link = $linked_pages[0];
+    $last_link = end($linked_pages);
+
+    $links = array();
+    $links[] = page_link('First', 1, $first_page, $pages, $per_page);
+    $links[] = page_link('Previous', $first_page - 1, $first_page, $pages, $per_page);
+    if ($first_link > 1) $links[] = page_link('...', $first_page, $pages, $per_page);
+    foreach ($linked_pages as $link) $links[] = page_link($link, $link, $first_page, $pages, $per_page);
+    if ($last_link < $pages) $links[] = page_link('...', $first_page, $pages, $per_page);
+    $links[] = page_link('Next', $first_page + 1, $first_page, $pages, $per_page);
+    $links[] = page_link('Last', $pages, $first_page, $pages, $per_page);
+
+    echo "<p>Page: ";
+    echo implode(' / ', $links);
+    echo "</p>\n";
+  }
+
   function get_city_by_name($name, $postcode_area = null, $verbose = true) {
     $q = new CityQuery;
 
     return $order_ids;
   }
 
-  function get_contact_orders($contact, $state_mask = null) {
+  function get_beneficiary_orders($contact, $state_mask = null) {
     $q = new OrderQuery;
     $q->filterByBeneficiaryId($contact->getId());
     if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
     return $q->orderByDate()->find();
   }
 
+  function get_requester_orders($contact, $state_mask = null) {
+    $q = new OrderQuery;
+    $q->filterByRequesterId($contact->getId());
+    if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
+    return $q->orderByDate()->find();
+  }
+
+  function get_contact_orders($contact, $state_mask = null) {
+    $q = new OrderQuery;
+    $q->filterByBeneficiaryId($contact->getId())->_or()->filterByRequesterId($contact->getId());
+    if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
+    return $q->orderByDate()->find();
+  }
+
   function get_user_by_contact_id($id, $verbose = true) {
     $q = new UserQuery;
     $user = $q->findOneByContactId($id);
   function get_order_parcel_string($order) {
     global $parcel_sizes, $parcel_contents;
 
-    $parcel_size = "";
+    $parcel_size = null;
     for ($i = 0 ; $i < count($parcel_sizes); $i++) {
       if ($order->getParcel() & (1 << $i)) {
         $parcel_size = $parcel_sizes[$i];
       if ($order->getParcel() & (1 << $i)) $selected[] = $parcel_contents[$i];
     }
 
-    return implode(": ", array($parcel_size, implode(", ", $selected)));
+    $ret = implode(": ", array($parcel_size, implode(", ", $selected)));
+    $ret = preg_replace('/^: /', '', $ret);
+    $ret = preg_replace('/: $/', '', $ret);
+
+    return $ret;
   }
 
   function get_order_displayname($order) {
     return sprintf("<span class=\"small\">%s</span> on %s", get_order_parcel_string($order), $order->getDate());
   }
 
+  function get_order_state_string($order_state = null) {
+    global $states;
+
+    if (is_null($order_state)) return null;
+
+    for ($i = 0; $i < count($states); $i++) {
+      if ($order_state->getState() & (1 << $i)) {
+        return $states[$i];
+      }
+    }
+
+    return "unknown";
+  }
+
+  function get_order_state($order) {
+    $q = new OrderStateQuery();
+    return $q->filterByOrderId($order->getId())->orderByUpdated('desc')->findOne();
+  }
+
   function get_order_summary($order) {
-    $ret = "Order " . $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order);
+    $ret = "Order ";
+    $order_state = get_order_state($order);
+    if ($order_state) $ret = "<strong>" . ucfirst(get_order_state_string($order_state)) . "</strong> order ";
+    $ret .= $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order);
 
     if (check_admin(1)) $ret .= " " . $order->getDeleteLink();
 
     return $q->findOneById($address->getAreaId());
   }
 
+  function get_address_map_link($address) {
+    $postcode = trim($address->getPostcode());
+    if ($postcode) {
+      # mrt=loc specifies a location search.
+      $map = "maps.google.co.uk/maps?q=" . urlencode($postcode) . "&mrt=loc";
+      $url = "http://$map";
+      # output=embed allows display in an iframe.
+      # iwloc=near hides the popup window for the embedded view.
+      $embed = $GLOBALS['http'] . "://$map&output=embed&iwloc=near";
+      $html = " ";
+      $html .= get_small_link_with_id("map", "Map", $url);
+      $html .= "<script>\n  $(function() {\n";
+      $html .= "    var x = 0;\n";
+      $html .= "    var y = 0;\n";
+      $html .= "    var loaded = false;\n";
+      $html .= "    $(\"#map\").hover(function(e) {\n";
+      $html .= "      x = $(this).outerWidth();\n";
+      $html .= "      y = $(this).outerHeight() / 2;\n";
+      $html .= "      $(\"#popup\").css(\"left\", e.pageX + x).css(\"top\", e.pageY + y);;\n";
+      $html .= "      $(\"#popup\").show();\n";
+      $html .= "      if (! loaded) {\n";
+      $html .= "        $(\"#popup\").html(\"<iframe width='100%' height='100%' src='$embed'></iframe>\");\n";
+      $html .= "        loaded = true;\n";
+      $html .= "      }\n";
+      $html .= "    },function() {\n";
+      $html .= "      $(\"#popup\").hide();\n";
+      $html .= "    })\n";
+      $html .= "  });</script>";
+      return $html;
+    }
+  }
+
   function get_contact_address($contact) {
     $q = new AddressQuery;
     return $q->findOneById($contact->getAddressId());
 
   function show_role_form($role, $roles) {
     for ($i = 0; $i < count($roles); $i++) {
-      echo " <input type=\"checkbox\" name=\"role_$i\"";
+      echo " <input type=\"checkbox\" id=\"role_$i\" name=\"role_$i\"";
       if ($role & (1 << $i)) echo " checked";
-      echo ">$roles[$i]\n";
+      echo "><label for=\"role_$i\">$roles[$i]</label>\n";
     }
   }
 
   }
 
   function show_date_form($name, $date = null) {
-    $past = 60;
-    $future = 60;
-    echo "<select name=\"$name\">\n";
-    $now = date('Y-m-d', time());
-    list($y, $m, $d) = explode('-', $now);
-    $today = mktime(0, 0, 0, $m, $d, $y);
-    if (isset($date)) {
-      list($y, $m, $d) = explode('-', $date);
-      $then = mktime(0, 0, 0, $m, $d, $y);
-      if ($then < $today - 86400 * $past || $then > $today + 86400 * $future) {
-        option($name, $date, date('l j F Y', $then), $date);
-      }
+    if (! isset($date)) $date = date('Y-m-d', time());
+    datepicker($name, $date);
+  }
+
+  function validate_postcode($postcode, &$outward = null, &$inward = null) {
+    /*
+      Valid postcode formats (BS7666):
+
+        AN NLL
+        ABN NLL
+        ANN NLL
+        ABNN NLL
+        ABND NLL
+        ANC NLL
+
+      Where N is a number; A is a letter not including Q, V, X;
+      B is a letter not including I, J, Z; C is a letter from the set
+      ABCDEFGHJKSTUW; D is a letter from the set ABEHMNPRVWXY;
+      L is a letter from the set ABDEFGHJLNPQRSTUWXYZ.
+
+      The postcode GIR 0AA is also valid.
+    */
+    $outward = $inward = null;
+
+    /* Treat blank as valid for convenience. */
+    $postcode = trim($postcode);
+    if (! $postcode) return true;
+
+    $A = '[ABCDEFGHIJKLMNOPRSTUWYZ]';
+    $B = '[ABCDEFGHKLMNOPQRSTUVWXY]';
+    $C = '[ABCDEFGHJKSTUW]';
+    $D = '[ABEHMNPRVWXY]';
+    $L = '[ABDEFGHJLNPQRSTUWXYZ]';
+    $N = '\d';
+    if (! preg_match("/^($A$N|$A$B$N|$A$N$N|$A$B$N$N|$A$B$N$D|$A$N$C|GIR)\s*($N$L$L)$/", $postcode, $m)) return false;
+    if ($m[1] == "GIR" && $m[2] != "0AA") return false;
+    list($ignored, $outward, $inward) = $m;
+    return true;
+  }
+
+  function format_postcode($postcode, $complain = true) {
+    if (validate_postcode($postcode, $outward, $inward)) {
+      return "$outward $inward";
     }
-    else $date = $now;
-    for ($i = -$past; $i < $future; $i++) {
-      $then = $today + 86400 * $i;
-      option($name, date('Y-m-d', $then), date('l j F Y', $then), $date);
+    if ($complain) {
+      echo "<p>Invalid postcode!</p>\n";
+      return null;
     }
-    echo "</select>\n";
-    return;
-    if (! isset($date)) $date = date('Y-m-d');
-    list($y, $m, $d) = iso8601_to_ymd($date);
+  }
 
-    echo "Year: <input name=\"$name" . "_y\" value=\"$y\" size=4 maxlen=4> ";
-    echo "Month: <input name=\"$name" . "_m\" value=\"$m\" size=2 maxlen=2> ";
-    echo "Day: <input name=\"$name" . "_d\" value=\"$d\" size=2 maxlen=2> ";
+  function get_small_link_with_id() {
+    /* Args are <id>, <alt text>, <format>, [<stuff> ...] */
+    $args = func_get_args();
+    $id = array_shift($args);
+    if (isset($id)) $id = " id=\"$id\"";
+    $html = htmlspecialchars(array_shift($args));
+    $url = array_shift($args);
+    return vsprintf("<a$id class=\"small noprint\" href=\"$url\">$html</a>\n", $args);
   }
 
   function get_small_link() {
     /* Args are <alt text>, <format>, [<stuff> ...] */
     $args = func_get_args();
-    $html = htmlspecialchars(array_shift($args));
-    $url = array_shift($args);
-    return vsprintf("<a class=\"small noprint\" href=\"$url\">$html</a>\n", $args);
+    array_unshift($args, null);
+    return call_user_func_array("get_small_link_with_id", $args);
   }
 
   function small_link() {
     echo call_user_func_array("get_small_link", func_get_args());
   }
 
-  include_once("$lib_root/admin.php");
-  include_once("$lib_root/forms.php");
+  function check_dates($description, $from, $to, $mandatory_from = true, $mandatory_to = true) {
+    $Description = ucfirst($description);
+    if ($from || $mandatory_from) {
+      list($y, $m, $d) = explode('-', $from);
+      if (! checkdate($m, $d, $y)) {
+        echo "<p>Invalid $description start date!</p>\n";
+        return false;
+      }
+      $start = mktime(0, 0, 0, $m, $d, $y);
+    }
+    else $start = 0;
+
+    if ($to || $mandatory_to) {
+      list($y, $m, $d) = explode('-', $to);
+      if (! checkdate($m, $d, $y)) {
+        echo "<p>Invalid $description end date!</p>\n";
+        return false;
+      }
+      $end = mktime(0, 0, 0, $m, $d, $y);
+    }
+    else $end = PHP_INT_MAX;
+
+    if ($end < $start) {
+      echo "<p>$Description end date is earlier than start date!</p>\n";
+      return false;
+    }
+
+    return true;
+  }
+
+  include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "admin.php")));
+  include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "auth0.php")));
+  include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "forms.php")));
 
 ?>