root/wifidogadmin/wifidog/classes/ProfileTemplateField.php

Revision 479, 24.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  * Fields of a profile template
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 ProfileTemplateField implements GenericObject {
51     private static $instanceArray = array();
52     
53     private $profile_template_field_row;
54     private $display_label_content = null;
55     private $content_type_filter = null;
56     
57     private $id = null;
58
59     /**
60      * Constructor
61      *
62      * @param string $profile_template_field_id Field ID
63      */
64     protected function __construct($profile_template_field_id) {
65         $db = AbstractDb::getObject();
66
67         // Init values
68         $row = null;
69
70         $profile_template_field_id = $db->escapeString($profile_template_field_id);
71
72         $sql = "SELECT * FROM profile_template_fields WHERE profile_template_field_id = '{$profile_template_field_id}'";
73         $db->execSqlUniqueRes($sql, $row, false);
74
75         $this->id = $row['profile_template_field_id'];
76         $this->profile_template_field_row = $row;
77     }
78     
79     public static function &getObject($id)
80     {
81         if(!isset(self::$instanceArray[$id]))
82         {
83             self::$instanceArray[$id] = new self($id);
84         }
85         return self::$instanceArray[$id];
86     }
87     
88     /**
89      * Retreives the Id of the object
90      *
91      * @return string The Id
92      */
93     public function getId()
94     {
95         return $this->id;
96     }
97     
98     public static function createNewObject() {}
99     public static function getCreateNewObjectUI() {}
100     public static function processCreateNewObjectUI() {}
101     
102     /**
103      * Get a flexible interface to generate new ProfileTemplateField
104      *
105      * @param string $user_prefix      A identifier provided by the programmer
106      *                                 to recognise it's generated HTML form
107      * @return string HTML markup
108      */
109     public static function getCreateFieldUI($user_prefix, $title = null) {
110
111         $db = AbstractDb :: getObject();
112
113         // Init values
114         $html = "";
115         $html .= "<fieldset class='admin_container Content'>\n";
116         if (!empty ($title)) {
117             $html .= "<legend>$title</legend>\n";
118         }
119
120         $availableContentTypeFilters = ContentTypeFilter :: getAllContentTypeFilters();
121
122         $name = "get_new_profile_template_field_{$user_prefix}_content_type_filter";
123
124         $i = 0;
125         $tab = array ();
126         foreach ($availableContentTypeFilters as $filter) {
127             $tab[$i][0] = $filter->getId();
128             $tab[$i][1] = $filter->getLabel() == null ? "["._("No label")."] - ".$filter->getId() : $filter->getLabel();
129             $i++;
130         }
131         if (count($tab) > 1) {
132             $label = _("Add new profile template field filtered by") . ": ";
133             $html .= "<div class='admin_element_data content_add'>";
134             $html .= $label;
135             $html .= FormSelectGenerator :: generateFromArray($tab, null, $name, null, false);
136             $html .= "</div>";
137         } else
138             if (count($tab) == 1) {
139                 $html .= '<input type="hidden" name="' . $name . '" value="' . $tab[0][0] . '">';
140             } else {
141                 $html .= "<div class='errormsg'>"._("Sorry, no content type filter exists.")."</div>\n";
142             }
143
144         $name = "get_new_profile_template_field_{$user_prefix}_add";
145
146         if (count($tab) >= 1) {
147             $value = _("Add");
148             $html .= "<div class='admin_element_tools'>";
149             $html .= '<input type="submit" class="submit" name="' . $name . '" value="' . $value . '">';
150             $html .= "</div>";
151         }
152         
153         $html .= "</fieldset>\n";
154         return $html;
155     }
156     
157     /**
158      * This method will create a ProfileTemplateField based on the content type filter specified
159      *
160      * @param string $user_prefix                A identifier provided by the programmer to
161      *                                           recognise it's generated form
162      * @param string $profile_template              Must be present
163      *
164      * @return object The ProfileTemplateField object, or null if the user didn't create one
165      * @static
166      */
167     public static function processCreateFieldUI($user_prefix, ProfileTemplate $profile_template) {
168         $db = AbstractDb::getObject();
169
170         // Init values
171         $profile_template_field_object = null;
172         $max_display_order_row = null;
173
174            $name = "get_new_profile_template_field_{$user_prefix}_add";
175
176         if (!empty ($_REQUEST[$name])) {
177             /* Get the display order to add the ProfileTemplateField at the end */
178             $sql = "SELECT MAX(display_order) as max_display_order FROM profile_template_fields WHERE profile_template_id = '" . $profile_template->getId() . "'";
179             $db->execSqlUniqueRes($sql, $max_display_order_row, false);
180             $display_order = $max_display_order_row['max_display_order'] + 1;
181
182             $profile_template_field_id = get_guid();
183             $sql = "INSERT INTO profile_template_fields (profile_template_field_id, profile_template_id, display_order) VALUES ('$profile_template_field_id', '{$profile_template->getId()}', $display_order);";
184
185             if (!$db->execSqlUpdate($sql, false)) {
186                 throw new Exception(_('Unable to insert new content into database!'));
187             }
188
189             $profile_template_field_object = self :: getObject($profile_template_field_id);
190             $name = "get_new_profile_template_field_{$user_prefix}_content_type_filter";
191             $content_type_filter_ui_result = FormSelectGenerator :: getResult($name, null);
192
193             if(empty($content_type_filter_ui_result))
194             {
195                 throw new exception("Unable to retrieve the content type filter to associate with the new field");
196             }
197             
198             $content_type_filter = ContentTypeFilter :: getObject($content_type_filter_ui_result);
199             $profile_template_field_object->replaceContentTypeFilter($content_type_filter);
200         }
201
202         return $profile_template_field_object;
203     }
204     
205     /**
206      * Replace and delete the old content_type_filter_id (if any) by the new
207      * content type filter
208      *
209      * @param object $new_content_type_filter ContentTypeFilter object or null.
210      *
211      * @return void
212      */
213     private function replaceContentTypeFilter($new_content_type_filter) {
214         $db = AbstractDb::getObject();
215
216         if ($new_content_type_filter != null && get_class($new_content_type_filter) == "ContentTypeFilter") {
217             $new_content_type_filter_id_sql = "'" . $new_content_type_filter->getId() . "'";
218             $this->content_type_filter = $new_content_type_filter;
219         } else {
220             $new_content_type_filter_id_sql = "NULL";
221             $this->content_type_filter = null;
222         }
223
224         $db->execSqlUpdate("UPDATE profile_template_fields SET content_type_filter_id = $new_content_type_filter_id_sql WHERE profile_template_field_id = '{$this->getId()}'", false);
225
226         $this->refresh();
227     }
228
229     /**
230      * Get the ContentTypeFilter object for this field
231      *
232      * @return ContentTypeFilter object
233      */
234     public function getContentTypeFilter() {
235         if ($this->content_type_filter == null) {
236             $this->content_type_filter = ContentTypeFilter :: getObject($this->profile_template_field_row['content_type_filter_id']);
237         }
238         return $this->content_type_filter;
239     }
240     
241     /**
242      * Replace and delete the old admin label content (if any) by the new
243      * content
244      *
245      * @param object $new_admin_label_content Content object or null.
246      *
247      * @return void
248      */
249     private function replaceAdminLabelContent($new_admin_label_content) {
250         $db = AbstractDb::getObject();
251
252         // Init values
253         $old_admin_label_content = null;
254         $errmsg = null;
255
256         if (!empty ($this->profile_template_field_row['admin_label_content_id'])) {
257             $old_admin_label_content = Content :: getObject($this->profile_template_field_row['admin_label_content_id']);
258         }
259
260         if ($new_admin_label_content != null) {
261             $new_admin_label_content_id_sql = "'" . $new_admin_label_content->getId() . "'";
262         } else {
263             $new_admin_label_content_id_sql = "NULL";
264         }
265
266         $db->execSqlUpdate("UPDATE profile_template_fields SET admin_label_content_id = $new_admin_label_content_id_sql WHERE profile_template_field_id = '$this->id'", false);
267
268         if ($old_admin_label_content != null) {
269             $old_admin_label_content->delete($errmsg);
270         }
271         $this->refresh();
272     }
273     
274     /**
275      * Get the admin label content object for this field (if there is one)
276      *
277      * @return Content object or null
278      */
279     public function getAdminLabelContent() {
280         $retval=null;
281         if($this->profile_template_field_row['admin_label_content_id']!=null) {
282             $retval = Content :: getObject($this->profile_template_field_row['admin_label_content_id']);
283         }
284         return $retval;
285     }
286     
287     /**
288      * Replace and delete the old display label content (if any) by the new
289      * content
290      *
291      * @param object $new_display_label_content Content object or null.
292      *
293      * @return void
294      */
295     private function replaceDisplayLabelContent($new_display_label_content) {
296         $db = AbstractDb::getObject();
297
298         // Init values
299         $old_display_label_content = null;
300         $errmsg = null;
301
302         if (!empty ($this->profile_template_field_row['display_label_content_id'])) {
303             $old_display_label_content = Content :: getObject($this->profile_template_field_row['display_label_content_id']);
304         }
305
306         if ($new_display_label_content != null) {
307             $new_display_label_content_id_sql = "'" . $new_display_label_content->getId() . "'";
308             $this->display_label_content = $new_display_label_content;
309         } else {
310             $new_display_label_content_id_sql = "NULL";
311             $this->display_label_content = null;
312         }
313
314         $db->execSqlUpdate("UPDATE profile_template_fields SET display_label_content_id = $new_display_label_content_id_sql WHERE profile_template_field_id = '$this->id'", false);
315
316         if ($old_display_label_content != null) {
317             $old_display_label_content->delete($errmsg);
318         }
319         $this->refresh();
320     }
321     
322     /**
323      * Get the display label content object for this field
324      *
325      * @return Content object
326      */
327     public function getDisplayLabelContent() {
328         $retval=null;
329         if($this->profile_template_field_row['display_label_content_id']!=null) {
330             $retval = Content :: getObject($this->profile_template_field_row['display_label_content_id']);
331         }
332         return $retval;
333     }
334     
335     /**
336      * Get the order of the field in the profile template
337      *
338      * @return string the order of the field in the profile template
339      */
340     public function getDisplayOrder() {
341         return $this->profile_template_field_row['display_order'];
342     }
343
344     /**
345      * Set the order of the field in the profile template
346      *
347      * @param string $order Order how fields should be displayed
348      *
349      * @return void
350      */
351     public function setDisplayOrder($order) {
352         
353         $db = AbstractDb::getObject();
354
355         if ($order != $this->getDisplayOrder()) {
356             /*
357              * Only update database if there is an actual change
358              */
359             $order = $db->escapeString($order);
360             $db->execSqlUpdate("UPDATE profile_template_fields SET display_order = $order WHERE profile_template_field_id = '$this->id'", false);
361             $this->refresh();
362         }
363     }
364     
365     /**
366      * Get the semantic ID of the field in the profile template
367      *
368      * @return string the semantic ID of the field in the profile template
369      */
370     public function getSemanticId() {
371         return $this->profile_template_field_row['semantic_id'];
372     }
373
374     /**
375      * Set the semantic ID of the field in the profile template
376      *
377      * @param string $semantic_id
378      *
379      * @return void
380      */
381     public function setSemanticId($semantic_id) {
382         
383         $db = AbstractDb::getObject();
384
385         if ($semantic_id != $this->getSemanticId()) {
386             /*
387              * Only update database if there is an actual change
388              */
389             $semantic_id = $db->escapeString($semantic_id);
390             $db->execSqlUpdate("UPDATE profile_template_fields SET semantic_id = '{$semantic_id}' WHERE profile_template_field_id = '$this->id'", false);
391             $this->refresh();
392         }
393     }
394
395     /**
396      * Shows the administration interface for ContentGroupElement
397      *
398      * @param string $subclass_admin_interface HTML code to be added after the
399      *                                         administration interface
400      *
401      * @return string HTML code for the administration interface
402      */
403     public function getAdminUI($subclass_admin_interface = null, $title = null) {
404         // Init values
405         $html = '';
406         $html .= "<li class='admin_element_item_container'>\n";
407         $html .= "<fieldset class='admin_element_group'>\n";
408         $html .= "<legend>".get_class($this)." [" . $this->getDisplayOrder() . "]</legend>\n";
409
410         $html .= "<ul class='admin_element_list'>\n";
411         
412         // display_order
413         $html .= "<li class='admin_element_item_container'>\n";
414         $html .= "<div class='admin_element_label'>"._("Display order").":</div>\n";
415         $html .= "<div class='admin_element_data'>\n";
416         $name = "profile_template_field_{$this->id}_display_order";
417         $html .= "<input type='text' name='$name' value='" . $this->getDisplayOrder() . "' size='2'>\n";
418         $html .= "</div>\n";
419         $html .= "</li>\n";
420
421         $metadata_criteria_array = array (
422         array (
423         'isSimpleContent'
424                         )
425         );
426         $metadada_allowed_content_types = ContentTypeFilter :: getObject($metadata_criteria_array);
427
428         
429         // content_type_filter_id
430         $html .= "<li class='admin_element_item_container'>\n";
431         if ($this->getContentTypeFilter() == null) {
432             $html .= "<div class='errormsg'>"._("Sorry, content type filter is missing.")."</div>\n";
433         } else {
434             $html .= "<div class='admin_element_label'>"._("Content type filter").":</div>\n";
435             $html .= "<div class='admin_element_data'>\n";
436             $label = $this->getContentTypeFilter()->getLabel();
437             $html .= empty($label) ? "["._("No label")."] - ".$this->getContentTypeFilter()->getId() : $label;
438             $html .= "</div>\n";
439         }
440         $html .= "</li>\n";
441         
442         // display_label_content_id
443         $html .= "<li class='admin_element_item_container'>\n";
444         $html .= "<div class='admin_element_label'>" . _("Display label") . ":</div>\n";
445         if (empty ($this->profile_template_field_row['display_label_content_id'])) {
446             $html .= Content :: getNewContentUI("profile_template_field_{$this->id}_new_display_label_content", $metadada_allowed_content_types);
447             $html .= "</li>\n";
448         } else {
449             $display_label_content = Content :: getObject($this->profile_template_field_row['display_label_content_id']);
450             $html .= $display_label_content->getAdminUI(null, sprintf(_("%s display label (%s)"), get_class($this), get_class($display_label_content)));
451             
452             $html .= "<div class='admin_element_tools'>\n";
453             $name = "profile_template_field_{$this->id}_erase_display_label_content";
454             $html .= "<input type='submit' name='$name' value='".sprintf(_("Delete %s (%s)"), _("display label"), get_class($display_label_content))."'>";
455             $html .= "</div>\n";
456         }
457         $html .= "</li>\n";
458         
459         // admin_label_content_id
460         $html .= "<li class='admin_element_item_container'>\n";
461         $html .= "<div class='admin_element_label'>" . _("Admin label") . ":</div>\n";
462         if (empty ($this->profile_template_field_row['admin_label_content_id'])) {
463             $html .= "<li class='admin_element_item_container'>\n";
464             $html .= Content :: getNewContentUI("profile_template_field_{$this->id}_new_admin_label_content", $metadada_allowed_content_types);
465             $html .= "</li>\n";
466         } else {
467             $admin_label_content = Content :: getObject($this->profile_template_field_row['admin_label_content_id']);
468             $html .= $admin_label_content->getAdminUI(null, sprintf(_("%s admin label (%s)"), get_class($this), get_class($admin_label_content)));
469             
470             $html .= "<div class='admin_element_tools'>\n";
471             $name = "profile_template_field_{$this->id}_erase_admin_label_content";
472             $html .= "<input type='submit' name='$name' value='".sprintf(_("Delete %s (%s)"), _("admin label"), get_class($admin_label_content))."'>";
473             $html .= "</div>\n";
474         }
475         $html .= "</li>\n";
476         
477         // semantic_id
478         $html .= "<li class='admin_element_item_container'>\n";
479         $html .= "<div class='admin_element_label'>"._("Semantic ID").":</div>\n";
480         $html .= "<div class='admin_element_data'>\n";
481         
482         $name = "profile_template_field_{$this->id}_semantic_id";
483         $html .= "<input type='text' name='$name' value='" . $this->getSemanticId() . "' size='15'>\n";
484         
485         $semantic_id_presets = array (
486             "---" => "",
487             "foaf:name - "._("Full name") => "foaf:name",
488             "foaf:nick - "._("Nickname") => "foaf:nick",
489             "foaf:mbox - "._("E-mail") => "foaf:mbox",
490             "foaf:mbox_sha1sum - "._("Hashed e-mail") => "foaf:mbox_sha1sum",
491             "foaf:img - "._("Picture") => "foaf:img",
492             "foaf:weblog - "._("URL of a blog") => "foaf:weblog",
493             "foaf:homepage - "._("URL of a homepage") => "foaf:homepage"
494         );
495         
496         $html .= "<select onchange=\"this.form.{$name}.value = this.value;\">";
497
498         foreach ($semantic_id_presets as $label => $value) {
499             $value == $this->getSemanticId() ? $selected = 'SELECTED' : $selected = '';
500             $html .= "<option value=\"{$value}\" $selected>{$label}";
501         }
502
503         $html .= "</select>\n"