CMS  Version 3.9
section.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
44 class Section extends DataItem
45 {
46  var $primary_key = "section_id";
47  var $table = "section";
48 
49  // Fields
50 
51  var $fields = array("section_id" => Number,
52  "section" => String,
53  "section_title" => String,
54  "default_template" => String,
55  "default_role" => String,
56  "default_permissions" => String,
57  "default_page" => String,
58  "default_body_class" => String,
59  "site_id" => Number,
60  "use_SSL" => Boolean,
61  "section_type" => String
62  );
63 
64  var $relations = array("SectionModuleXrefs" => SectionModuleXref,
65  "Modules" => Module,
66  "Content" => SectionContent,
67  "Site" => Site
68  );
69 
70  function SectionModuleXrefs($constraint = "ORDER BY position, sort_order")
71  {
72  return $this->getRelatedList(SectionModuleXref, "", $constraint);
73  }
74 
75  function Modules($constraint = "ORDER BY position, sort_order")
76  {
77  return $this->crossReference(Module, SectionModuleXref, $constraint);
78  }
79 
80  function Content($constraint = "")
81  {
82  return $this->getRelatedList(SectionContent, "", $constraint);
83  }
84 
85  function Site()
86  {
87  return $this->getRelated(Site);
88  }
89 
90  function getContentManager()
91  {
93  }
94 
96  {
97  if (!$identifier) return null;
98 
99  $site = Site::getSite();
100 
101  try
102  {
103  return Query::create(Section, "WHERE section=:identifier AND site_id=:site")
104  ->bind(":identifier", $identifier, ":site", $site->site_id)
105  ->executeSingle();
106  }
107  catch(Exception $e)
108  {
109  ComponentManager::fireEvent("SectionNotFound", $identifier);
110  throw new FakoliException("Unknown or ambiguous section '$identifier'");
111  }
112  }
113 
114  var $_content = array();
115 
117  {
118  if ($this->_content[$identifier]) return $this->_content[$identifier];
120 
121  return SectionContentManagerMap::getManager($this)->getContent($this, $identifier);
122  }
123 
125  {
126  $content = is_object($identifier) ? $identifier : $this->getContent($identifier);
127  if ($content->template) return $content->template;
128  return $this->default_template;
129  }
130 
132  {
133  $content = is_object($identifier) ? $identifier : $this->getContent($identifier);
134  if ($content->role) return $content->role;
135  return $this->default_role;
136  }
137 
139  {
140  $content = is_object($identifier) ? $identifier : $this->getContent($identifier);
141  if ($content->permissions) return $content->permissions;
142  return $this->default_permissions;
143  }
144 
145  static function findSection($section_name)
146  {
147  $site = Site::getSite();
148 
149  $sections = Query::create(Section, "WHERE section=:section AND site_id=:site")
150  ->bind(":section", $section_name, ":site", $site->site_id)
151  ->execute();
152 
153  return (count($sections) > 0) ? $sections[0] : null;
154  }
155 
156  function getModulesByPosition($constraint = "ORDER BY position, sort_order")
157  {
158  $modules = Cache::get("section_module_map_{$this->section_id}");
159  if (!$modules)
160  {
161  $join = new InnerJoin();
162  $join->add(Module);
163  $join->add(SectionModuleXref);
164 
165  $result = $join->groupedQuery("WHERE section_id={$this->section_id} $constraint", "SectionModuleXref.position");
166  $modules = extractGroupedJoinResults(Module, $result);
167 
168  // Merge in global modules
169  $globals = groupedQuery(Module, "WHERE global=1", "global_position");
170  foreach($globals as $position => $modulesAtPosition)
171  {
172  if (array_key_exists($position, $modules))
173  {
174  $modules[$position] = removeDuplicates(array_merge($modulesAtPosition, $modules[$position]));
175  }
176  else
177  {
178  $modules[$position] = $modulesAtPosition;
179  }
180  }
181 
182  Cache::put("section_module_map_{$this->section_id}", $modules);
183  }
184 
185  return $modules;
186  }
187 
188  function clearModuleCache()
189  {
190  Cache::invalidate("section_module_map_{$this->section_id}");
191  }
192 
197  function getSectionURL()
198  {
199  global $config;
200 
201  $url = "http" . (($this->use_SSL) ? "s" : "") . "://{$config['http_host']}/";
202  if ($this->section != "/")
203  {
204  $url .= $this->section . "/";
205  }
206  return $url;
207  }
208 
214  {
215  $directory = $this->Site()->getThemeDirectory();
216  $file = $directory . DIRECTORY_SEPARATOR . $this->default_template;
217  trace("Template File: $file", 3);
218  $template = file_get_contents($file);
219 
220  return $template;
221  }
222 
226  function getDefaultPage()
227  {
228  $url = $this->getSectionURL();
229  return $url . $this->default_page;
230  }
231 
236  function getPositions()
237  {
238  $template = $this->getDefaultTemplate();
239 
240  $positionMatches = array();
241 
242  preg_match_all("/\{position:(.*?)\}/", $template, $positionMatches, PREG_PATTERN_ORDER);
243 
244  $positions = $positionMatches[1];
245  sort($positions);
246  return $positions;
247  }
248 }
249 
250 class SectionContent extends DataItem
251 {
252  var $primary_key = "section_content_id";
253  var $table = "section_content";
254 
255  // Fields
256 
257  var $fields = array("section_content_id" => Number,
258  "section_id" => String,
259  "identifier" => String,
260  "role" => String,
261  "permissions" => String,
262  "template" => String,
263  "body_class" => String,
264  "override_page_title" => String,
265  "use_SSL" => Boolean);
266 
267  var $relations = array(
268  "Section" => Section,
269  "SectionContentModuleXrefs" => SectionContentModuleXref
270  );
271 
272  function Section()
273  {
274  return $this->getRelated(Section);
275  }
276 
277 
278  function SectionContentModuleXrefs($constraint = "ORDER BY position, sort_order")
279  {
280  return $this->getRelatedList(SectionContentModuleXref, "", $constraint);
281  }
282 
283  function getModulesByPosition($constraint = "ORDER BY position, sort_order")
284  {
285  if (!$this->section_content_id)
286  {
287  // Mocked SectionContent object
288  return array();
289  }
290 
291  $modules = Cache::get("section_content_module_map_{$this->section_content_id}");
292  if (!$modules)
293  {
294  $join = new InnerJoin();
295  $join->add(Module);
296  $join->add(SectionContentModuleXref);
297 
298  $result = $join->groupedQuery("WHERE section_content_id={$this->section_content_id} $constraint", "SectionContentModuleXref.position");
299  $modules = extractGroupedJoinResults(Module, $result);
300 
301  if (count($modules) > 0)
302  {
303  // Merge in global modules
304  $globals = groupedQuery(Module, "WHERE global=1", "global_position");
305  foreach($globals as $position => $modulesAtPosition)
306  {
307  if (array_key_exists($position, $modules))
308  {
309  $modules[$position] = removeDuplicates(array_merge($modulesAtPosition, $modules[$position]));
310  }
311  else
312  {
313  $modules[$position] = $modulesAtPosition;
314  }
315  }
316  }
317 
318  Cache::put("section_module_map_{$this->section_id}", $modules);
319  }
320 
321  return $modules;
322  }
323 
324  static function getMappedContent($identifier)
325  {
326  $content = Query::create(SectionContent, "WHERE identifier=:i")
327  ->bind(":i", $identifier)
328  ->execute();
329 
330  return $content;
331  }
332 
333 
338  function getTemplate()
339  {
340  global $config;
341 
342  if ($this->template)
343  {
344  $template = file_get_contents("{$config['homedir']}/templates/{$this->template}");
345  }
346  else
347  {
348  $template = $this->Section()->getDefaultTemplate();
349  }
350 
351  return $template;
352  }
353 
358  function getPositions()
359  {
360  $template = $this->getTemplate();
361 
362  $positionMatches = array();
363 
364  preg_match_all("/\{position:(.*?)\}/", $template, $positionMatches, PREG_PATTERN_ORDER);
365 
366  $positions = $positionMatches[1];
367  sort($positions);
368  return $positions;
369  }
370 
374  function getBodyClass()
375  {
376  return $this->body_class ? $this->body_class : $this->Section()->default_body_class;
377  }
378 
387  function addToSection($section_folder)
388  {
389  if ($this->section_id)
390  {
391  throw new FakoliException("Attempt to map section content that is already mapped to a section");
392  }
393 
394  $section = Section::findSection($section_folder);
395  if (!$section)
396  {
397  throw new FakoliException("Unrecognized section '$section_folder'");
398  }
399 
400  if (SectionManager::isInSection($section_folder, $this->identifier)) return;
401 
402  $this->section_id = $section->section_id;
403  $this->save();
404  }
405 }
406 
407 
408 class SectionModuleXref extends DataItem
409 {
410  var $table = "section_module_xref";
411  var $primary_key = "join_id";
412 
413  // Fields
414  var $fields = array("join_id" => Number,
415  "section_id" => Number,
416  "module_id" => Number,
417  "position" => String,
418  "sort_order" => Number);
419 
420  // Relations
421 
422  var $relations = array( "Section" => Section,
423  "Module" => Module);
424 
425  function Section()
426  {
427  return $this->getRelated(Section);
428  }
429 
430  function Module()
431  {
432  return $this->getRelated(Module);
433  }
434 }
435 
436 
437 
438 class SectionContentModuleXref extends DataItem
439 {
440  var $table = "section_content_module_xref";
441  var $primary_key = "join_id";
442 
443  // Fields
444  var $fields = array("join_id" => Number,
445  "section_content_id" => Number,
446  "module_id" => Number,
447  "position" => String,
448  "sort_order" => Number);
449 
450  // Relations
451 
452  var $relations = array( "SectionContent" => SectionContent,
453  "Module" => Module);
454 
455  function SectionContent()
456  {
457  return $this->getRelated(SectionContent);
458  }
459 
460  function Module()
461  {
462  return $this->getRelated(Module);
463  }
464 }
465 ?>
$constraint
$section
Definition: event_form.inc:44
$file
Definition: delete.inc:47
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
Defines the Module class.
Definition: module.inc:57
SectionContentModuleXrefs($constraint="ORDER BY position, sort_order")
Definition: section.inc:278
getBodyClass()
Returns any custom body CSS classes defined for this section content.
Definition: section.inc:374
getModulesByPosition($constraint="ORDER BY position, sort_order")
Definition: section.inc:283
getTemplate()
Loads the default template associated with this section.
Definition: section.inc:338
static getMappedContent($identifier)
Definition: section.inc:324
addToSection($section_folder)
Add this SectionContent to the specified Section.
Definition: section.inc:387
getPositions()
Retrieves an array of the position names defined in the template for this page.
Definition: section.inc:358
static getManager($section)
Retrieves the SectionContentManager object for the specified section.
Section DataItem, defining the data model for sections within a site.
Definition: section.inc:45
$relations
Definition: section.inc:64
Site()
Definition: section.inc:85
getContent($identifier)
Definition: section.inc:116
getRole($identifier)
Definition: section.inc:131
static findSection($section_name)
Definition: section.inc:145
getDefaultPage()
Returns the path to the default page for this section.
Definition: section.inc:226
clearModuleCache()
Definition: section.inc:188
getModulesByPosition($constraint="ORDER BY position, sort_order")
Definition: section.inc:156
static createFromIdentifier($identifier)
Definition: section.inc:95
getSectionURL()
Returns the Section URL.
Definition: section.inc:197
getContentManager()
Definition: section.inc:90
Modules($constraint="ORDER BY position, sort_order")
Definition: section.inc:75
$primary_key
Definition: section.inc:46
SectionModuleXrefs($constraint="ORDER BY position, sort_order")
Definition: section.inc:70
getDefaultTemplate()
Loads the default template associated with this section.
Definition: section.inc:213
getPermissions($identifier)
Definition: section.inc:138
Content($constraint="")
Definition: section.inc:80
getTemplateFile($identifier)
Definition: section.inc:124
getPositions()
Retrieves an array of the position names defined in the template for this page.
Definition: section.inc:236
static isInSection($section_folder, $identifier)
static ensureContentManager()
Definition: site.inc:40
static getSite()
Returns the Site object that describes the currently active site (i.e.
Definition: site.inc:103
global $config
Definition: import.inc:4
$result
$positions
$identifier
Definition: rss.inc:37
if(! $blog->published||! $blog->enable_rss_feed||!checkRole($blog->allow_read)) $url
Definition: rss.inc:58
$tabs section
if(array_key_exists("HTTP_IF_MODIFIED_SINCE", $_SERVER)) $content
Definition: styles.css.inc:24