SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Widgets/Widget.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\Widgets;
  15.  
  16.  
  17.  
  18. /**
  19. * Base class for widgets
  20. **/
  21. abstract class Widget {
  22.  
  23. /**
  24.   * Settings - These get automatically set in the admin and loaded on the front-end
  25.   **/
  26. protected $settings = [];
  27.  
  28.  
  29. /**
  30.   * The friendly name, for display in the admin
  31.   **/
  32. protected $friendly_name = 'Widget';
  33.  
  34.  
  35. /**
  36.   * A description, for display in the admin
  37.   **/
  38. protected $friendly_desc = '';
  39.  
  40.  
  41. /**
  42.   * Array of default settings for new widgets
  43.   */
  44. protected $default_settings = [];
  45.  
  46.  
  47. /**
  48.   * Optional HTML H2 heading that's rendered on front-end view
  49.   */
  50. protected $heading = '';
  51.  
  52.  
  53. /**
  54.   * Imports the settings
  55.   *
  56.   * @param array $settings The widget settings
  57.   **/
  58. public function importSettings(array $settings)
  59. {
  60. foreach ($this->default_settings as $key => $val) {
  61. if (!isset($settings[$key]) or $settings[$key] === '') {
  62. $settings[$key] = $val;
  63. }
  64. }
  65. $this->settings = $settings;
  66. $this->cleanupSettings();
  67. }
  68.  
  69.  
  70. /**
  71.   * Return the currently loaded settings for the widget
  72.   *
  73.   * @return array The widget settings
  74.   */
  75. public function getSettings()
  76. {
  77. return $this->settings;
  78. }
  79.  
  80.  
  81. /**
  82.   * Gets the english name of this widget
  83.   **/
  84. public function getFriendlyName()
  85. {
  86. return $this->friendly_name;
  87. }
  88.  
  89. /**
  90.   * Gets the english name of this widget
  91.   **/
  92. public function getFriendlyDesc()
  93. {
  94. return $this->friendly_desc;
  95. }
  96.  
  97.  
  98. /**
  99.   * Set widget title
  100.   *
  101.   * @param string $str Widget title (heading)
  102.   * @return void
  103.   */
  104. public function setTitle($str)
  105. {
  106. $this->heading = $str;
  107. }
  108.  
  109.  
  110. /**
  111.   * Returns the title to use for the widget - should not contain HTML
  112.   **/
  113. public function getTitle()
  114. {
  115. if (!empty($this->heading)) {
  116. return $this->heading;
  117. }
  118. return null;
  119. }
  120.  
  121. /**
  122.   * Returns the default settings for the widget
  123.   **/
  124. public final function getDefaultSettings()
  125. {
  126. return $this->default_settings;
  127. }
  128.  
  129.  
  130. /**
  131.   * Run after import of settings, allows extending widgets to clean up settings which may
  132.   * have bad values for some reason
  133.   *
  134.   * @return void
  135.   */
  136. public function cleanupSettings()
  137. {
  138. }
  139.  
  140.  
  141. /**
  142.   * This function should return the content of the widget that is used on the front-end of the website.
  143.   * The content should be returned as well-formed HTML.
  144.   * The content will be contained within a DIV element,
  145.   * which has the class 'widget', and the class 'widget-[WIDGETNAME]' applied to it.
  146.   *
  147.   * Styles and Javascript can be loaded with the {@see needs} helper, usually the {@see Needs::fileGroup} function is most useful.
  148.   *
  149.   * @param int $orientation The orientation of the widget.
  150.   * Will be one of the constants defined in the {@see WidgetArea} class.
  151.   * ORIENTATION_TALL - used for sidebars, and other mainly tall areas
  152.   * ORIENTATION_WIDE - used for wide areas, like the main content area
  153.   *
  154.   * @return string The output HTML.
  155.   *
  156.   * @tag api
  157.   * @tag widget-api
  158.   **/
  159. abstract public function render ($orientation);
  160.  
  161.  
  162. /**
  163.   * This function should return the content of the widget settings form.
  164.   * The content should be returned as well-formed HTML.
  165.   * The content will be contained within a DIV element, which is inside a FORM element.
  166.   *
  167.   * This function will be called from an AJAX request, so {@see Needs} cannot be used.
  168.   * Inline CSS and Javascript should be loaded correctly by the jQuery library.
  169.   *
  170.   * Form fields *must* be generated using the {@see Form} helper, to ensure correct field
  171.   * prefix as well as reloading of field values
  172.   *
  173.   * @return string The output HTML.
  174.   **/
  175. public function getSettingsForm()
  176. {
  177. return '<p><em>This add-on does not have any settings.</em></p>';
  178. }
  179.  
  180.  
  181. /**
  182.   * This function should return a URL which can be used to edit the *content* which is
  183.   * displayed by this widget.
  184.   *
  185.   * Most widgets show content using a category selection box; the URL should in that
  186.   * case direct the user to the contents list for that category.
  187.   *
  188.   * Return NULL if there isn't an appropriate URL to use.
  189.   **/
  190. public function getEditUrl()
  191. {
  192. return null;
  193. }
  194.  
  195.  
  196. /**
  197.   * This function should return a summary of what this widget's *content* is.
  198.   * Don't specify the widget name; we already know that!
  199.   *
  200.   * Most widgets show content using a category selection box; in that case
  201.   * the most useful thing is probably the category name, perhaps also the
  202.   * order and limit.
  203.   *
  204.   * Return NULL if there isn't an appropriate label to use.
  205.   **/
  206. public function getInfoLabels()
  207. {
  208. return null;
  209. }
  210.  
  211.  
  212. /**
  213.   * Is there sufficent content to add this addon at the moment?
  214.   * If this widget is not available, a string message should be returned
  215.   * If this widget *is* available, NULL should be returned.
  216.   **/
  217. public function getNotAvailableReason()
  218. {
  219. return null;
  220. }
  221.  
  222. }
  223.