Overview

Packages

  • ClipIt
    • clipit
      • api
    • urjc
      • backend
  • Elgg
    • Core
      • Access
      • Authentication
      • Cache
      • Caches
      • Core
      • DataMode
        • Site
      • DataModel
        • Annotations
        • Entities
        • Extender
        • File
        • Importable
        • Loggable
        • Notable
        • Object
        • User
      • DataStorage
      • Exception
      • Exceptions
        • Stub
      • FileStore
        • Disk
      • Groups
      • Helpers
      • HMAC
      • Memcache
      • Metadata
      • Navigation
      • ODD
      • Output
      • Plugins
        • Settings
      • Sessions
      • SocialModel
        • Friendable
        • Locatable
      • WebServicesAPI
      • Widgets
      • XML
      • XMLRPC
    • Exceptions
      • Stub
  • None
  • PHP

Classes

  • ElggWidget
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * ElggWidget
  5:  *
  6:  * Stores metadata in private settings rather than as ElggMetadata
  7:  *
  8:  * @package    Elgg.Core
  9:  * @subpackage Widgets
 10:  *
 11:  * @property-read string $handler internal, do not use
 12:  * @property-read string $column internal, do not use
 13:  * @property-read string $order internal, do not use
 14:  * @property-read string $context internal, do not use
 15:  */
 16: class ElggWidget extends ElggObject {
 17: 
 18:     /**
 19:      * Set subtype to widget.
 20:      *
 21:      * @return void
 22:      */
 23:     protected function initializeAttributes() {
 24:         parent::initializeAttributes();
 25: 
 26:         $this->attributes['subtype'] = "widget";
 27:     }
 28: 
 29:     /**
 30:      * Override entity get and sets in order to save data to private data store.
 31:      *
 32:      * @param string $name Name
 33:      *
 34:      * @return mixed
 35:      */
 36:     public function get($name) {
 37:         // See if its in our base attribute
 38:         if (array_key_exists($name, $this->attributes)) {
 39:             return $this->attributes[$name];
 40:         }
 41: 
 42:         // No, so see if its in the private data store.
 43:         $meta = $this->getPrivateSetting($name);
 44:         if ($meta) {
 45:             return $meta;
 46:         }
 47: 
 48:         // Can't find it, so return null
 49:         return null;
 50:     }
 51: 
 52:     /**
 53:      * Override entity get and sets in order to save data to private data store.
 54:      *
 55:      * @param string $name  Name
 56:      * @param string $value Value
 57:      *
 58:      * @return bool
 59:      */
 60:     public function set($name, $value) {
 61:         if (array_key_exists($name, $this->attributes)) {
 62:             // Check that we're not trying to change the guid!
 63:             if ((array_key_exists('guid', $this->attributes)) && ($name == 'guid')) {
 64:                 return false;
 65:             }
 66: 
 67:             $this->attributes[$name] = $value;
 68:         } else {
 69:             return $this->setPrivateSetting($name, $value);
 70:         }
 71: 
 72:         return true;
 73:     }
 74: 
 75:     /**
 76:      * Set the widget context
 77:      *
 78:      * @param string $context The widget context
 79:      * @return bool
 80:      * @since 1.8.0
 81:      */
 82:     public function setContext($context) {
 83:         return $this->setPrivateSetting('context', $context);
 84:     }
 85: 
 86:     /**
 87:      * Get the widget context
 88:      *
 89:      * @return string
 90:      * @since 1.8.0
 91:      */
 92:     public function getContext() {
 93:         return $this->getPrivateSetting('context');
 94:     }
 95: 
 96:     /**
 97:      * Get the title of the widget
 98:      *
 99:      * @return string
100:      * @since 1.8.0
101:      */
102:     public function getTitle() {
103:         $title = $this->title;
104:         if (!$title) {
105:             global $CONFIG;
106:             $title = $CONFIG->widgets->handlers[$this->handler]->name;
107:         }
108:         return $title;
109:     }
110: 
111:     /**
112:      * Move the widget
113:      *
114:      * @param int $column The widget column
115:      * @param int $rank   Zero-based rank from the top of the column
116:      * @return void
117:      * @since 1.8.0
118:      */
119:     public function move($column, $rank) {
120:         $options = array(
121:             'type' => 'object',
122:             'subtype' => 'widget',
123:             'container_guid' => $this->container_guid,
124:             'limit' => false,
125:             'private_setting_name_value_pairs' => array(
126:                 array('name' => 'context', 'value' => $this->getContext()),
127:                 array('name' => 'column', 'value' => $column)
128:             )
129:         );
130:         $widgets = elgg_get_entities_from_private_settings($options);
131:         if (!$widgets) {
132:             $this->column = (int)$column;
133:             $this->order = 0;
134:             return;
135:         }
136: 
137:         usort($widgets, create_function('$a,$b','return (int)$a->order > (int)$b->order;'));
138: 
139:         // remove widgets from inactive plugins
140:         $widget_types = elgg_get_widget_types($this->context);
141:         $inactive_widgets = array();
142:         foreach ($widgets as $index => $widget) {
143:             if (!array_key_exists($widget->handler, $widget_types)) {
144:                 $inactive_widgets[] = $widget;
145:                 unset($widgets[$index]);
146:             }
147:         }
148: 
149:         if ($rank == 0) {
150:             // top of the column
151:             $this->order = reset($widgets)->order - 10;
152:         } elseif ($rank == (count($widgets) - 1)) {
153:             // bottom of the column of active widgets
154:             $this->order = end($widgets)->order + 10;
155:         } else {
156:             // reorder widgets
157: 
158:             // remove the widget that's being moved from the array
159:             foreach ($widgets as $index => $widget) {
160:                 if ($widget->guid == $this->guid) {
161:                     unset($widgets[$index]);
162:                 }
163:             }
164: 
165:             // split the array in two and recombine with the moved widget in middle
166:             $before = array_slice($widgets, 0, $rank);
167:             array_push($before, $this);
168:             $after = array_slice($widgets, $rank);
169:             $widgets = array_merge($before, $after);
170:             ksort($widgets);
171:             $order = 0;
172:             foreach ($widgets as $widget) {
173:                 $widget->order = $order;
174:                 $order += 10;
175:             }
176:         }
177: 
178:         // put inactive widgets at the bottom
179:         if ($inactive_widgets) {
180:             $bottom = 0;
181:             foreach ($widgets as $widget) {
182:                 if ($widget->order > $bottom) {
183:                     $bottom = $widget->order;
184:                 }
185:             }
186:             $bottom += 10;
187:             foreach ($inactive_widgets as $widget) {
188:                 $widget->order = $bottom;
189:                 $bottom += 10;
190:             }
191:         }
192: 
193:         $this->column = $column;
194:     }
195: 
196:     /**
197:      * Saves the widget's settings
198:      *
199:      * Plugins can override the save mechanism using the plugin hook:
200:      * 'widget_settings', <widget handler identifier>. The widget and
201:      * the parameters are passed. The plugin hook handler should return
202:      * true to indicate that it has successfully saved the settings.
203:      *
204:      * @warning The values in the parameter array cannot be arrays
205:      *
206:      * @param array $params An array of name => value parameters
207:      *
208:      * @return bool
209:      * @since 1.8.0
210:      */
211:     public function saveSettings($params) {
212:         if (!$this->canEdit()) {
213:             return false;
214:         }
215: 
216:         // plugin hook handlers should return true to indicate the settings have
217:         // been saved so that default code does not run
218:         $hook_params = array(
219:             'widget' => $this,
220:             'params' => $params
221:         );
222:         if (elgg_trigger_plugin_hook('widget_settings', $this->handler, $hook_params, false) == true) {
223:             return true;
224:         }
225: 
226:         if (is_array($params) && count($params) > 0) {
227:             foreach ($params as $name => $value) {
228:                 if (is_array($value)) {
229:                     // private settings cannot handle arrays
230:                     return false;
231:                 } else {
232:                     $this->$name = $value;
233:                 }
234:             }
235:             $this->save();
236:         }
237: 
238:         return true;
239:     }
240: }
241: 
API documentation generated by ApiGen 2.8.0