1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class ElggWidget extends ElggObject {
17:
18: 19: 20: 21: 22:
23: protected function initializeAttributes() {
24: parent::initializeAttributes();
25:
26: $this->attributes['subtype'] = "widget";
27: }
28:
29: 30: 31: 32: 33: 34: 35:
36: public function get($name) {
37:
38: if (array_key_exists($name, $this->attributes)) {
39: return $this->attributes[$name];
40: }
41:
42:
43: $meta = $this->getPrivateSetting($name);
44: if ($meta) {
45: return $meta;
46: }
47:
48:
49: return null;
50: }
51:
52: 53: 54: 55: 56: 57: 58: 59:
60: public function set($name, $value) {
61: if (array_key_exists($name, $this->attributes)) {
62:
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: 77: 78: 79: 80: 81:
82: public function setContext($context) {
83: return $this->setPrivateSetting('context', $context);
84: }
85:
86: 87: 88: 89: 90: 91:
92: public function getContext() {
93: return $this->getPrivateSetting('context');
94: }
95:
96: 97: 98: 99: 100: 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: 113: 114: 115: 116: 117: 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:
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:
151: $this->order = reset($widgets)->order - 10;
152: } elseif ($rank == (count($widgets) - 1)) {
153:
154: $this->order = end($widgets)->order + 10;
155: } else {
156:
157:
158:
159: foreach ($widgets as $index => $widget) {
160: if ($widget->guid == $this->guid) {
161: unset($widgets[$index]);
162: }
163: }
164:
165:
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:
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: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210:
211: public function saveSettings($params) {
212: if (!$this->canEdit()) {
213: return false;
214: }
215:
216:
217:
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:
230: return false;
231: } else {
232: $this->$name = $value;
233: }
234: }
235: $this->save();
236: }
237:
238: return true;
239: }
240: }
241: