2 namespace wcf\system\dashboard
;
3 use wcf\data\dashboard\box\DashboardBox
;
4 use wcf\data\
object\type\ObjectTypeCache
;
6 use wcf\system\cache\builder\DashboardBoxCacheBuilder
;
7 use wcf\system\database\util\PreparedStatementConditionBuilder
;
8 use wcf\system\exception\SystemException
;
9 use wcf\system\SingletonFactory
;
11 use wcf\util\ClassUtil
;
14 * Handles dashboard boxes.
16 * @author Alexander Ebert
17 * @copyright 2001-2014 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package com.woltlab.wcf
20 * @subpackage system.dashboard
21 * @category Community Framework
23 class DashboardHandler
extends SingletonFactory
{
25 * list of cached dashboard boxes
26 * @var array<\wcf\data\dashboard\box\DashboardBox>
28 protected $boxCache = null;
31 * configuration options for pages
34 protected $pageCache = null;
37 * @see \wcf\system\SingletonFactory::init()
39 protected function init() {
40 $this->boxCache
= DashboardBoxCacheBuilder
::getInstance()->getData(array(), 'boxes');
41 $this->pageCache
= DashboardBoxCacheBuilder
::getInstance()->getData(array(), 'pages');
45 * Loads the active dashboard boxes for the given object type and page.
47 * @param string $objectType
48 * @param \wcf\page\IPage $page
50 public function loadBoxes($objectType, IPage
$page) {
51 $objectTypeObj = ObjectTypeCache
::getInstance()->getObjectTypeByName('com.woltlab.wcf.user.dashboardContainer', $objectType);
52 if ($objectTypeObj === null) {
53 throw new SystemException("Unable to find object type '".$objectType."' for definition 'com.woltlab.wcf.user.dashboardContainer'");
57 if (isset($this->pageCache
[$objectTypeObj->objectTypeID
]) && is_array($this->pageCache
[$objectTypeObj->objectTypeID
])) {
58 foreach ($this->pageCache
[$objectTypeObj->objectTypeID
] as $boxID) {
63 $contentTemplate = $sidebarTemplate = '';
64 foreach ($boxIDs as $boxID) {
65 $className = $this->boxCache
[$boxID]->className
;
66 if (!ClassUtil
::isInstanceOf($className, 'wcf\system\dashboard\box\IDashboardBox')) {
67 throw new SystemException("'".$className."' does not implement 'wcf\system\dashboard\box\IDashboardbox'");
70 $boxObject = new $className();
71 $boxObject->init($this->boxCache
[$boxID], $page);
73 if ($this->boxCache
[$boxID]->boxType
== 'content') {
74 $contentTemplate .= $boxObject->getTemplate();
77 $sidebarTemplate .= $boxObject->getTemplate();
81 WCF
::getTPL()->assign(array(
82 '__boxContent' => $contentTemplate,
83 '__boxSidebar' => $sidebarTemplate
88 * Sets default values upon installation, you should not call this method
89 * under any other circumstances. If you do not specify a list of box names,
90 * all boxes will be assigned as disabled for given object type.
92 * @param string $objectType
93 * @param array<names> $enableBoxNames
95 public static function setDefaultValues($objectType, array $enableBoxNames = array()) {
98 // no boxes given, aborting
99 if (empty($enableBoxNames)) {
103 // get object type id (cache might be outdated)
104 if (PACKAGE_ID
&& PACKAGE_ID
!= 1) {
105 // reset object type cache
106 ObjectTypeCache
::getInstance()->resetCache();
109 $objectTypeObj = ObjectTypeCache
::getInstance()->getObjectTypeByName('com.woltlab.wcf.user.dashboardContainer', $objectType);
110 if ($objectTypeObj === null) {
111 throw new SystemException("Object type '".$objectType."' is not valid for definition 'com.woltlab.wcf.user.dashboardContainer'");
114 $objectTypeID = $objectTypeObj->objectTypeID
;
116 // select available box ids
117 $conditions = new PreparedStatementConditionBuilder();
118 $conditions->add("boxName IN (?)", array(array_keys($enableBoxNames)));
120 $sql = "SELECT boxID, boxName, boxType
121 FROM wcf".WCF_N
."_dashboard_box
123 $statement = WCF
::getDB()->prepareStatement($sql);
124 $statement->execute($conditions->getParameters());
127 // work-around during WCFSetup
128 $conditions = new PreparedStatementConditionBuilder();
129 $conditions->add("object_type.objectType = ?", array($objectType));
130 $conditions->add("object_type_definition.definitionName = ?", array('com.woltlab.wcf.user.dashboardContainer'));
132 $sql = "SELECT object_type.objectTypeID
133 FROM wcf".WCF_N
."_object_type object_type
134 LEFT JOIN wcf".WCF_N
."_object_type_definition object_type_definition
135 ON (object_type_definition.definitionID = object_type.definitionID)
137 $statement = WCF
::getDB()->prepareStatement($sql);
138 $statement->execute($conditions->getParameters());
139 $row = $statement->fetchArray();
141 $objectTypeID = $row['objectTypeID'];
144 if (!$objectTypeID) {
145 throw new SystemException("Object type '".$objectType."' is not valid for definition 'com.woltlab.wcf.user.dashboardContainer'");
148 // select available box ids
149 $conditions = new PreparedStatementConditionBuilder();
150 $conditions->add("boxName IN (?)", array(array_keys($enableBoxNames)));
152 $sql = "SELECT boxID, boxName, boxType
153 FROM wcf".WCF_N
."_dashboard_box
155 $statement = WCF
::getDB()->prepareStatement($sql);
156 $statement->execute($conditions->getParameters());
160 while ($row = $statement->fetchArray()) {
161 $boxes[$row['boxID']] = new DashboardBox(null, $row);
164 if (!empty($boxes)) {
165 $sql = "UPDATE wcf".WCF_N
."_dashboard_option
166 SET showOrder = showOrder + 1
167 WHERE objectTypeID = ?
168 AND boxID IN (SELECT boxID FROM wcf".WCF_N
."_dashboard_box WHERE boxType = ?)
170 $updateStatement = WCF
::getDB()->prepareStatement($sql);
171 $sql = "INSERT INTO wcf".WCF_N
."_dashboard_option
172 (objectTypeID, boxID, showOrder)
174 $insertStatement = WCF
::getDB()->prepareStatement($sql);
176 WCF
::getDB()->beginTransaction();
177 foreach ($boxes as $boxID => $box) {
179 $updateStatement->execute(array(
182 $enableBoxNames[$box->boxName
]
185 // insert associations
186 $insertStatement->execute(array(
189 $enableBoxNames[$box->boxName
]
192 WCF
::getDB()->commitTransaction();
197 * Clears dashboard box cache.
199 public static function clearCache() {
200 DashboardBoxCacheBuilder
::getInstance()->reset();