PageRenderTime 35ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/app/protected/extensions/zurmoinc/framework/portlets/Portlet.php

https://bitbucket.org/zurmo/zurmo/
PHP | 232 lines | 175 code | 15 blank | 42 comment | 3 complexity | 73fd8b51420d1057646e47510b66fbb8 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
  24. * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
  25. ********************************************************************************/
  26. /**
  27. * Portlets is a model utilized by some view types to display
  28. * information. Portlets have display states on a per user basis
  29. * including hide/collapse and portlet position. The home page dashboard
  30. * and subviews are comprised of portlets. A portlet contains 2 views, a display view
  31. * and a configuration view.
  32. */
  33. class Portlet extends RedBeanModel
  34. {
  35. protected $view;
  36. /**
  37. * TODO
  38. */
  39. public $params;
  40. public static function getByLayoutIdAndUserSortedByColumnIdAndPosition($layoutId, $userId, $params)
  41. {
  42. $portletCollection = array();
  43. assert('is_integer($userId) && $userId >= 1');
  44. $quote = DatabaseCompatibilityUtil::getQuote();
  45. $sql = "select id, {$quote}column$quote, position " .
  46. 'from portlet ' .
  47. "where layoutid = '$layoutId' and _user_id = $userId " .
  48. "order by {$quote}column$quote, position;";
  49. $rows = R::getAll($sql);
  50. if (!empty($rows))
  51. {
  52. foreach ($rows as $row)
  53. {
  54. $portlet = Portlet::getById(intval($row['id']));
  55. $portlet->params = $params;
  56. $portletCollection[intval($row['column'])][intval($row['position'])] = $portlet;
  57. }
  58. }
  59. return $portletCollection;
  60. }
  61. public static function getByLayoutIdAndUserSortedById($layoutId, $userId)
  62. {
  63. $portletCollection = array();
  64. assert('is_integer($userId) && $userId >= 1');
  65. $quote = DatabaseCompatibilityUtil::getQuote();
  66. $sql = "select id, {$quote}column$quote, position " .
  67. 'from portlet ' .
  68. "where layoutid = '$layoutId' and _user_id = $userId " .
  69. 'order by id;';
  70. foreach (R::getAll($sql) as $row)
  71. {
  72. $portlet = Portlet::getById(intval($row['id']));
  73. $portletCollection[$row['id']] = $portlet;
  74. }
  75. return $portletCollection;
  76. }
  77. public static function getByLayoutId($layoutId)
  78. {
  79. $portletCollection = array();
  80. assert('is_string($layoutId)');
  81. $quote = DatabaseCompatibilityUtil::getQuote();
  82. $sql = "select id, {$quote}column$quote, position " .
  83. 'from portlet ' .
  84. "where layoutid = '$layoutId'" .
  85. 'order by id;';
  86. foreach (R::getAll($sql) as $row)
  87. {
  88. $portlet = Portlet::getById(intval($row['id']));
  89. $portletCollection[$row['id']] = $portlet;
  90. }
  91. return $portletCollection;
  92. }
  93. public static function makePortletsUsingMetadataSortedByColumnIdAndPosition($layoutId, $metadata, $user, $params)
  94. {
  95. $portletCollection = array();
  96. foreach ($metadata['global']['columns'] as $column => $columns)
  97. {
  98. foreach ($columns['rows'] as $position => $portletMetadata)
  99. {
  100. $portlet = new Portlet();
  101. $portlet->params = $params;
  102. $portlet->column = $column + 1;
  103. $portlet->position = $position + 1;
  104. $portlet->layoutId = $layoutId;
  105. $portlet->collapsed = false;
  106. $portlet->viewType = $portletMetadata['type'];
  107. $portlet->user = $user;
  108. $portletCollection[$column + 1][$position + 1] = $portlet;
  109. }
  110. }
  111. return $portletCollection;
  112. }
  113. public static function savePortlets($portletCollection)
  114. {
  115. foreach ($portletCollection as $column => $columns)
  116. {
  117. foreach ($columns as $position => $portlet)
  118. {
  119. $saved = $portlet->save();
  120. assert('$saved'); // TODO - deal with this properly.
  121. }
  122. }
  123. }
  124. public static function shiftPositionsBasedOnColumnReduction($portletCollection, $newColumnCount)
  125. {
  126. $currentColumnCount = count($portletCollection);
  127. if (!empty($portletCollection[1]))
  128. {
  129. $maxPositionInColumn1 = count($portletCollection[1]);
  130. }
  131. $shiftToPosition = $maxPositionInColumn1 + 1;
  132. for ($i = ($newColumnCount + 1); $i <= $currentColumnCount; $i++)
  133. {
  134. foreach ($portletCollection[$i] as $position => $portlet)
  135. {
  136. $portlet->column = 1;
  137. $portlet->position = $shiftToPosition;
  138. $portlet->save();
  139. $shiftToPosition++;
  140. }
  141. }
  142. }
  143. /**
  144. * Make a portlet with default values.
  145. */
  146. public static function makePortletUsingViewType($viewType, $uniqueLayoutId, $user)
  147. {
  148. $portlet = new Portlet();
  149. $portlet->column = 1;
  150. $portlet->position = 1;
  151. $portlet->layoutId = $uniqueLayoutId;
  152. $portlet->collapsed = false;
  153. $portlet->viewType = $viewType;
  154. $portlet->user = $user;
  155. $portlet->save();
  156. }
  157. public static function getDefaultMetadata()
  158. {
  159. $metadata[__CLASS__] = array(
  160. 'members' => array(
  161. 'column',
  162. 'position',
  163. 'layoutId',
  164. 'viewType',
  165. 'serializedViewData',
  166. 'collapsed',
  167. ),
  168. 'relations' => array(
  169. 'user' => array(RedBeanModel::HAS_ONE, 'User'),
  170. ),
  171. 'rules' => array(
  172. array('column', 'required'),
  173. array('column', 'type', 'type' => 'number'),
  174. array('position', 'required'),
  175. array('position', 'type', 'type' => 'number'),
  176. array('layoutId', 'required'),
  177. array('layoutId', 'type', 'type' => 'string'),
  178. array('layoutId', 'length', 'max' => 100),
  179. array('viewType', 'required'),
  180. array('viewType', 'type', 'type' => 'number'),
  181. array('serializedViewData', 'type', 'type' => 'string'),
  182. array('collapsed', 'boolean'),
  183. )
  184. );
  185. return $metadata;
  186. }
  187. public function getView()
  188. {
  189. $className = $this->viewType . 'View';
  190. $this->params['portletId'] = $this->id;
  191. $this->view = new $className(unserialize($this->serializedViewData), $this->params, $this->getUniquePortletPageId());
  192. return $this->view;
  193. }
  194. /**
  195. * Gets a unique identifier to allow for
  196. * multiple portlets on the same page.
  197. */
  198. public function getUniquePortletPageId()
  199. {
  200. return $this->layoutId . "_" . $this->id;
  201. }
  202. public function getTitle()
  203. {
  204. return $this->getView()->getTitle();
  205. }
  206. public function renderContent()
  207. {
  208. //return '<div>test</div>';
  209. return $this->getView()->render();
  210. }
  211. public function isEditable()
  212. {
  213. $className = get_class($this->getView());
  214. return $className::canUserConfigure();
  215. }
  216. }
  217. ?>