forked from KodiCMS-Kohana/cms
/
dashboard.php
138 lines (116 loc) · 3.02 KB
/
dashboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php defined( 'SYSPATH' ) or die( 'No direct access allowed.' );
/**
* @package KodiCMS/Dashboard
* @author butschster <butschster@gmail.com>
* @link http://kodicms.ru
* @copyright (c) 2012-2014 butschster
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
class Dashboard {
const WIDGET_BLOCKS_KEY = 'dashboard';
const WIDGET_SETTINGS_KEY = 'dashboard_widget_settings';
/**
*
* @param string $widget_id
* @return \Model_Widget_Decorator_Dashboard|null
*/
public static function get_widget($id, $user_id = NULL)
{
$widget_settings = Model_User_Meta::get(self::WIDGET_SETTINGS_KEY, array(), $user_id);
$widget = Arr::get($widget_settings, $id);
if (!($widget instanceof Model_Widget_Decorator_Dashboard))
{
return NULL;
}
return $widget;
}
/**
*
* @param string $type
* @return Model_Widget_Decorator_Dashboard
*/
public static function add_widget($type, array $data = NULL, $user_id = NULL)
{
$widget_settings = Model_User_Meta::get(self::WIDGET_SETTINGS_KEY, array(), $user_id);
$widget = Widget_Manager::factory($type);
$widget->id = uniqid();
if ($data !== NULL)
{
$widget->set_values($data);
}
$widget_settings[$widget->id] = $widget;
Model_User_Meta::set(self::WIDGET_SETTINGS_KEY, $widget_settings, $user_id);
return $widget;
}
/**
*
* @param string $id
* @param array $data
* @return boolean
*/
public static function update_widget($id, array $data, $user_id = NULL)
{
$widget_settings = Model_User_Meta::get(self::WIDGET_SETTINGS_KEY, array(), $user_id);
$widget = Arr::get($widget_settings, $id);
if ($widget instanceof Model_Widget_Decorator_Dashboard)
{
$widget_settings[$id] = $widget->set_values($data);
Model_User_Meta::set(self::WIDGET_SETTINGS_KEY, $widget_settings, $user_id);
return $widget;
}
return NULL;
}
/**
*
* @param string $id
* @return boolean
*/
public static function delete_widget($id, $user_id = NULL)
{
$widget_settings = Model_User_Meta::get(self::WIDGET_SETTINGS_KEY, array(), $user_id);
unset($widget_settings[$id]);
Model_User_Meta::set(self::WIDGET_SETTINGS_KEY, $widget_settings, $user_id);
return TRUE;
}
/**
*
* @param string $id
* @param string $column
* @return boolean
*/
public static function move_widget($id, $column, $user_id = NULL)
{
$widgets = Model_User_Meta::get(self::WIDGET_BLOCKS_KEY, array(), $user_id);
$found = FALSE;
foreach ($widgets as $data)
{
foreach ($ids as $i => $_id)
{
if ($_id = $id AND $_column != $column)
{
$found = TRUE;
unset($blocks[$_column][$i]);
break;
}
}
}
if ($found === TRUE)
{
$blocks[$column][] = $id;
Model_User_Meta::set(self::WIDGET_BLOCKS_KEY, $blocks, $user_id);
return TRUE;
}
return FALSE;
}
/**
*
* @return boolean
*/
public static function remove_data()
{
return (bool) DB::delete('user_meta')
->where('key', '=', self::WIDGET_SETTINGS_KEY)
->or_where('key', '=', self::WIDGET_BLOCKS_KEY)
->execute();
}
}