Tidy up forms.
[readifood.git] / lib / city.php
1 <?php
2
3   /* XXX: Show links to other objects.  Don't show cities by default. */
4
5   if ($_POST['city_name']) {
6     $id = add_city($_POST['city_name'], $_POST['city_postcode_area']);
7     if ($id !== false) {
8       echo "<p>Added city.</p>\n";
9       $parameters = array($_POST['city_name'], $id);
10     }
11   }
12
13   function show_cities($offset, $per_page, $name = null) {
14     echo "<p>Cities:";
15     $q = new CityQuery;
16     if (isset($name)) $q->filterByName($name);
17     $p = $q->paginate($offset, $per_page);
18     if (count($p)) {
19       foreach ($p as $city) {
20         echo "<br>\nCity: " . $city->getStrongLink(get_city_displayname($city));
21         $n = $city->getName();
22         $i = $city->getId();
23         echo " " . get_small_link("Areas", "/area/in/city/%s/%d", $n, $i);
24         echo " " . get_small_link("Contacts", "/contact/in/city/%s/%d", $n, $i);
25         echo " " . get_small_link("Donations", "/donation/in/city/%s/%d", $n, $i);
26         echo " " . get_small_link("Orders", "/order/in/city/%s/%d", $n, $i);
27         if (check_admin(1)) {
28           echo " " . $city->getDeleteLink();
29         }
30       }
31     }
32     else echo " none";
33     echo "</p>\n";
34   }
35
36   function show_new_city_form() {
37     if (! check_admin(1)) return;
38
39     form("noprint standout");
40     echo "<p>Add a new city <input name=\"city_name\">\n";
41     echo "with postcode prefix <input name=\"city_postcode_area\" size=4 maxlength=4>\n";
42     echo "<input type=\"submit\" value=\"Add\"></p>\n";
43   }
44
45   function add_city($name, $postcode) {
46     if (! check_admin(1, "add a city")) return;
47
48     if (preg_match('/^([A-Za-z]+)/', $postcode, $m)) {
49       $prefix = strtoupper($m[1]);
50     }
51     else {
52       echo "<p>Invalid postcode prefix!</p>\n";
53       return false;
54     }
55
56     $city = get_city_by_name($name, $prefix, false);
57     if ($city) {
58       echo "<p>$name already exists!</p>\n";
59       show_city($name, $city->getId());
60       return false;
61     }
62
63     $city = new City;
64     $city->setName($name);
65     $city->setPostcodeArea($prefix);
66
67     try {
68       $city->save();
69     }
70     catch (Exception $e) {
71       echo "<p>Error adding $name!</p>\n";
72       /* XXX: Why? */
73       return false;
74     }
75
76     return $city->getId();
77   }
78
79   function delete_city($name, $id = null) {
80     if (! check_admin(1, "delete a city")) return;
81
82     if (isset($id)) $city = get_city_by_id($id);
83     else $city = get_city_by_name($name);
84     if (! $city) return false;
85
86     try {
87       $city->delete();
88       echo "<p>Deleted city.</p>\n";
89     }
90     catch (Exception $e) {
91       echo "<p>Error deleting $name!</p>\n";
92       /* XXX: Why? Check for addresses in use... */
93       return false;
94     }
95
96     return true;
97   }
98
99   function show_city($name, $id = null) {
100     if (isset($id)) $city = get_city_by_id($id);
101     else $city = get_city_by_name($name);
102     if (! $city) return;
103
104     echo "<p>City: <span class=\"strong\">" . get_city_displayname($city) . "</span>";
105     $n = $city->getName();
106     $i = $city->getId();
107     echo " " . get_small_link("Areas", "/area/in/city/%s/%d", $n, $i);
108     echo " " . get_small_link("Contacts", "/contact/in/city/%s/%d", $n, $i);
109     echo " " . get_small_link("Donations", "/donation/in/city/%s/%d", $n, $i);
110     echo " " . get_small_link("Orders", "/order/in/city/%s/%d", $n, $i);
111     if (check_admin(1)) {
112       echo " " . $city->getDeleteLink();
113     }
114
115     $q = new AreaQuery;
116     $areas = $q->filterByCityId($city->getId())->find();
117     if (count($areas)) {
118       foreach ($areas as $area) {
119         echo "<br>\nArea: " . $area->getLink();
120       }
121     }
122
123     echo "</p>\n";
124   }
125
126   list($name, $id, $args) = parse_parameters($parameters);
127   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
128   if (count($args)) {
129     switch ($args[0]) {
130       case "delete":
131         delete_city($name, $id);
132       break;
133
134       default:
135         show_cities(0, 10, $name);
136         show_new_city_form();
137       break;
138     }
139   }
140   else if (isset($name)) show_city($name, $id);
141   else show_cities(0, 10);
142
143   if (count($parameters)) echo "<p class=\"noprint standout\">Show all <a href=\"/city\">cities</a></p>\n";
144   show_new_city_form();
145
146   # XXX: Format URL in branch case...
147   if (count($parameters))
148   echo "<p>Show all <a href=\"/city\">cities</a></p>\n";
149 ?>