root/wifidogadmin/wifidog/classes/ProfileField.php

Revision 479, 13.5 kB (checked in by insultant, 10 months ago)

--

Line 
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 // +-------------------------------------------------------------------+
5 // | WiFiDog Authentication Server                                     |
6 // | =============================                                     |
7 // |                                                                   |
8 // | The WiFiDog Authentication Server is part of the WiFiDog captive  |
9 // | portal suite.                                                     |
10 // +-------------------------------------------------------------------+
11 // | PHP version 5 required.                                           |
12 // +-------------------------------------------------------------------+
13 // | Homepage:     http://www.wifidog.org/                             |
14 // | Source Forge: http://sourceforge.net/projects/wifidog/            |
15 // +-------------------------------------------------------------------+
16 // | This program is free software; you can redistribute it and/or     |
17 // | modify it under the terms of the GNU General Public License as    |
18 // | published by the Free Software Foundation; either version 2 of    |
19 // | the License, or (at your option) any later version.               |
20 // |                                                                   |
21 // | This program is distributed in the hope that it will be useful,   |
22 // | but WITHOUT ANY WARRANTY; without even the implied warranty of    |
23 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
24 // | GNU General Public License for more details.                      |
25 // |                                                                   |
26 // | You should have received a copy of the GNU General Public License |
27 // | along with this program; if not, contact:                         |
28 // |                                                                   |
29 // | Free Software Foundation           Voice:  +1-617-542-5942        |
30 // | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
31 // | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
32 // |                                                                   |
33 // +-------------------------------------------------------------------+
34
35 /**
36  * Field of a profile
37  * @package    WiFiDogAuthServer
38  * @subpackage ContentClasses
39  * @author     François Proulx <francois.proulx@gmail.com>, Benoit Grégoire
40  * @copyright  2007 François Proulx, 2007 Technologies Coeus inc.
41  * @link       http://www.wifidog.org/
42  */
43
44 /**
45  * Load required classes
46  */
47 require_once ('classes/ProfileTemplate.php');
48 require_once ('classes/Content.php');
49
50 class ProfileField implements GenericObject {
51     private static $instanceArray = array();
52
53     private $profile_field_row;
54     private $profile_template_field = null;
55
56     private $id = null;
57
58     /**
59      * Constructor
60      *
61      * @param string $profile_field_id Field ID
62      */
63     protected function __construct($profile_field_id) {
64         $db = AbstractDb::getObject();
65
66         // Init values
67         $row = null;
68
69         $profile_field_id = $db->escapeString($profile_field_id);
70
71         $sql = "SELECT * FROM profile_fields WHERE profile_field_id = '{$profile_field_id}'";
72         $db->execSqlUniqueRes($sql, $row, false);
73
74         $this->id = $row['profile_field_id'];
75         $this->profile_field_row = $row;
76     }
77
78     public static function &getObject($id)
79     {
80         if(!isset(self::$instanceArray[$id]))
81         {
82             self::$instanceArray[$id] = new self($id);
83         }
84         return self::$instanceArray[$id];
85     }
86
87     /**
88      * Retreives the Id of the object
89      *
90      * @return string The Id
91      */
92     public function getId()
93     {
94         return $this->id;
95     }
96
97     /**
98      * This method contains the interface to add an additional element to a
99      * content object.  (For example, a new string in a Langstring)
100      * It is called when getNewContentUI has only a single possible object type.
101      * It may also be called by the object getAdminUI to avoid code duplication.
102      *
103      * @param string $contentId      The id of the (possibly not yet created) content object.
104      *
105      * @param string $userData=null Understood values are:
106      * contentTypeFilter
107      *
108      *
109      * @return HTML markup or false.  False means that this object does not support this interface.
110      */
111     public static function getNewUI($contentId, $userData=null) {
112         $html = '';
113         $futureContentId = get_guid();
114         $name = "profile_field_{$contentId}_content_future_id";
115         $html .= '<input type="hidden" name="' . $name . '" value="' . $futureContentId . '">';
116         //echo "Profile::getNewUI: userData";pretty_print_r($userData);
117         $html .= Content::getNewUI($futureContentId, $userData);
118         return $html;
119     }
120
121     /**
122      *
123      *
124      * @param string $contentId  The id of the (possibly not yet created) content object.
125      *
126      * @param string $checkOnly  If true, only check if there is data to be processed.
127      *     Will be used to decide if an object is to be created.  If there is
128      * processNewUI will typically be called again with $checkOnly=false
129      *
130      * @return true if there was data to be processed, false otherwise
131
132      */
133     public static function processNewUI($contentId, $checkOnly=false) {
134         $name = "profile_field_{$contentId}_content_future_id";
135         $futureContentId = $_REQUEST[$name];
136
137         $contentNewUIRetval = Content::processNewUI($futureContentId, true);
138         if($contentNewUIRetval && $checkOnly==false) {
139             $object = Content :: createNewObject('Content', $futureContentId);//The true content type will be set by processNewUI()
140             //If there was data to processs, process it for real
141             Content::processNewUI($futureContentId, false);
142             $field = self::getObject($contentId);
143             $field->setContentField(Content::getObject($futureContentId));
144         }
145         return $contentNewUIRetval;
146     }
147
148
149     /**
150      * Retrieves the profile field's modification date
151      *
152      * @return string profile field's modification date
153      *
154      * @access public
155      */
156     public function getLastModificationDate()
157     {
158         return $this->_row['last_modified'];
159     }
160
161     /**
162      * Update the last modification date
163      */
164     public function touch()
165     {
166         $db = AbstractDb::getObject();
167
168         // Init values
169         $_retVal = true;
170
171         $_retVal = $db->execSqlUpdate("UPDATE profile_fields SET last_modified = NOW() WHERE profile_field = '{$this->getId()}'", false);
172         $this->refresh();
173
174         return $_retVal;
175     }
176
177     /**
178      * Retrieves the associated profile template field
179      *
180      * @return ProfileTemplateField object
181      *
182      * @access public
183      */
184     public function getProfileTemplateField()
185     {
186         $retval = null;
187         $retval = ProfileTemplateField :: getObject($this->profile_field_row['profile_template_field_id']);
188         return $retval;
189     }
190
191     /**
192      * Retrieves the associated content field
193      *
194      * @return Content object
195      *
196      * @access public
197      */
198     public function getContentField()
199     {
200         $retval = null;
201         if($this->profile_field_row['content_id'] != null)
202         $retval = Content :: getObject($this->profile_field_row['content_id']);
203         return $retval;
204     }
205
206     /**
207      * Set the content field
208      * @param a content object to associate with the field
209      */
210     public function setContentField($content)
211     {
212         $db = AbstractDb::getObject();
213
214         // Init values
215         $_retVal = true;
216
217         if($content == null)
218         $content_id = "NULL";
219         else
220         $content_id = "'".$db->escapeString($content->getId())."'";
221
222         $_retVal = $db->execSqlUpdate("UPDATE profile_fields SET content_id = $content_id WHERE profile_field_id = '{$this->getId()}'", false);
223         $this->refresh();
224
225         return $_retVal;
226     }
227
228     /**
229      * Create a new ProfileField in the database
230      *
231      * @param string $profile MANDATORY The profile this field belongs to
232      * @param string $templateField MANDATORY The template this field is based on
233      * @param string $id Optionnal The id to be given to the new Object. If
234      *                             null, a new id will be assigned
235      *
236      * @return object The newly created object, or null if there was an
237      *                error (an exception is also trown)
238
239      */
240     public static function createNewObject(Profile $profile = null, ProfileTemplateField $templateField = null, $id = null) {
241         $db = AbstractDb :: getObject();
242         $profileId = $db->escapeString($profile->getId());
243         $templateFieldId = $db->escapeString($templateField->getId());
244         if (empty ($id)) {
245             $fieldId = get_guid();
246         } else {
247             $fieldId = $db->escapeString($id);
248         }
249         $sql = "INSERT INTO profile_fields (profile_id, profile_field_id, profile_template_field_id) VALUES ('$profileId', '$fieldId', '$templateFieldId');\n";
250         if (!$db->execSqlUpdate($sql, false)) {
251             throw new Exception(_('Unable to insert the new profile fields in the database!'));
252         }
253         return self::getObject($fieldId);
254     }
255     public static function getCreateNewObjectUI() {}
256     public static function processCreateNewObjectUI() {}
257
258     /**
259      * Shows the administration interface for ProfileField
260      * @return string HTML code for the administration interface
261      */
262     public function getAdminUI() {
263         $html = "";
264         $title = null;
265         $admin_label = $this->getProfileTemplateField()->getAdminLabelContent();
266         if($admin_label != null) {
267             $title $admin_label->__toString();
268         }
269
270         if($this->getProfileTemplateField()->getContentTypeFilter() != null) {
271             $content = $this->getContentField();
272             if($content == null) {
273                 $html .= Content :: getNewContentUI("profile_field_{$this->id}_new_content", $this->getProfileTemplateField()->getContentTypeFilter());
274             }
275             else {
276                 $html .= "<div class='admin_element_data'>\n";
277                 $html .= $content->getAdminUI(null, $title);
278                 $html .= "</div>\n";
279                 $html .= "<div class='admin_element_tools'>\n";
280                 $name = "profile_field_" . $this->id . "_erase";
281                 $html .= "<input type='submit' class='submit' name='$name' value='" . sprintf(_("Delete %s"), get_class($content)) . "'>";
282                 $html .= "</div>\n";
283             }
284         }
285         else
286         throw new Exception("Could not retrieve the associated content type filter.");
287
288
289         return $html;
290     }
291
292     /**
293      * Processes the input of the administration interface for ProfileField
294      *
295      * @return void
296      */
297     public function processAdminUI() {
298         //echo "ProfileField::processAdminUI()<br/>\n";
299         $content = $this->getContentField();
300         if ($content == null) {
301             $new_content = Content :: processNewContentUI("profile_field_{$this->id}_new_content");
302             if ($new_content != null) {
303                 $this->setContentField($new_content);
304             }
305         } else {
306             $content->processAdminUI();
307             $name = "profile_field_" . $this->id . "_erase";
308             if (!empty ($_REQUEST[$name]) && $_REQUEST[$name] == true) {
309                 $errmsg = null;
310                 $content->delete($errmsg);
311             }
312         }
313
314         $this->refresh();
315     }
316
317     /**
318      * Retreives the user interface of this object.
319      *
320      * @return string The HTML fragment for this interface
321      */
322     public function getUserUI() {
323         // Init values
324         //echo "ProfileField::getUserUI()";
325         $html = "";
326         $template_field = $this->getProfileTemplateField();
327         $html .=  "<div class='user_ui_profile_field ".$template_field->getSemanticId()."'>\n";
328
329         $content_label = $template_field->getDisplayLabelContent();
330         if(!empty($content_label)){
331             $html .=  "<div class='profile_field_label'>".$content_label->getUserUI()."</div>\n";
332         }
333
334         $content_field = $this->getContentField();
335         if(!empty($content_field)) {
336             // Display the actual value from the profile field
337             $html .=  "<div class='profile_field_content'>".$content_field->getUserUI()."</div>\n";
338         }
339         $html .=  "</div>\n";
340
341         return $html;
342     }
343
344     /**
345      * Deletes a ProfileTemplateField object
346      *
347      * @param string $errmsg Reference to error message
348      *
349      * @return bool True if deletion was successful
350      * @internal Persistent content will not be deleted
351      *
352      * @todo Implement proper access control
353      */
354     public function delete(& $errmsg) {
355         require_once('classes/User.php');
356
357         $db = AbstractDb::getObject();
358
359         // Init values
360         $_retVal = false;
361
362         if (!User::getCurrentUser()->DEPRECATEDisSuperAdmin()) {
363             $errmsg = _('Access denied (must have super admin access)');
364         } else {
365             $_id = $db->escapeString($this->getId());
366
367             if (!$db->execSqlUpdate("DELETE FROM profile_fields WHERE profile_field_id = '{$_id}'", false)) {
368                 $errmsg = _('Could not delete ProfileField!');
369             } else {
370                 $_retVal = true;
371             }
372         }
373
374         return $_retVal;
375     }
376     /** Reloads the object from the database.  Should normally be called after a set operation */
377     protected function refresh() {
378         $this->__construct($this->id);
379     }
380 }
381
382 /*
383  * Local variables:
384  * tab-width: 4
385  * c-basic-offset: 4
386  * c-hanging-comment-ender-p: nil
387  * End:
388  */
Note: See TracBrowser for help on using the browser.