root/trunk/wifidog-auth/wifidog/classes/Content.php @ 545

Revision 545, 17.8 KB (checked in by benoitg, 8 years ago)

2005-04-14 Benoit Gr�goire <bock@…>

  • First part of the future content delivery infrastructure. Many files added.
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[545]1<?php
2
3
4/********************************************************************\
5 * This program is free software; you can redistribute it and/or    *
6 * modify it under the terms of the GNU General Public License as   *
7 * published by the Free Software Foundation; either version 2 of   *
8 * the License, or (at your option) any later version.              *
9 *                                                                  *
10 * This program is distributed in the hope that it will be useful,  *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
13 * GNU General Public License for more details.                     *
14 *                                                                  *
15 * You should have received a copy of the GNU General Public License*
16 * along with this program; if not, contact:                        *
17 *                                                                  *
18 * Free Software Foundation           Voice:  +1-617-542-5942       *
19 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
20 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
21 *                                                                  *
22 \********************************************************************/
23/**@file Content.php
24 * @author Copyright (C) 2005 Benoit Grégoire <bock@step.polymtl.ca>,
25 * Technologies Coeus inc.
26 */
27require_once BASEPATH.'include/common.php';
28require_once BASEPATH.'classes/FormSelectGenerator.php';
29
30$class_names = Content :: getAvailableContentTypes();
31foreach ($class_names as $class_name)
32{
33        require_once BASEPATH.'classes/Content/'.$class_name.'.php';
34}
35
36/** Any type of content */
37class Content
38{
39        protected $id;
40        protected $content_row;
41        private $content_type;
42        private $is_trivial_content;
43
44        /** Create a new Content object in the database
45         * @param $content_type Optionnal, the content type to be given to the new object
46         * @param $id Optionnal, the id to be given to the new Content.  If null, a new id will be assigned
47         * @return the newly created Content object, or null if there was an error (an exception is also trown
48         */
49        static function createNewContent($content_type = 'Content', $id = null)
50        {
51                global $db;
52                if (empty ($id))
53                {
54                        $content_id = get_guid();
55                }
56                else
57                {
58                        $content_id = $db->EscapeString($id);
59                }
60
61                if (empty ($content_type))
62                {
63                        throw new Exception(_('Content type is optionnal, but cannot be empty!'));
64                }
65                else
66                {
67                        $content_type = $db->EscapeString($content_type);
68                }
69                $sql = "INSERT INTO content (content_id, content_type) VALUES ('$content_id', '$content_type')";
70
71                if (!$db->ExecSqlUpdate($sql, false))
72                {
73                        throw new Exception(_('Unable to insert new content into database!'));
74                }
75
76                $object = self :: getContent($content_id);
77                /** At least add the current user as the default owner */
78                $object->AddOwner(User :: getCurrentUser());
79                return $object;
80        }
81        /** Get the Content object, specific to it's content type
82         * @param $content_id The content id
83         * @return the Content object, or null if there was an error (an exception is also thrown)
84         */
85        static function getContent($content_id)
86        {
87                global $db;
88                $content_id = $db->EscapeString($content_id);
89                $sql = "SELECT content_type FROM content WHERE content_id='$content_id'";
90                $db->ExecSqlUniqueRes($sql, $row, false);
91                if ($row == null)
92                {
93                        throw new Exception(_("The content with the following id could not be found in the database: ").$content_id);
94                }
95                $content_type = $row['content_type'];
96                $object = new $content_type ($content_id);
97                return $object;
98        }
99        /** Get the list of available content type on the system
100         * @return an array of class names */
101        public static function getAvailableContentTypes()
102        {
103                $dir = BASEPATH.'classes/Content';
104                if ($handle = opendir($dir))
105                {
106                        $tab = Array ();
107                        $i = 0;
108                        /* This is the correct way to loop over the directory. */
109                        while (false !== ($file = readdir($handle)))
110                        {
111                                if ($file != '.' && $file != '..')
112                                {
113                                        $tab[$i] = $file;
114                                        $i ++;
115                                }
116                        }
117                        closedir($handle);
118                        //echo $gfs->genererDeArray($tab, $this->GetStylesheet(), "stylesheet", "Theme::AfficherInterfaceAdmin", true, 'Style par défaut ou style du parent');
119                }
120                else
121                {
122                        throw new Exception(_('Unable to open directory ').$dir);
123                }
124                $tab = str_ireplace('.php', '', $tab);
125                return $tab;
126        }
127
128        /** Get a flexible interface to generate new content objects
129         * @param $user_prefix A identifier provided by the programmer to recognise it's generated html form
130         * @param $content_type If set, the created content will be of this type, otherwise, the user will have to chose
131         * @return html markup
132         */
133        static function getNewContentInterface($user_prefix, $content_type = null)
134        {
135                global $db;
136                $html = '';
137                $available_content_types = self :: getAvailableContentTypes();
138
139                $name = "get_new_content_{$user_prefix}_content_type";
140                if (empty ($content_type))
141                {
142                        $html .= _("Content type: ");
143                        $i = 0;
144                        foreach ($available_content_types as $classname)
145                        {
146                                $tab[$i][0] = $classname;
147                                $tab[$i][1] = $classname;
148                                $i ++;
149                        }
150                        $html .= FormSelectGenerator :: generateFromArray($tab, null, $name, null, false);
151                }
152                else
153                {
154                        if (false === array_search($content_type, $available_content_types, true))
155                        {
156                                throw new Exception(_("The following content type isn't valid: ").$content_type);
157                        }
158                        $html .= "<input type='hidden' name='$name' value='$content_type'>";
159                }
160                $name = "get_new_content_{$user_prefix}_add";
161
162                if ($content_type)
163                {
164                        $value = _("Add a")." $content_type";
165                }
166                else
167                {
168                        $value = _("Add");
169                }
170                $html .= "<input type='submit' name='$name' value='$value' onclick='submit();'>";
171                return $html;
172        }
173
174        /** Get the created Content object, IF one was created
175         * @param $user_prefix A identifier provided by the programmer to recognise it's generated form
176         * @return the Content object, or null if the user didn't greate one
177         */
178        static function processNewContentInterface($user_prefix)
179        {
180                $object = null;
181                $name = "get_new_content_{$user_prefix}_add";
182                if (!empty ($_REQUEST[$name]) && $_REQUEST[$name] == true)
183                {
184                        $name = "get_new_content_{$user_prefix}_content_type";
185                        $content_type = FormSelectGenerator :: getResult($name, null);
186                        $object = self :: createNewContent($content_type);
187                }
188                return $object;
189        }
190
191        private function __construct($content_id)
192        {
193                global $db;
194
195                $content_id = $db->EscapeString($content_id);
196                $sql = "SELECT * FROM content WHERE content_id='$content_id'";
197                $db->ExecSqlUniqueRes($sql, $row, false);
198                if ($row == null)
199                {
200                        throw new Exception(_("The content with the following id could not be found in the database: ").$content_id);
201                }
202                $this->content_row = $row;
203                $this->id = $row['content_id'];
204                $this->content_type = $row['content_type'];
205        }
206
207        /** Get the true object type represented by this isntance
208         * @return an array of class names */
209        public function getContentType()
210        {
211                return $this->content_type;
212        }
213
214        /** Set the object type of this object
215         * Note that after using this, the object must be re-instanciated to have the right type
216         * */
217        private function setContentType($content_type)
218        {
219                global $db;
220                $content_type = $db->EscapeString($content_type);
221                $available_content_types = self :: getAvailableContentTypes();
222                if (false === array_search($content_type, $available_content_types, true))
223                {
224                        throw new Exception(_("The following content type isn't valid: ").$content_type);
225                }
226                $sql = "UPDATE content SET content_type = '$content_type' WHERE content_id='$this->id'";
227
228                if (!$db->ExecSqlUpdate($sql, false))
229                {
230                        throw new Exception(_("Update was unsuccessfull (database error)"));
231                }
232
233        }
234
235        /** Check if a user is one of the owners of the object
236         * @param $user The user to be added to the owners list
237         * @param $is_author Optionnal, true or false.  Set to true if the user is one of the actual authors of the Content
238         * @return true on success, false on failure */
239        public function addOwner(User $user, $is_author = false)
240        {
241                global $db;
242                $content_id = "'".$this->id."'";
243                $user_id = "'".$db->EscapeString($user->getId())."'";
244                if ($is_author == true)
245                {
246                        $is_author = 'TRUE';
247                }
248                else
249                {
250                        $is_author = 'FALSE';
251                }
252                $sql = "INSERT INTO content_has_owners (content_id, user_id, is_author) VALUES ($content_id, $user_id, $is_author)";
253
254                if (!$db->ExecSqlUpdate($sql, false))
255                {
256                        throw new Exception(_('Unable to insert the new Owner into database!'));
257                }
258
259                return true;
260        }
261
262        /** Check if a user is one of the owners of the object
263         * @return true on success, false on failure */
264        public function isOwner(User $user)
265        {
266                echo "<h1>WRITEME</h1>";
267                return false;
268        }
269        /** Get the authors of the Content
270         * @return null or array of User objects */
271        public function getAuthors()
272        {
273                echo "<h1>WRITEME</h1>";
274                return false;
275        }
276        /** Retreives the id of the object
277         * @return The id */
278        public function getId()
279        {
280                return $this->id;
281        }
282
283        /** When a content object is set as trivial, it means that is is used merely to contain it's own data.  No title, description or other data will be set or displayed, during display or administration
284         * @param $is_trivial true or false */
285        public function setIsTrivialContent($is_trivial)
286        {
287                $this->is_trivial_content = $is_trivial;
288        }
289
290        /** Retreives the user interface of this object.  Anything that overrides this method should call the parent method with it's output at the END of processing.
291         * @param $subclass_admin_interface Html content of the interface element of a children
292         * @return The HTML fragment for this interface */
293        public function getUserUI($subclass_user_interface = null)
294        {
295                                $html = '';
296                                                $html .= "<div class='user_ui_container'>\n";
297                $html .= "<div class='user_ui_object_class'>Content (".get_class($this)." instance)</div>\n";
298                                                $html .= $subclass_user_interface;
299                $html .= "</div>\n";
300                return $html;
301        }
302       
303        /** Retreives the admin interface of this object.  Anything that overrides this method should call the parent method with it's output at the END of processing.
304         * @param $subclass_admin_interface Html content of the interface element of a children
305         * @return The HTML fragment for this interface */
306        public function getAdminInterface($subclass_admin_interface = null)
307        {
308                $html = '';
309                $html .= "<div class='admin_container'>\n";
310                $html .= "<div class='admin_class'>Content (".get_class($this)." instance)</div>\n";
311                if ($this->getContentType() == 'Content') /* The object hasn't yet been typed */
312                {
313                        $html .= _("You must select a content type: ");
314                        $i = 0;
315                        foreach (self :: getAvailableContentTypes() as $classname)
316                        {
317                                $tab[$i][0] = $classname;
318                                $tab[$i][1] = $classname;
319                                $i ++;
320                        }
321                        $html .= FormSelectGenerator :: generateFromArray($tab, null, "content_".$this->id."_content_type", "Content", false);
322                }
323                else
324                        if ($this->is_trivial_content == false)
325                        {
326                                /* title */
327                                $html .= "<div class='admin_section_container'>\n";
328                                $html .= "<span class='admin_section_title'>"._("Title:")."</span>\n";
329                                if (empty ($this->content_row['title']))
330                                {
331                                        $html .= self :: getNewContentInterface("title_{$this->id}_new");
332                                }
333                                else
334                                {
335                                        $title = self :: getContent($this->content_row['title']);
336                                        $html .= $title->getAdminInterface();
337                                        $html .= "<div class='admin_section_tools'>\n";
338                                        $name = "content_".$this->id."_title_erase";
339                                        $html .= "<input type='submit' name='$name' value='"._("Delete")."' onclick='submit();'>";
340                                        $html .= "</div>\n";
341                                }
342                                $html .= "</div>\n";
343
344                                /* description */
345                                $html .= "<div class='admin_section_container'>\n";
346                                $html .= "<span class='admin_section_title'>"._("Description:")."</span>\n";
347                                if (empty ($this->content_row['description']))
348                                {
349                                        $html .= self :: getNewContentInterface("description_{$this->id}_new");
350                                }
351                                else
352                                {
353                                        $description = self :: getContent($this->content_row['description']);
354                                        $html .= $description->getAdminInterface();
355                                        $html .= "<div class='admin_section_tools'>\n";
356                                        $name = "content_".$this->id."_description_erase";
357                                        $html .= "<input type='submit' name='$name' value='"._("Delete")."' onclick='submit();'>";
358                                        $html .= "</div>\n";
359                                }
360                                $html .= "</div>\n";
361
362                                /* project_info */
363                                $html .= "<div class='admin_section_container'>\n";
364                                $html .= "<span class='admin_section_title'>"._("Information on this project:")."</span>\n";
365                                if (empty ($this->content_row['project_info']))
366                                {
367                                        $html .= self :: getNewContentInterface("project_info_{$this->id}_new");
368                                }
369                                else
370                                {
371                                        $project_info = self :: getContent($this->content_row['project_info']);
372                                        $html .= $project_info->getAdminInterface();
373                                        $html .= "<div class='admin_section_tools'>\n";
374                                        $name = "content_".$this->id."_project_info_erase";
375                                        $html .= "<input type='submit' name='$name' value='"._("Delete")."' onclick='submit();'>";
376                                        $html .= "</div>\n";
377                                }
378                                $html .= "</div>\n";
379
380                                /* sponsor_info */
381                                $html .= "<div class='admin_section_container'>\n";
382                                $html .= "<span class='admin_section_title'>"._("Sponsor of this project:")."</span>\n";
383                                if (empty ($this->content_row['sponsor_info']))
384                                {
385                                        $html .= self :: getNewContentInterface("sponsor_info_{$this->id}_new");
386                                }
387                                else
388                                {
389                                        $sponsor_info = self :: getContent($this->content_row['sponsor_info']);
390                                        $html .= $sponsor_info->getAdminInterface();
391                                        $html .= "<div class='admin_section_tools'>\n";
392                                        $name = "content_".$this->id."_sponsor_info_erase";
393                                        $html .= "<input type='submit' name='$name' value='"._("Delete")."' onclick='submit();'>";
394                                        $html .= "</div>\n";
395                                }
396                                $html .= "</div>\n";
397
398                        }
399                $html .= $subclass_admin_interface;
400                $html .= "</div>\n";
401                return $html;
402        }
403        /** Process admin interface of this object.  When an object overrides this method, they should call the parent processAdminInterface at the BEGINING of processing.
404       
405        */
406        public function processAdminInterface()
407        {
408                if ($this->getContentType() == 'Content') /* The object hasn't yet been typed */
409                {
410                        $content_type = FormSelectGenerator :: getResult("content_".$this->id."_content_type", "Content");
411                        $this->setContentType($content_type);
412                }
413                else
414                        if ($this->is_trivial_content == false)
415                        {
416                                /* title */
417                                if (empty ($this->content_row['title']))
418                                {
419                                        $title = self :: processNewContentInterface("title_{$this->id}_new");
420                                        if ($title != null)
421                                        {
422                                                $title_id = $title->GetId();
423                                                $this->mBd->ExecSqlUpdate("UPDATE content SET title = '$title_id' WHERE content_id = '$this->id'", FALSE);
424                                        }
425                                }
426                                else
427                                {
428                                        $title = self :: getContent($this->content_row['title']);
429                                        $name = "content_".$this->id."_title_erase";
430                                        if (!empty ($_REQUEST[$name]) && $_REQUEST[$name] == true)
431                                        {
432                                                $this->mBd->ExecSqlUpdate("UPDATE content SET title = NULL WHERE content_id = '$this->id'", FALSE);
433                                                $title->delete();
434                                        }
435                                        else
436                                        {
437                                                $title->processAdminInterface();
438                                        }
439                                }
440
441                                /* title */
442                                if (empty ($this->content_row['description']))
443                                {
444                                        $description = self :: processNewContentInterface("description_{$this->id}_new");
445                                        if ($description != null)
446                                        {
447                                                $description_id = $description->GetId();
448                                                $this->mBd->ExecSqlUpdate("UPDATE content SET description = '$description_id' WHERE content_id = '$this->id'", FALSE);
449                                        }
450                                }
451                                else
452                                {
453                                        $description = self :: getContent($this->content_row['description']);
454                                        $name = "content_".$this->id."_description_erase";
455                                        if (!empty ($_REQUEST[$name]) && $_REQUEST[$name] == true)
456                                        {
457                                                $this->mBd->ExecSqlUpdate("UPDATE content SET description = NULL WHERE content_id = '$this->id'", FALSE);
458                                                $description->delete();
459                                        }
460                                        else
461                                        {
462                                                $description->processAdminInterface();
463                                        }
464                                }
465
466                                /* project_info */
467                                if (empty ($this->content_row['project_info']))
468                                {
469                                        $project_info = self :: processNewContentInterface("project_info_{$this->id}_new");
470                                        if ($project_info != null)
471                                        {
472                                                $project_info_id = $project_info->GetId();
473                                                $this->mBd->ExecSqlUpdate("UPDATE content SET project_info = '$project_info_id' WHERE content_id = '$this->id'", FALSE);
474                                        }
475                                }
476                                else
477                                {
478                                        $project_info = self :: getContent($this->content_row['project_info']);
479                                        $name = "content_".$this->id."_project_info_erase";
480                                        if (!empty ($_REQUEST[$name]) && $_REQUEST[$name] == true)
481                                        {
482                                                $this->mBd->ExecSqlUpdate("UPDATE content SET project_info = NULL WHERE content_id = '$this->id'", FALSE);
483                                                $project_info->delete();
484                                        }
485                                        else
486                                        {
487                                                $project_info->processAdminInterface();
488                                        }
489                                }
490
491                                /* sponsor_info */
492                                if (empty ($this->content_row['sponsor_info']))
493                                {
494                                        $sponsor_info = self :: processNewContentInterface("sponsor_info_{$this->id}_new");
495                                        if ($sponsor_info != null)
496                                        {
497                                                $sponsor_info_id = $sponsor_info->GetId();
498                                                $this->mBd->ExecSqlUpdate("UPDATE content SET sponsor_info = '$sponsor_info_id' WHERE content_id = '$this->id'", FALSE);
499                                        }
500                                }
501                                else
502                                {
503                                        $sponsor_info = self :: getContent($this->content_row['sponsor_info']);
504                                        $name = "content_".$this->id."_sponsor_info_erase";
505                                        if (!empty ($_REQUEST[$name]) && $_REQUEST[$name] == true)
506                                        {
507                                                $this->mBd->ExecSqlUpdate("UPDATE content SET sponsor_info = NULL WHERE content_id = '$this->id'", FALSE);
508                                                $sponsor_info->delete();
509                                        }
510                                        else
511                                        {
512                                                $sponsor_info->processAdminInterface();
513                                        }
514                                }
515
516                        }
517        }
518
519        /** Subscribe to the project
520         * @return true on success, false on failure */
521        public function subscribe(User $user)
522        {
523                echo "<h1>WRITEME</h1>";
524                return false;
525        }
526        /** Unsubscribe to the project
527         * @return true on success, false on failure */
528        public function unsubscribe(User $user)
529        {
530                echo "<h1>WRITEME</h1>";
531                return false;
532        }
533       
534        /** Delete this Content from the database
535         * @todo Implement proper Access control */
536                public function delete()
537        {       
538                global $db;
539                $sql = "DELETE FROM content WHERE content_id='$this->id'";
540                $db->ExecSqlUpdate($sql, false);
541        }
542
543} // End class
544?>
Note: See TracBrowser for help on using the browser.