root/trunk/wifidog-auth/wifidog/classes/Node.php @ 705

Revision 705, 40.8 KB (checked in by benoitg, 8 years ago)

2005-08-31 Benoit Gr�goire <bock@…>

  • Node.php: Fix node creation
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2/********************************************************************\
3 * This program is free software; you can redistribute it and/or    *
4 * modify it under the terms of the GNU General Public License as   *
5 * published by the Free Software Foundation; either version 2 of   *
6 * the License, or (at your option) any later version.              *
7 *                                                                  *
8 * This program is distributed in the hope that it will be useful,  *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
11 * GNU General Public License for more details.                     *
12 *                                                                  *
13 * You should have received a copy of the GNU General Public License*
14 * along with this program; if not, contact:                        *
15 *                                                                  *
16 * Free Software Foundation           Voice:  +1-617-542-5942       *
17 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
18 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
19 *                                                                  *
20 \********************************************************************/
21/**@file Node.php
22 * @author Copyright (C) 2005 Benoit Gr�goire <bock@step.polymtl.ca>
23 */
24
25require_once BASEPATH.'include/common.php';
26require_once 'Content/ContentGroup.php';
27require_once 'User.php';
28require_once 'GisPoint.php';
29require_once 'AbstractGeocoder.php';
30
31/** Abstract a Node.  A Node is an actual physical transmitter. */
32class Node implements GenericObject
33{
34        private $mRow;
35        private $id;
36        private static $current_node_id = null;
37
38        /** Instantiate a node object
39         * @param $id The id of the requested node
40         * @return a Node object, or null if there was an error
41         */
42        static function getObject($id)
43        {
44                $object = null;
45                $object = new self($id);
46                return $object;
47        }
48
49        /** Get the current node for which the portal is displayed or to which a user is physically connected.
50         * @param $real_node_only true or false.  If true, the real physical node where the user is connected is returned, and the node set by setCurrentNode is ignored.
51         * @return a Node object, or null if it can't be found.
52         */
53        static function getCurrentNode($real_node_only = false)
54        {
55                $object = null;
56                if (self :: $current_node_id != null && $real_node_only == false)
57                {
58                        $object = new self(self :: $current_node_id);
59                }
60                else
61                {
62                        $object = self :: getCurrentRealNode();
63                }
64                return $object;
65        }
66
67        /** Set the current node where the user is to be considered connected to.  (For portal and content display purpuses, among other.
68         * @param $node Node.  The new current node.
69         * @return true  */
70        static function setCurrentNode(Node $node)
71        {
72                self :: $current_node_id = $node->GetId();
73                return true;
74        }
75
76        /** Get the current node to which a user is physically connected, if any.  This is done by an IP adress lookup against the last reported IP adress of the node
77         * @param        * @return a Node object, or null if it can't be found.
78         */
79        public static function getCurrentRealNode()
80        {
81                global $db;
82                $retval = null;
83                $sql = "SELECT node_id, last_heartbeat_ip from nodes WHERE last_heartbeat_ip='$_SERVER[REMOTE_ADDR]'";
84                $db->ExecSql($sql, $node_rows, false);
85                $num_match = count($node_rows);
86                if ($num_match == 0)
87                {
88
89                        // User is not physically connected to a node
90                        $retval = null;
91                }
92                else
93                        if ($num_match = 1)
94                        {
95                                // Only a single node matches, the user is presumed to be there
96                                $retval = new self($node_rows[0]['node_id']);
97                        }
98                        else
99                        {
100                                /* We have more than one node matching the IP (the nodes are behind the same NAT).
101                                 * We will try to discriminate by finding which node the user last authenticated against.
102                                 * If the IP matches, we can be pretty certain the user is there.
103                                 */
104                                $retval = null;
105                                $current_user = User :: getCurrentUser();
106                                if ($current_user != null)
107                                {
108                                        $current_user_id = $current_user->getId();
109                                        $_SERVER['REMOTE_ADDR'];
110                                        $sql = "SELECT node_id, last_heartbeat_ip from connections NATURAL JOIN nodes WHERE user_id='$current_user_id' ORDER BY last_updated DESC ";
111                                        $db->ExecSql($sql, $node_rows, false);
112                                        $node_row = $node_rows[0];
113                                        if ($node_row != null && $node_row['last_heartbeat_ip'] == $_SERVER['REMOTE_ADDR'])
114                                        {
115                                                $retval = new self($node_row['node_id']);
116                                        }
117                                }
118                        }
119                return $retval;
120        }
121
122        public function delete(& $errmsg)
123        {
124                $retval = false;
125                $user = User :: getCurrentUser();
126                if ($this->isOwner($user) || $user->isSuperAdmin())
127                {
128                        $errmsg = _('Access denied!');
129                }
130
131                global $db;
132                $id = $db->EscapeString($this->getId());
133                if (!$db->ExecSqlUpdate("DELETE FROM nodes WHERE node_id='{$id}'", false))
134                {
135                        $errmsg = _('Could not delete node!');
136                }
137                else
138                {
139                        $retval = true;
140                }
141
142                return $retval;
143        }
144
145        /** Create a new Node in the database
146         * @param $id The id to be given to the new node
147         * @return the newly created Node object, or null if there was an error
148         */
149        static function createNewObject()
150        {
151                global $db;
152
153                $node_id = $db->EscapeString(get_guid());
154                $object = self::createNewNode($node_id, Network::getCurrentNetwork());
155                return $object;
156        }
157
158        /** Create a new Node in the database
159         * @param $node_id The id to be given to the new node
160         * @param $network Network object.  The node's network
161         * @todo Implement network
162         * @return the newly created Node object, or null if there was an error
163         */
164        static function createNewNode($node_id, Network $network)
165        {
166                global $db;
167                $node_id = $db->EscapeString($node_id);
168                $node_deployment_status = $db->EscapeString("IN_PLANNING");
169                $node_name = _("New node");
170                if (Node :: nodeExists($node_id))
171                        throw new Exception(_('This node already exists.'));
172
173                $sql = "INSERT INTO nodes (node_id, creation_date, node_deployment_status, name) VALUES ('$node_id', NOW(),'$node_deployment_status', '$node_name')";
174
175                if (!$db->ExecSqlUpdate($sql, false))
176                {
177                        throw new Exception(_('Unable to insert new node into database!'));
178                }
179                $object = new self($node_id);
180                return $object;
181        }
182
183        /** Get an interface to pick a node.
184        * @param $user_prefix A identifier provided by the programmer to recognise it's generated html form
185        * @param $sql_additional_where Addidional where conditions to restrict the candidate objects
186        * @return html markup
187        */
188        public static function getSelectNodeUI($user_prefix, $sql_additional_where = null)
189        {
190                global $db;
191                $html = '';
192                $name = "{$user_prefix}";
193                $html .= "Node: \n";
194                $sql = "SELECT node_id, name from nodes WHERE 1=1 $sql_additional_where ORDER BY node_id";
195                $db->ExecSql($sql, $node_rows, false);
196                if ($node_rows != null)
197                {
198                        $i = 0;
199                        foreach ($node_rows as $node_row)
200                        {
201                                $tab[$i][0] = $node_row['node_id'];
202                                $tab[$i][1] = $node_row['node_id'].": ".$node_row['name'];
203                                $i ++;
204                        }
205                        $html .= FormSelectGenerator :: generateFromArray($tab, null, $name, null, false);
206                }
207                return $html;
208        }
209
210        /** Get the selected Network object.
211         * @param $user_prefix A identifier provided by the programmer to recognise it's generated form
212         * @return the node object
213         */
214        static function processSelectNodeUI($user_prefix)
215        {
216                $object = null;
217                $name = "{$user_prefix}";
218                return new self($_REQUEST[$name]);
219        }
220
221        /** Get an interface to select the deployment status
222        * @param $user_prefix A identifier provided by the programmer to recognise it's generated html form
223        * @return html markup
224        */
225        public function getSelectDeploymentStatus($user_prefix)
226        {
227                global $db;
228                $html = '';
229                $name = "{$user_prefix}";
230                $status_list = self :: getAllDeploymentStatus();
231                if ($status_list != null)
232                {
233                        $tab = array ();
234                        foreach ($status_list as $status)
235                                $tab[] = array ($status, $status);
236                        $html .= FormSelectGenerator :: generateFromArray($tab, $this->getDeploymentStatus(), $name, null, false);
237                }
238                return $html;
239        }
240
241        /** Get the selected deployment status
242         * @param $user_prefix A identifier provided by the programmer to recognise it's generated form
243         * @return the deployment status
244         */
245        public function processSelectDeploymentStatus($user_prefix)
246        {
247                $object = null;
248                $name = "{$user_prefix}";
249                return $_REQUEST[$name];
250        }
251
252        /** @param $node_id The id of the node */
253        function __construct($node_id)
254        {
255                global $db;
256
257                $node_id_str = $db->EscapeString($node_id);
258                $sql = "SELECT * FROM nodes WHERE node_id='$node_id_str'";
259                $db->ExecSqlUniqueRes($sql, $row, false);
260                if ($row == null)
261                {
262                        throw new Exception(_("The id $node_id_str could not be found in the database"));
263                }
264                $this->mRow = $row;
265                $this->mDb = & $db;
266                $this->id = $row['node_id'];
267        }
268
269        function getId()
270        {
271                return $this->mRow['node_id'];
272        }
273
274        /** Get a GisPoint object ; altide is not supported yet
275         */
276        function getGisLocation()
277        {
278                // Altitude is not supported yet
279                return new GisPoint($this->mRow['latitude'], $this->mRow['longitude'], 0);
280        }
281
282        function setGisLocation($pt)
283        {
284                if (!empty ($pt))
285                {
286                        $lat = $this->mDb->EscapeString($pt->getLatitude());
287                        $long = $this->mDb->EscapeString($pt->getLongitude());
288
289                        if (!empty ($lat) && !empty ($long))
290                                $this->mDb->ExecSqlUpdate("UPDATE nodes SET latitude = $lat, longitude = $long WHERE node_id = '{$this->getId()}'");
291                        else
292                                $this->mDb->ExecSqlUpdate("UPDATE nodes SET latitude = NULL, longitude = NULL WHERE node_id = '{$this->getId()}'");
293                        $this->refresh();
294                }
295        }
296
297        /** Return the name of the node
298         */
299        function getName()
300        {
301                return $this->mRow['name'];
302        }
303
304        function setName($name)
305        {
306                $name = $this->mDb->EscapeString($name);
307                $this->mDb->ExecSqlUpdate("UPDATE nodes SET name = '{$name}' WHERE node_id = '{$this->getId()}'");
308                $this->refresh();
309        }
310
311        function getHomePageURL()
312        {
313                return $this->mRow['home_page_url'];
314        }
315
316        function setHomePageUrl($url)
317        {
318                $url = $this->mDb->EscapeString($url);
319                $this->mDb->ExecSqlUpdate("UPDATE nodes SET home_page_url = '{$url}' WHERE node_id = '{$this->getId()}'");
320                $this->refresh();
321        }
322
323        function getDescription()
324        {
325                return $this->mRow['description'];
326        }
327
328        function setDescription($description)
329        {
330                $description = $this->mDb->EscapeString($description);
331                $this->mDb->ExecSqlUpdate("UPDATE nodes SET description = '{$description}' WHERE node_id = '{$this->getId()}'");
332                $this->refresh();
333        }
334
335        function getMapURL()
336        {
337                return $this->mRow['map_url'];
338        }
339
340        function setMapURL($url)
341        {
342                $url = $this->mDb->EscapeString($url);
343                $this->mDb->ExecSqlUpdate("UPDATE nodes SET map_url = '{$url}' WHERE node_id = '{$this->getId()}'");
344                $this->refresh();
345        }
346
347        public function getCivicNumber()
348        {
349                return $this->mRow['civic_number'];
350        }
351
352        public function setCivicNumber($civic_number)
353        {
354                $civic_number = $this->mDb->EscapeString($civic_number);
355                $this->mDb->ExecSqlUpdate("UPDATE nodes SET civic_number = '{$civic_number}' WHERE node_id = '{$this->getId()}'");
356                $this->refresh();
357        }
358
359        public function getStreetName()
360        {
361                return $this->mRow['street_name'];
362        }
363
364        public function setStreetName($street_name)
365        {
366                $street_name = $this->mDb->EscapeString($street_name);
367                $this->mDb->ExecSqlUpdate("UPDATE nodes SET street_name = '{$street_name}' WHERE node_id = '{$this->getId()}'");
368                $this->refresh();
369        }
370
371        public function getCity()
372        {
373                return $this->mRow['city'];
374        }
375
376        public function setCity($city)
377        {
378                $city = $this->mDb->EscapeString($city);
379                $this->mDb->ExecSqlUpdate("UPDATE nodes SET city = '{$city}' WHERE node_id = '{$this->getId()}'");
380                $this->refresh();
381        }
382
383        public function getProvince()
384        {
385                return $this->mRow['province'];
386        }
387
388        public function setProvince($province)
389        {
390                $province = $this->mDb->EscapeString($province);
391                $this->mDb->ExecSqlUpdate("UPDATE nodes SET province = '{$province}' WHERE node_id = '{$this->getId()}'");
392                $this->refresh();
393        }
394
395        public function getCountry()
396        {
397                return $this->mRow['country'];
398        }
399
400        protected function setCountry($country)
401        {
402                $country = $this->mDb->EscapeString($country);
403                $this->mDb->ExecSqlUpdate("UPDATE nodes SET country = '{$country}' WHERE node_id = '{$this->getId()}'");
404                $this->refresh();
405        }
406
407        public function getPostalCode()
408        {
409                return $this->mRow['postal_code'];
410        }
411
412        public function setPostalCode($postal_code)
413        {
414                $postal_code = $this->mDb->EscapeString($postal_code);
415                $this->mDb->ExecSqlUpdate("UPDATE nodes SET postal_code = '{$postal_code}' WHERE node_id = '{$this->getId()}'");
416                $this->refresh();
417        }
418
419        function getTelephone()
420        {
421                return $this->mRow['public_phone_number'];
422        }
423
424        function setTelephone($phone)
425        {
426                $phone = $this->mDb->EscapeString($phone);
427                $this->mDb->ExecSqlUpdate("UPDATE nodes SET public_phone_number = '{$phone}' WHERE node_id = '{$this->getId()}'");
428                $this->refresh();
429        }
430
431        function getTransitInfo()
432        {
433                return $this->mRow['mass_transit_info'];
434        }
435
436        function setTransitInfo($transit_info)
437        {
438                $transit_info = $this->mDb->EscapeString($transit_info);
439                $this->mDb->ExecSqlUpdate("UPDATE nodes SET mass_transit_info = '{$transit_info}' WHERE node_id = '{$this->getId()}'");
440                $this->refresh();
441        }
442
443        function getEmail()
444        {
445                return $this->mRow['public_email'];
446        }
447
448        function setEmail($email)
449        {
450                $email = $this->mDb->EscapeString($email);
451                $this->mDb->ExecSqlUpdate("UPDATE nodes SET public_email = '{$email}' WHERE node_id = '{$this->getId()}'");
452                $this->refresh();
453        }
454
455        function getDeploymentStatus()
456        {
457                return $this->mRow['node_deployment_status'];
458        }
459
460        function setDeploymentStatus($status)
461        {
462                $status = $this->mDb->EscapeString($status);
463                $this->mDb->ExecSqlUpdate("UPDATE nodes SET node_deployment_status = '{$status}' WHERE node_id = '{$this->getId()}'");
464                $this->refresh();
465        }
466
467        /** Retreives the admin interface of this object.
468         * @return The HTML fragment for this interface */
469        public function getAdminUI()
470        {
471                //TODO: Most of this code will be moved to Hotspot class when the abtraction will be completed
472
473                $html = '';
474                $html .= "<div class='admin_container'>\n";
475                $html .= "<div class='admin_class'>Node (".get_class($this)." instance)</div>\n";
476                $html .= "<h3>"._("Edit a hotspot")."</h3>\n";
477
478                // Information about the node
479                $html .= "<div class='admin_section_container'>\n";
480                $html .= "<div class='admin_section_title'>"._("Information about the node:")."</div>\n";
481
482                // Node ID
483                $value = htmlspecialchars($this->getId(), ENT_QUOTES);
484                $html .= "<div class='admin_section_container'>\n";
485                $html .= "<div class='admin_section_title'>"._("ID")." : {$value}</div>\n";
486                //$html .= "<div class='admin_section_data'>\n";
487                //$name = "node_".$this->getId()."_id";
488                //$html .= "<input type='text' readonly='' size='10' value='$value' name='$name'>\n";
489                //$html .= "</div>\n";
490                $html .= "</div>\n";
491
492                // Name
493                $html .= "<div class='admin_section_container'>\n";
494                $html .= "<div class='admin_section_title'>"._("Name")." : </div>\n";
495                $html .= "<div class='admin_section_data'>\n";
496                $name = "node_".$this->getId()."_name";
497                $value = htmlspecialchars($this->getName(), ENT_QUOTES);
498                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
499                $html .= "</div>\n";
500                $html .= "</div>\n";
501
502                // Homepage URL
503                $html .= "<div class='admin_section_container'>\n";
504                $html .= "<div class='admin_section_title'>"._("Homepage URL")." : </div>\n";
505                $html .= "<div class='admin_section_data'>\n";
506                $name = "node_".$this->getId()."_homepage_url";
507                $value = htmlspecialchars($this->getHomePageURL(), ENT_QUOTES);
508                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
509                $html .= "</div>\n";
510                $html .= "</div>\n";
511
512                // Description
513                $html .= "<div class='admin_section_container'>\n";
514                $html .= "<div class='admin_section_title'>"._("Description")." : </div>\n";
515                $html .= "<div class='admin_section_data'>\n";
516                $name = "node_".$this->getId()."_description";
517                $value = htmlspecialchars($this->getDescription(), ENT_QUOTES);
518                $html .= "<textarea cols='50' rows='5' name='$name'>$value</textarea>\n";
519                $html .= "</div>\n";
520                $html .= "</div>\n";
521
522                // Map URL
523                $html .= "<div class='admin_section_container'>\n";
524                $html .= "<div class='admin_section_title'>"._("Map URL")." : </div>\n";
525                $html .= "<div class='admin_section_data'>\n";
526                $name = "node_".$this->getId()."_map_url";
527                $value = htmlspecialchars($this->getMapURL(), ENT_QUOTES);
528                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
529                $html .= "</div>\n";
530                $html .= "</div>\n";
531
532                // Civic number
533                $html .= "<div class='admin_section_container'>\n";
534                $html .= "<div class='admin_section_title'>"._("Civic number")." : </div>\n";
535                $html .= "<div class='admin_section_data'>\n";
536                $name = "node_".$this->getId()."_civic_number";
537                $value = htmlspecialchars($this->getCivicNumber(), ENT_QUOTES);
538                $html .= "<input type='text' size ='10' value='$value' name='$name'>\n";
539                $html .= "</div>\n";
540                $html .= "</div>\n";
541
542                // Street name
543                $html .= "<div class='admin_section_container'>\n";
544                $html .= "<div class='admin_section_title'>"._("Street name")." : </div>\n";
545                $html .= "<div class='admin_section_data'>\n";
546                $name = "node_".$this->getId()."_street_name";
547                $value = htmlspecialchars($this->getStreetName(), ENT_QUOTES);
548                $html .= "<input type='text' size ='25' value='$value' name='$name'>\n";
549                $html .= "</div>\n";
550                $html .= "</div>\n";
551
552                // City
553                $html .= "<div class='admin_section_container'>\n";
554                $html .= "<div class='admin_section_title'>"._("City")." : </div>\n";
555                $html .= "<div class='admin_section_data'>\n";
556                $name = "node_".$this->getId()."_city";
557                $value = htmlspecialchars($this->getCity(), ENT_QUOTES);
558                $html .= "<input type='text' size ='25' value='$value' name='$name'>\n";
559                $html .= "</div>\n";
560                $html .= "</div>\n";
561
562                // Province
563                $html .= "<div class='admin_section_container'>\n";
564                $html .= "<div class='admin_section_title'>"._("Province / State")." : </div>\n";
565                $html .= "<div class='admin_section_data'>\n";
566                $name = "node_".$this->getId()."_province";
567                $value = htmlspecialchars($this->getProvince(), ENT_QUOTES);
568                $html .= "<input type='text' size ='15' value='$value' name='$name'>\n";
569                $html .= "</div>\n";
570                $html .= "</div>\n";
571
572                // Postal Code
573                $html .= "<div class='admin_section_container'>\n";
574                $html .= "<div class='admin_section_title'>"._("Postal code")." : </div>\n";
575                $html .= "<div class='admin_section_data'>\n";
576                $name = "node_".$this->getId()."_postal_code";
577                $value = htmlspecialchars($this->getPostalCode(), ENT_QUOTES);
578                $html .= "<input type='text' size ='10' value='$value' name='$name'>\n";
579                $html .= "</div>\n";
580                $html .= "</div>\n";
581
582                // Country
583                $html .= "<div class='admin_section_container'>\n";
584                $html .= "<div class='admin_section_title'>"._("Country")." : </div>\n";
585                $html .= "<div class='admin_section_data'>\n";
586                $name = "node_".$this->getId()."_country";
587                $value = htmlspecialchars($this->getCountry(), ENT_QUOTES);
588                $html .= "<input type='text' size ='15' value='$value' name='$name'>\n";
589                $html .= "</div>\n";
590                $html .= "</div>\n";
591
592                // Public phone #
593                $html .= "<div class='admin_section_container'>\n";
594                $html .= "<div class='admin_section_title'>"._("Public phone number")." : </div>\n";
595                $html .= "<div class='admin_section_data'>\n";
596                $name = "node_".$this->getId()."_public_phone";
597                $value = htmlspecialchars($this->getTelephone(), ENT_QUOTES);
598                $html .= "<input type='text' size ='20' value='$value' name='$name'>\n";
599                $html .= "</div>\n";
600                $html .= "</div>\n";
601
602                // Public mail
603                $html .= "<div class='admin_section_container'>\n";
604                $html .= "<div class='admin_section_title'>"._("Public email")." : </div>\n";
605                $html .= "<div class='admin_section_data'>\n";
606                $name = "node_".$this->getId()."_public_email";
607                $value = htmlspecialchars($this->getEmail(), ENT_QUOTES);
608                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
609                $html .= "</div>\n";
610                $html .= "</div>\n";
611
612                // Mass transit info
613                $html .= "<div class='admin_section_container'>\n";
614                $html .= "<div class='admin_section_title'>"._("Mass transit info")." : </div>\n";
615                $html .= "<div class='admin_section_data'>\n";
616                $name = "node_".$this->getId()."_mass_transit_info";
617                $value = htmlspecialchars($this->getTransitInfo(), ENT_QUOTES);
618                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
619                $html .= "</div>\n";
620                $html .= "</div>\n";
621
622                // Deployment status
623                $html .= "<div class='admin_section_container'>\n";
624                $html .= "<div class='admin_section_title'>"._("Node deployment status")." : </div>\n";
625                $html .= "<div class='admin_section_data'>\n";
626                $name = "node_".$this->getId()."_deployment_status";
627                $html .= self :: getSelectDeploymentStatus($name);
628                $html .= "</div>\n";
629                $html .= "</div>\n";
630
631                // End of information section
632                $html .= "</div>\n";
633
634                // Node GIS data
635                $html .= "<div class='admin_section_container'>\n";
636                $html .= "<div class='admin_section_title'>"._("GIS data")." : </div>\n";
637
638                // Build HTML form fields names & values
639                $gis_point = $this->getGisLocation();
640                $gis_lat_name = "node_".$this->getId()."_gis_latitude";
641                $gis_lat_value = htmlspecialchars($gis_point->getLatitude(), ENT_QUOTES);
642                $gis_long_name = "node_".$this->getId()."_gis_longitude";
643                $gis_long_value = htmlspecialchars($gis_point->getLongitude(), ENT_QUOTES);
644
645                $html .= "<div class='admin_section_container'>\n";
646                $html .= "<div class='admin_section_title'>"._("Latitude")." : </div>\n";
647                $html .= "<div class='admin_section_data'>\n";
648                $html .= "<input type='text' size ='15' value='$gis_lat_value' id='$gis_lat_name' name='$gis_lat_name'>\n";
649                $html .= "</div>\n";
650                $html .= "</div>\n";
651
652                $html .= "<div class='admin_section_container'>\n";
653                $html .= "<div class='admin_section_title'>"._("Longitude")." : </div>\n";
654                $html .= "<div class='admin_section_data'>\n";
655                $html .= "<input type='text' size ='15' value='$gis_long_value' id='$gis_long_name' name='$gis_long_name'>\n";
656                $html .= "</div>\n";
657                $html .= "</div>\n";
658
659                /*
660                 * If Google Maps is enabled, call the geocoding service,
661                 * then use Google Maps to let the user choose a more precise location
662                 *
663                 * otherwise
664                 *
665                 * Simply use a geocoding service.
666                 */
667
668                if (defined('GMAPS_HOTSPOTS_MAP_ENABLED') && GMAPS_HOTSPOTS_MAP_ENABLED === true)
669                {
670                        $html .= "<div class='admin_section_container'>\n";
671                        $html .= "<div class='admin_section_data'>\n";
672                        $html .= "<input type='submit' name='geocode_only' value='"._("Geocode only")."'>\n";
673                        $html .= "<input type='button' name='google_maps_geocode' value='"._("Check using Google Maps")."' onClick='window.open(\"hotspot_location_map.php?node_id={$this->getId()}\", \"hotspot_location\", \"toolbar=0,scrollbars=1,resizable=1,location=0,statusbar=0,menubar=0,width=600,height=600\");'>\n";
674                        $html .= " ("._("Use a geocoding service, then use Google Maps to pinpoint the exact location.").")";
675                        $html .= "</div>\n";
676                        $html .= "</div>\n";
677                }
678                else
679                {
680                        $html .= "<div class='admin_section_container'>\n";
681                        $html .= "<div class='admin_section_data'>\n";
682                        $html .= "<input type='submit' name='geocode_only' value='"._("Geocode location")."'>\n";
683                        $html .= " ("._("Use a geocoding service").")";
684                        $html .= "</div>\n";
685                        $html .= "</div>\n";
686                }
687
688                // End of GIS data
689                $html .= "</div>\n";
690
691                // Owners management
692                $html .= "<div class='admin_section_container'>\n";
693                $html .= "<div class='admin_section_title'>"._("Node owners")." : </div>\n";
694                $html .= "<ul class='admin_section_list'>\n";
695                foreach ($this->getOwners() as $owner)
696                {
697                        $html .= "<li class='admin_section_list_item'>\n";
698                        $html .= "<div class='admin_section_data'>\n";
699                        $html .= "{$owner->getUsername()}";
700                        $html .= "</div>\n";
701                        $html .= "<div class='admin_section_tools'>\n";
702                        $name = "node_{$this->getId()}_owner_{$owner->GetId()}_remove";
703                        $html .= "<input type='submit' name='$name' value='"._("Remove owner")."'>";
704                        $html .= "</div>\n";
705                        $html .= "</li>\n";
706                }
707                $html .= "<li class='admin_section_list_item'>\n";
708                $name = "node_{$this->getId()}_new_owner";
709                $html .= User :: getSelectUserUI($name);
710                $name = "node_{$this->getId()}_new_owner_submit";
711                $html .= "<input type='submit' name='$name' value='"._("Add owner")."'>";
712                $html .= "</li>\n";
713                $html .= "</ul>\n";
714                $html .= "</div>\n";
715                $html .= "</div>\n";
716
717                // Tech officers management
718                $html .= "<div class='admin_section_container'>\n";
719                $html .= "<div class='admin_section_title'>"._("Technical officers")." : </div>\n";
720                $html .= "<ul class='admin_section_list'>\n";
721                foreach ($this->getTechnicalOfficers() as $tech_officer)
722                {
723                        $html .= "<li class='admin_section_list_item'>\n";
724                        $html .= "<div class='admin_section_data'>\n";
725                        $html .= "{$tech_officer->getUsername()}";
726                        $html .= "</div>\n";
727                        $html .= "<div class='admin_section_tools'>\n";
728                        $name = "node_{$this->getId()}_tech_officer_{$tech_officer->GetId()}_remove";
729                        $html .= "<input type='submit' name='$name' value='"._("Remove technical officer")."'>";
730                        $html .= "</div>\n";
731                        $html .= "</li>\n";
732                }
733                $html .= "<li class='admin_section_list_item'>\n";
734                $name = "node_{$this->getId()}_new_tech_officer";
735                $html .= User :: getSelectUserUI($name);
736                $name = "node_{$this->getId()}_new_tech_officer_submit";
737                $html .= "<input type='submit' name='$name' value='"._("Add technical officer")."'>";
738                $html .= "</li>\n";
739                $html .= "</ul>\n";
740                $html .= "</div>\n";
741                $html .= "</div>\n";
742
743                // Display stats
744                $html .= "<div class='admin_section_container'>\n";
745                $html .= "<div class='admin_section_title'>"._("Statistics:")."</div>\n";
746                $html .= "<div class='admin_section_data'>\n";
747                $name = "node_".$this->id."_get_stats";
748                $html .= "<input type='submit' name='$name' value='"._("Get access statistics")."'>";
749                $html .= "</div>\n";
750                $html .= "</div>\n";
751
752                $html .= "<div class='admin_section_container'>\n";
753                $html .= "<div class='admin_section_title'>"._("Node content:")."</div>\n";
754
755                $html .= "<ul class='admin_section_list'>\n";
756                foreach ($this->getAllContent() as $content)
757                {
758                        $html .= "<li class='admin_section_list_item'>\n";
759                        $html .= "<div class='admin_section_data'>\n";
760                        $html .= $content->getListUI();
761                        $html .= "</div>\n";
762                        $html .= "<div class='admin_section_tools'>\n";
763                        $name = "node_".$this->id."_content_".$content->GetId()."_edit";
764                        $html .= "<input type='button' name='$name' value='"._("Edit")."' onClick='window.location.href = \"".GENERIC_OBJECT_ADMIN_ABS_HREF."?object_class=Content&action=edit&object_id=".$content->GetId()."\";'>\n";
765                        $name = "node_".$this->id."_content_".$content->GetId()."_erase";
766                        $html .= "<input type='submit' name='$name' value='"._("Remove")."'>";
767                        $html .= "</div>\n";
768                        $html .= "</li>\n";
769                }
770                $html .= "<li class='admin_section_list_item'>\n";
771                $name = "node_{$this->id}_new_content";
772                $html .= Content :: getSelectContentUI($name, "AND content_id NOT IN (SELECT content_id FROM node_has_content WHERE node_id='$this->id')");
773                $name = "node_{$this->id}_new_content_submit";
774                $html .= "<input type='submit' name='$name' value='"._("Add")."'>";
775                $html .= "</li>\n";
776                $html .= "</ul>\n";
777                $html .= "</div>\n";
778
779                return $html;
780        }
781
782        /** Process admin interface of this object.
783        */
784        public function processAdminUI()
785        {
786                $user = User :: getCurrentUser();
787                if (!$this->isOwner($user) && !$user->isSuperAdmin())
788                {
789                        throw new Exception(_('Access denied!'));
790                }
791
792                // Information about the node
793
794                // Name
795                $name = "node_".$this->getId()."_name";
796                $this->setName($_REQUEST[$name]);
797
798                // Homepage URL
799                $name = "node_".$this->getId()."_homepage_url";
800                $this->setHomePageUrl($_REQUEST[$name]);
801
802                // Description
803                $name = "node_".$this->getId()."_description";
804                $this->setDescription($_REQUEST[$name]);
805
806                // Map URL
807                $name = "node_".$this->getId()."_map_url";
808                $this->setMapUrl($_REQUEST[$name]);
809
810                // Civic number
811                $name = "node_".$this->getId()."_civic_number";
812                $this->setCivicNumber($_REQUEST[$name]);
813
814                // Street name
815                $name = "node_".$this->getId()."_street_name";
816                $this->setStreetName($_REQUEST[$name]);
817
818                // City
819                $name = "node_".$this->getId()."_city";
820                $this->setCity($_REQUEST[$name]);
821
822                // Province
823                $name = "node_".$this->getId()."_province";
824                $this->setProvince($_REQUEST[$name]);
825
826                // Postal Code
827                $name = "node_".$this->getId()."_postal_code";
828                $this->setPostalCode($_REQUEST[$name]);
829
830                // Country
831                $name = "node_".$this->getId()."_country";
832                $this->setCountry($_REQUEST[$name]);
833
834                // Public phone #
835                $name = "node_".$this->getId()."_public_phone";
836                $this->setTelephone($_REQUEST[$name]);
837
838                // Public mail
839                $name = "node_".$this->getId()."_public_email";
840                $this->setEmail($_REQUEST[$name]);
841
842                // Mass transit info
843                $name = "node_".$this->getId()."_mass_transit_info";
844                $this->setTransitInfo($_REQUEST[$name]);
845
846                // Deployment status
847                $name = "node_".$this->getId()."_deployment_status";
848                $this->setDeploymentStatus(self :: processSelectDeploymentStatus($name));
849
850                // GIS data
851                // Get a geocoder for a given country
852                if (!empty ($_REQUEST['geocode_only']))
853                {
854                        $geocoder = AbstractGeocoder :: getGeocoder($this->getCountry());
855                        if ($geocoder != null)
856                        {
857                                $geocoder->setCivicNumber($this->getCivicNumber());
858                                $geocoder->setStreetName($this->getStreetName());
859                                $geocoder->setCity($this->getCity());
860                                $geocoder->setProvince($this->getProvince());
861                                $geocoder->setPostalCode($this->getPostalCode());
862                                if ($geocoder->validateAddress() === true)
863                                {
864                                        if (($point = $geocoder->getGisLocation()) !== null)
865                                                $this->setGisLocation($point);
866                                        else
867                                                echo _("It appears that the Geocoder could not be reached or could not geocode the given address.");
868                                }
869                                else
870                                        echo _("You must enter a valid address.");
871                        }
872                }
873                else
874                {
875                        // Use what has been set by the user.   
876                        $gis_lat_name = "node_".$this->getId()."_gis_latitude";
877                        $gis_long_name = "node_".$this->getId()."_gis_longitude";
878                        $this->setGisLocation(new GisPoint($_REQUEST[$gis_lat_name], $_REQUEST[$gis_long_name], .0));
879                }
880
881                // Statistics
882                $name = "node_{$this->id}_get_stats";
883                if (!empty ($_REQUEST[$name]))
884                        header("Location: hotspot_log.php?node_id=".urlencode($this->getId()));
885
886                // Owners processing
887                // Rebuild user id, and delete if it was selected
888                foreach ($this->getOwners() as $owner)
889                {
890                        $name = "node_{$this->getId()}_owner_{$owner->GetId()}_remove";
891                        if (!empty ($_REQUEST[$name]))
892                        {
893                                if ($this->isOwner($owner))
894                                        $this->removeOwner($owner);
895                                else
896                                        echo _("Invalid user!");
897                        }
898                }
899               
900                $name = "node_{$this->getId()}_new_owner_submit";
901                if (!empty ($_REQUEST[$name]))
902                {
903                        $name = "node_{$this->getId()}_new_owner";
904                        $owner = User :: processSelectUserUI($name);
905                        if ($owner)
906                        {
907                                if ($this->isOwner($owner))
908                                        echo _("The user is already an owner of this node.");
909                                else
910                                        $this->addOwner($owner);
911                        }
912                }
913
914                // Technical officers processing
915                // Rebuild user id, and delete if it was selected
916                foreach ($this->getTechnicalOfficers() as $tech_officer)
917                {
918                        $name = "node_{$this->getId()}_tech_officer_{$tech_officer->GetId()}_remove";
919                        if (!empty ($_REQUEST[$name]))
920                        {
921                                if ($this->isTechnicalOfficer($tech_officer))
922                                        $this->removeTechnicalOfficer($tech_officer);
923                                else
924                                        echo _("Invalid user!");
925                        }
926                }
927
928                $name = "node_{$this->getId()}_new_tech_officer_submit";
929                if (!empty ($_REQUEST[$name]))
930                {
931                        $name = "node_{$this->getId()}_new_tech_officer";
932                        $tech_officer = User :: processSelectUserUI($name);
933                        if ($tech_officer)
934                        {
935                                if ($this->isTechnicalOfficer($tech_officer))
936                                        echo _("The user is already a technical officer of this node.");
937                                else
938                                        $this->addTechnicalOfficer($tech_officer);
939                        }
940                }
941
942                // Content processing
943                // Rebuild content id and deleting if it was selected for deletion )
944                foreach ($this->getAllContent() as $content)
945                {
946                        $name = "node_".$this->id."_content_".$content->GetId()."_erase";
947                        if (!empty ($_REQUEST[$name]))
948                        {
949                                $this->removeContent($content);
950                        }
951                }
952
953                $name = "node_{$this->id}_new_content_submit";
954                if (!empty ($_REQUEST[$name]))
955                {
956                        $name = "node_{$this->id}_new_content";
957                        $content = Content :: processSelectContentUI($name);
958                        if ($content)
959                                $this->addContent($content);
960                }
961        }
962
963        // Redirect to this node's portal page
964        public function getUserUI()
965        {
966                header("Location: ".BASE_SSL_PATH."portal/?gw_id=".$this->getId());
967        }
968
969        /** Add content to this node */
970        public function addContent(Content $content)
971        {
972                global $db;
973                $content_id = $db->EscapeString($content->getId());
974                $sql = "INSERT INTO node_has_content (node_id, content_id) VALUES ('$this->id','$content_id')";
975                $db->ExecSqlUpdate($sql, false);
976        }
977
978        /** Remove content from this node */
979        public function removeContent(Content $content)
980        {
981                global $db;
982                $content_id = $db->EscapeString($content->getId());
983                $sql = "DELETE FROM node_has_content WHERE node_id='$this->id' AND content_id='$content_id'";
984                $db->ExecSqlUpdate($sql, false);
985        }
986
987        /**Get an array of all Content linked to this node
988         * @param boolean $exclude_subscribed_content
989        * @param User $subscriber The User object used to discriminate the content
990        * @return an array of Content or an empty arrray */
991        function getAllContent($exclude_subscribed_content = false, $subscriber = null)
992        {
993                global $db;
994                $retval = array ();
995                // Get all network, but exclude user subscribed content if asked
996                if ($exclude_subscribed_content == true && $subscriber)
997                        $sql = "SELECT content_id FROM node_has_content WHERE node_id='$this->id' AND content_id NOT IN (SELECT content_id FROM user_has_content WHERE user_id = '{$subscriber->getId()}') ORDER BY subscribe_timestamp DESC";
998                else
999                        $sql = "SELECT content_id FROM node_has_content WHERE node_id='$this->id' ORDER BY subscribe_timestamp DESC";
1000                $db->ExecSql($sql, $content_rows, false);
1001
1002                if ($content_rows != null)
1003                {
1004                        foreach ($content_rows as $content_row)
1005                        {
1006                                $retval[] = Content :: getObject($content_row['content_id']);
1007                        }
1008                }
1009                return $retval;
1010        }
1011
1012        /** Get an array of all artistic and locative Content for this hotspot
1013        * @return an array of Content or an empty arrray */
1014        function getAllLocativeArtisticContent()
1015        {
1016                global $db;
1017                $retval = array ();
1018                $sql = "SELECT * FROM content_group JOIN content ON (content.content_id = content_group.content_group_id) JOIN node_has_content ON (node_has_content.content_id = content_group.content_group_id AND node_has_content.node_id = '{$this->getId()}') WHERE is_persistent = true AND is_artistic_content = true AND is_locative_content = true ORDER BY subscribe_timestamp DESC";
1019                $db->ExecSql($sql, $content_rows, false);
1020                if ($content_rows != null)
1021                {
1022                        foreach ($content_rows as $content_row)
1023                        {
1024                                // Create a content group object and grab only those that have content for the current Node
1025                                $content_group = Content :: getObject($content_row['content_group_id']);
1026                                if ($content_group->getDisplayNumElements() >= 1)
1027                                {
1028                                        if ($content_group->isDisplayableAt($this))
1029                                        {
1030                                                // Disable logging and allow content to expand ( if possible )
1031                                                $content_group->setExpandStatus(true);
1032                                                $content_group->setLoggingStatus(false);
1033                                                $retval[] = $content_group;
1034                                        }
1035                                }
1036                        }
1037                }
1038                return $retval;
1039        }
1040
1041        /** Return all the nodes
1042         */
1043        static function getAllNodes()
1044        {
1045                global $db;
1046
1047                $db->ExecSql("SELECT * FROM nodes", $nodes, false);
1048
1049                if ($nodes == null)
1050                        throw new Exception(_("No nodes could not be found in the database"));
1051
1052                return $nodes;
1053        }
1054
1055        static function getAllNodesOrdered($order_by)
1056        {
1057                global $db;
1058
1059                $db->ExecSql("SELECT * FROM nodes ORDER BY $order_by", $nodes, false);
1060
1061                if ($nodes == null)
1062                        throw new Exception(_("No nodes could not be found in the database"));
1063
1064                return $nodes;
1065        }
1066
1067        static function getAllNodesWithStatus()
1068        {
1069                global $db;
1070
1071                $db->ExecSql("SELECT node_id, name, last_heartbeat_user_agent, (NOW()-last_heartbeat_timestamp) AS since_last_heartbeat, last_heartbeat_ip, CASE WHEN ((NOW()-last_heartbeat_timestamp) < interval '5 minutes') THEN true ELSE false END AS online, creation_date, node_deployment_status FROM nodes ORDER BY node_id", $nodes, false);
1072
1073                if ($nodes == null)
1074                        throw new Exception(_("No nodes could not be found in the database"));
1075
1076                return $nodes;
1077        }
1078
1079        static function getAllDeploymentStatus()
1080        {
1081                global $db;
1082
1083                $db->ExecSql("SELECT * FROM node_deployment_status", $statuses, false);
1084                if ($statuses == null)
1085                        throw new Exception(_("No deployment statues  could be found in the database"));
1086
1087                $statuses_array = array ();
1088                foreach ($statuses as $status)
1089                        array_push($statuses_array, $status['node_deployment_status']);
1090
1091                return $statuses_array;
1092        }
1093
1094        /** The list of users online at this node
1095         * @return An array of User object, or en empty array */
1096        function getOnlineUsers()
1097        {
1098                global $db;
1099                $retval = array ();
1100                $db->ExecSql("SELECT users.user_id FROM users,connections WHERE connections.token_status='".TOKEN_INUSE."' AND users.user_id=connections.user_id AND connections.node_id='{$this->id}'", $users, false);
1101                if ($users != null)
1102                {
1103                        foreach ($users as $user_row)
1104                        {
1105                                $retval[] = User :: getObject($user_row['user_id']);
1106                        }
1107                }
1108                return $retval;
1109        }
1110
1111        function getOwners()
1112        {
1113                global $db;
1114                $retval = array ();
1115                $db->ExecSql("SELECT user_id FROM node_stakeholders WHERE is_owner = true AND node_id='{$this->id}'", $owners, false);
1116                if ($owners != null)
1117                {
1118                        foreach ($owners as $owner_row)
1119                        {
1120                                $retval[] = User :: getObject($owner_row['user_id']);
1121                        }
1122                }
1123                return $retval;
1124        }
1125
1126        function getTechnicalOfficers()
1127        {
1128                global $db;
1129                $retval = array ();
1130                $db->ExecSql("SELECT user_id FROM node_stakeholders WHERE is_tech_officer = true AND node_id='{$this->id}'", $officers, false);
1131                if ($officers != null)
1132                {
1133                        foreach ($officers as $officer_row)
1134                        {
1135                                $retval[] = User :: getObject($officer_row['user_id']);
1136                        }
1137                }
1138                return $retval;
1139        }
1140
1141        /** Associates an owner to this node
1142         * @param User
1143         */
1144        function addOwner(User $user)
1145        {
1146                global $db;
1147                $db->ExecSql("SELECT * FROM node_stakeholders WHERE node_id = '{$this->getId()}' AND user_id = '{$user->getId()}'", $rows, false);
1148                if (!$rows)
1149                {
1150                        if (!$db->ExecSqlUpdate("INSERT INTO node_stakeholders (node_id, user_id, is_owner) VALUES ('{$this->getId()}','{$user->getId()}', true)", false))
1151                                throw new Exception(_('Could not add owner'));
1152                }
1153                else
1154                        if (!$db->ExecSqlUpdate("UPDATE node_stakeholders SET is_owner = true WHERE node_id = '{$this->getId()}' AND user_id = '{$user->getId()}';", false))
1155                                throw new Exception(_('Could not add owner'));
1156        }
1157
1158        /** Associates a technical officer ( tech support ) to this node
1159         * @param User
1160         */
1161        function addTechnicalOfficer(User $user)
1162        {
1163                global $db;
1164                $db->ExecSql("SELECT * FROM node_stakeholders WHERE node_id = '{$this->getId()}' AND user_id = '{$user->getId()}'", $rows, false);
1165                if (!$rows)
1166                {
1167                        if (!$db->ExecSqlUpdate("INSERT INTO node_stakeholders (node_id, user_id, is_tech_officer) VALUES ('{$this->getId()}','{$user->getId()}', true)", false))
1168                                throw new Exception(_('Could not add tech officer'));
1169                }
1170                else
1171                        if (!$db->ExecSqlUpdate("UPDATE node_stakeholders SET is_tech_officer = true WHERE node_id = '{$this->getId()}' AND user_id = '{$user->getId()}';", false))
1172                                throw new Exception(_('Could not add tech officer'));
1173        }
1174
1175        /** Remove a technical officer ( tech support ) from this node
1176         * @param User
1177         */
1178        function removeOwner(User $user)
1179        {
1180                global $db;
1181                if (!$db->ExecSqlUpdate("UPDATE node_stakeholders SET is_owner = false WHERE node_id = '{$this->getId()}' AND user_id = '{$user->getId()}';", false))
1182                        throw new Exception(_('Could not remove owner'));
1183        }
1184
1185        /** Remove a technical officer ( tech support ) from this node
1186         * @param User
1187         */
1188        function removeTechnicalOfficer(User $user)
1189        {
1190                global $db;
1191                if (!$db->ExecSqlUpdate("UPDATE node_stakeholders SET is_tech_officer = false WHERE node_id = '{$this->getId()}' AND user_id = '{$user->getId()}';", false))
1192                        throw new Exception(_('Could not remove tech officer'));
1193        }
1194
1195        /** Is the user an owner of the Node?
1196         * @return true our false*/
1197        function isOwner(User $user)
1198        {
1199                global $db;
1200                if ($user != null)
1201                {
1202                        $user_id = $user->getId();
1203                        $retval = false;
1204                        $db->ExecSqlUniqueRes("SELECT * FROM node_stakeholders WHERE is_owner = true AND node_id='{$this->id}' AND user_id='{$user_id}'", $row, false);
1205                        if ($row != null)
1206                        {
1207                                $retval = true;
1208                        }
1209                }
1210                return $retval;
1211        }
1212
1213        /** Is the user a technical officer of the Node?
1214         * @return true our false*/
1215        function isTechnicalOfficer(User $user)
1216        {
1217                global $db;
1218                if ($user != null)
1219                {
1220                        $user_id = $user->getId();
1221                        $retval = false;
1222                        $db->ExecSqlUniqueRes("SELECT * FROM node_stakeholders WHERE is_tech_officer = true AND node_id='{$this->id}' AND user_id='{$user_id}'", $row, false);
1223                        if ($row != null)
1224                        {
1225                                $retval = true;
1226                        }
1227                }
1228                return $retval;
1229        }
1230
1231        /** Check if an node exists */
1232        private function nodeExists($id)
1233        {
1234                global $db;
1235                $retval = false;
1236                $id_str = $db->EscapeString($id);
1237                $sql = "SELECT * FROM nodes WHERE node_id='{$id_str}'";
1238                $db->ExecSqlUniqueRes($sql, $row, false);
1239                if($row!=null)
1240                {
1241                        $retval = true;
1242                }
1243                return $retval;
1244        }
1245
1246        /** Warning, the semantics of this function will change *
1247         * @deprecated version - 2005-04-29 USE getOnlineUsers instead
1248         * */
1249        public static function getAllOnlineUsers()
1250        {
1251                global $db;
1252                $db->ExecSql("SELECT * FROM connections,users,nodes WHERE token_status='".TOKEN_INUSE."' AND users.user_id=connections.user_id AND nodes.node_id=connections.node_id ORDER BY timestamp_in DESC", $online_users);
1253                return $online_users;
1254        }
1255
1256        /** Reloads the object from the database.  Should normally be called after a set operation */
1257        protected function refresh()
1258        {
1259                $this->__construct($this->id);
1260        }
1261
1262} // End class
1263?>
Note: See TracBrowser for help on using the browser.