SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/WidgetArea.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use Kohana;
  17.  
  18. /**
  19. * Stores information about an individual widget area, including the names of all widgets
  20. * that are allowed to be in this area.
  21. **/
  22. class WidgetArea
  23. {
  24. private $name;
  25. private $nice_name;
  26. private $index;
  27. private $widgets;
  28. private $defaults;
  29. private $orientation;
  30. private $embed;
  31.  
  32. const ORIENTATION_TALL = 1;
  33. const ORIENTATION_WIDE = 2;
  34. const ORIENTATION_EMAIL = 3;
  35.  
  36. /**
  37.   * Class names which are output in the wrapper div
  38.   **/
  39. public static $orientation_classes = array(
  40. self::ORIENTATION_TALL => 'tall',
  41. self::ORIENTATION_WIDE => 'wide',
  42. self::ORIENTATION_EMAIL => 'email',
  43. );
  44.  
  45.  
  46. /**
  47.   * Constructor
  48.   *
  49.   * @param string $name The name of the widget area
  50.   * @param int $index The numerical index of the widget area in the config file
  51.   **/
  52. public function __construct($name, $index)
  53. {
  54. $this->name = $name;
  55. $this->nice_name = $name;
  56. $this->index = $index;
  57. $this->widgets = array();
  58. $this->embed = false;
  59. }
  60.  
  61. /**
  62.   * Returns the name of this widget area
  63.   **/
  64. public function getName()
  65. {
  66. return $this->name;
  67. }
  68.  
  69. /**
  70.   * Returns the nice name of this widget area
  71.   **/
  72. public function getNiceName()
  73. {
  74. return Enc::html($this->nice_name);
  75. }
  76.  
  77.  
  78. /**
  79.   * Sets the nice name of this widget area
  80.   *
  81.   * @param string $name The nice name of the widget
  82.   **/
  83. public function setNiceName($name)
  84. {
  85. $this->nice_name = $name;
  86. }
  87.  
  88. /**
  89.   * Sets the orientation of the widget area
  90.   *
  91.   * @param int $value The new orientation
  92.   **/
  93. public function setOrientation($value)
  94. {
  95. $this->orientation = $value;
  96. }
  97.  
  98. /**
  99.   * If true, the widget area is an 'embedding' area.
  100.   **/
  101. public function setEmbed($embed)
  102. {
  103. $this->embed = $embed;
  104. }
  105.  
  106. /**
  107.   * If the widget area is an embedding area
  108.   **/
  109. public function isEmbed()
  110. {
  111. return $this->embed;
  112. }
  113.  
  114. /**
  115.   * Gets the orientation of the widget area
  116.   **/
  117. public function getOrientation()
  118. {
  119. return $this->orientation;
  120. }
  121.  
  122. /**
  123.   * Returns the index of this widget area
  124.   **/
  125. public function getIndex()
  126. {
  127. return $this->index;
  128. }
  129.  
  130. /**
  131.   * Adds a widget to the list of widgets allowed for this area
  132.   *
  133.   * @param string $widget_name The name of the widget to add
  134.   **/
  135. public function addWidget($widget_name)
  136. {
  137. if (! in_array($widget_name, $this->widgets)) {
  138. $this->widgets[] = $widget_name;
  139. }
  140. }
  141.  
  142. /**
  143.   * Gets the list of allowed widgets
  144.   **/
  145. public function getWidgets()
  146. {
  147. return $this->widgets;
  148. }
  149.  
  150.  
  151. /**
  152.   * Adds a widget to the list of widgets allowed for this area
  153.   *
  154.   * @param string $widget_name The name of the widget to add
  155.   **/
  156. public function addDefault($widget_name, $settings)
  157. {
  158. Widgets::add($this->index, $widget_name, $settings);
  159. }
  160.  
  161.  
  162. /**
  163.   * Returns the widget area which has the specified name.
  164.   * Uses the widget areas defined in the sprout config.
  165.   *
  166.   * @param string $name The name of the widget area to use.
  167.   **/
  168. public static function findAreaByName($name)
  169. {
  170. $areas = Kohana::config('sprout.widget_areas');
  171.  
  172. foreach ($areas as $index => $area) {
  173. if ($area->name == $name) return $area;
  174. }
  175.  
  176. return null;
  177. }
  178. }
  179.  
  180.  
  181.