PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/turba/lib/View/List.php

https://github.com/wrobel/horde
PHP | 496 lines | 275 code | 44 blank | 177 comment | 37 complexity | a65a73353b68ae842687b13bb9442271 MD5 | raw file
Possible License(s): BSD-2-Clause, AGPL-1.0, LGPL-2.1, LGPL-3.0, BSD-3-Clause, LGPL-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * The Turba_View_List:: class provides an interface for objects that
  4. * visualize Turba_List objects.
  5. *
  6. * Copyright 2000-2012 Horde LLC (http://www.horde.org/)
  7. *
  8. * See the enclosed file LICENSE for license information (ASL). If you did
  9. * did not receive this file, see http://www.horde.org/licenses/apache.
  10. *
  11. * @author Chuck Hagenbuch <chuck@horde.org>
  12. * @author Jon Parise <jon@csh.rit.edu>
  13. * @category Horde
  14. * @license http://www.horde.org/licenses/apache ASL
  15. * @package Turba
  16. */
  17. class Turba_View_List implements Countable
  18. {
  19. /**
  20. * The Turba_List object that we are visualizing.
  21. *
  22. * @var Turba_List
  23. */
  24. public $list;
  25. /**
  26. * Show/hide "mark" column in the display.
  27. *
  28. * @var boolean
  29. */
  30. public $showMark = false;
  31. /**
  32. * Show/hide "edit" column in the display.
  33. *
  34. * @var boolean
  35. */
  36. public $showEdit = false;
  37. /**
  38. * Show/hide "vcard" column in the display.
  39. *
  40. * @var boolean
  41. */
  42. public $showVcard = false;
  43. /**
  44. * Show/hide "group" column in the display.
  45. *
  46. * @var boolean
  47. */
  48. public $showGroup = false;
  49. /**
  50. * Show/hide "sort" column in the display.
  51. *
  52. * @var boolean
  53. */
  54. public $showSort = false;
  55. /**
  56. * Type of list.
  57. *
  58. * @var string
  59. */
  60. public $type;
  61. /**
  62. * The HTML renderer.
  63. *
  64. * @var Horde_Core_Ui_VarRenderer_Html
  65. */
  66. public $renderer;
  67. /**
  68. * A Horde_Variables object.
  69. *
  70. * @var Horde_Variables
  71. */
  72. public $vars;
  73. /**
  74. * A list of Horde_Form_Variable objects.
  75. *
  76. * @var array
  77. */
  78. public $variables = array();
  79. /**
  80. * A dummy form object.
  81. *
  82. * @var Horde_Form
  83. */
  84. public $form = null;
  85. /**
  86. * Which columns to render
  87. *
  88. * @var array
  89. */
  90. public $columns;
  91. /**
  92. * Constructs a new Turba_View_List object.
  93. *
  94. * @param Turba_List $list List of contacts to display.
  95. * @param array $controls Which icons to display
  96. * @param array $columns The list of columns to display
  97. *
  98. * @return Turba_View_List
  99. */
  100. public function __construct($list, array $controls = null,
  101. array $columns = null)
  102. {
  103. if (is_null($controls)) {
  104. $controls = array(
  105. 'Mark' => true,
  106. 'Edit' => true,
  107. 'Vcard' => true,
  108. 'Group' => true,
  109. 'Sort' => true
  110. );
  111. }
  112. $this->columns = $columns;
  113. $this->list = $list;
  114. $this->setControls($controls);
  115. $this->renderer = Horde_Core_Ui_VarRenderer::factory('Html');
  116. $this->vars = new Horde_Variables();
  117. }
  118. /**
  119. * Set which controls are shown by the display templates.
  120. *
  121. * @param array $controls
  122. */
  123. public function setControls(array $controls)
  124. {
  125. foreach ($controls as $control => $show) {
  126. $key = 'show' . $control;
  127. $this->$key = (bool)$show;
  128. }
  129. }
  130. /**
  131. *
  132. * @param string $type
  133. */
  134. public function setType($type)
  135. {
  136. $this->type = $type;
  137. }
  138. /**
  139. *
  140. * @return string
  141. */
  142. public function getType()
  143. {
  144. return $this->type;
  145. }
  146. /**
  147. * @TODO: these should be injected when we refactor to Horde_View
  148. * @global $prefs
  149. * @global $session
  150. * @global $default_source
  151. * @global $copymove_source_options
  152. */
  153. public function display()
  154. {
  155. global $prefs, $session, $default_source, $copymove_source_options;
  156. $driver = $GLOBALS['injector']
  157. ->getInstance('Turba_Factory_Driver')
  158. ->create($default_source);
  159. $hasDelete = $driver->hasPermission(Horde_Perms::DELETE);
  160. $hasEdit = $driver->hasPermission(Horde_Perms::EDIT);
  161. $hasExport = ($GLOBALS['conf']['menu']['import_export'] && !empty($GLOBALS['cfgSources'][$default_source]['export']));
  162. list($addToList, $addToListSources) = $this->getAddSources();
  163. $viewurl = Horde::url('browse.php')->add(array(
  164. 'key' => Horde_Util::getFormData('key'),
  165. 'url' => Horde::selfUrl(true, false, true)
  166. ));
  167. if ($this->type == 'search') {
  168. $page = Horde_Util::getFormData('page', 0);
  169. $numitem = count($this);
  170. $maxpage = $prefs->getValue('maxpage');
  171. $perpage = $prefs->getValue('perpage');
  172. $min = $page * $perpage;
  173. while ($min > $numitem) {
  174. $page--;
  175. $min = $page * $perpage;
  176. }
  177. $max = $min + $perpage;
  178. $start = ($page * $perpage) + 1;
  179. $end = min($numitem, $start + $perpage - 1);
  180. $listHtml = $this->getPage($numDisplayed, $min, $max);
  181. $crit = array();
  182. if ($session->get('turba', 'search_mode') == 'advanced') {
  183. $map = $driver->getCriteria();
  184. foreach ($map as $key => $value) {
  185. if ($key != '__key') {
  186. $val = Horde_Util::getFormData($key);
  187. if (!empty($val)) {
  188. $crit[$key] = $val;
  189. }
  190. }
  191. }
  192. }
  193. $params = array_merge($crit, array(
  194. 'criteria' => Horde_Util::getFormData('criteria'),
  195. 'val' => Horde_Util::getFormData('val'),
  196. 'source' => Horde_Util::getFormData('source', $default_source)
  197. ));
  198. $viewurl = Horde::url('search.php')->add($params);
  199. $vars = Horde_Variables::getDefaultVariables();
  200. $pager = new Horde_Core_Ui_Pager('page', $vars,
  201. array('num' => $numitem,
  202. 'url' => $viewurl,
  203. 'page_limit' => $maxpage,
  204. 'perpage' => $perpage));
  205. $pagerHeader = 'numPager.inc';
  206. } else {
  207. $page = Horde_Util::getFormData('page', '*');
  208. if (!preg_match('/^[A-Za-z*]$/', $page)) {
  209. $page = '*';
  210. }
  211. if (count($this) > $prefs->getValue('perpage')) {
  212. $page = Horde_Util::getFormData('page', 'A');
  213. if (!preg_match('/^[A-Za-z*]$/', $page)) {
  214. $page = 'A';
  215. }
  216. }
  217. $listHtml = $this->getAlpha($numDisplayed, $page);
  218. $pagerHeader = 'alphaPager.inc';
  219. }
  220. if ($numDisplayed) {
  221. require TURBA_TEMPLATES . '/browse/actions.inc';
  222. require TURBA_TEMPLATES . '/list/' . $pagerHeader;
  223. echo $listHtml;
  224. } else {
  225. require TURBA_TEMPLATES . '/list/' . $pagerHeader;
  226. echo '<p><em>' . _("No matching contacts") . '</em></p>';
  227. }
  228. }
  229. /**
  230. * Renders the list contents into an HTML view.
  231. *
  232. * @param integer $numDisplayed Ouptut parameter - the number of rows
  233. * rendered.
  234. * @param integer $min Minimum number of rows to display.
  235. * @param integer $max Maximum number of rows to display.
  236. *
  237. * @return string HTML to echo.
  238. */
  239. public function getPage(&$numDisplayed, $min = 0, $max = null)
  240. {
  241. if (is_null($max)) {
  242. $max = count($this);
  243. }
  244. return $this->_get($numDisplayed,
  245. new Turba_View_List_PageFilter($min, $max));
  246. }
  247. /**
  248. * Renders the list contents that match $alpha into an HTML view.
  249. *
  250. * @param integer $numDisplayed This will be set to the number of contacts
  251. * in the view.
  252. * @param string $alpha The letter to display.
  253. *
  254. * @return string HTML of the list.
  255. */
  256. public function getAlpha(&$numDisplayed, $alpha)
  257. {
  258. return $this->_get($numDisplayed,
  259. new Turba_View_List_AlphaFilter($alpha));
  260. }
  261. /**
  262. * Retrieves a column's name
  263. *
  264. * @param integer $i The zero-basd index of the column
  265. *
  266. * @return string
  267. */
  268. public function getColumnName($i)
  269. {
  270. return Turba::getColumnName($i, $this->columns);
  271. }
  272. /**
  273. * @param integer $i The zero-based index of the column
  274. */
  275. public function getSortInfoForColumn($i)
  276. {
  277. $sortorder = Turba::getPreferredSortOrder();
  278. $column_name = $this->getColumnName($i);
  279. $i = 0;
  280. foreach ($sortorder as $sortfield) {
  281. if ($column_name == $sortfield['field']) {
  282. return array_merge($sortfield, array('rank' => $i));
  283. }
  284. $i++;
  285. }
  286. return null;
  287. }
  288. /**
  289. * @param integer $i
  290. * @param string $title
  291. *
  292. * @return string
  293. */
  294. public function getColumnSortImage($i, $title = null)
  295. {
  296. if (is_null($title)) {
  297. $title = _("Sort Direction");
  298. }
  299. $sortdir = $this->getColumnSortDirection($i);
  300. if ($this->isPrimarySortColumn($i)) {
  301. return Horde::img($sortdir ? 'za.png' : 'az.png', $title);
  302. } else {
  303. return Horde::img($sortdir ? 'za_secondary.png' : 'az_secondary.png', _("Sort Direction"));
  304. }
  305. }
  306. /**
  307. * Retrieves a natural language description of the sort order
  308. *
  309. * @return string
  310. */
  311. public function getSortOrderDescription()
  312. {
  313. $description = array();
  314. $sortorder = Turba::getPreferredSortOrder();
  315. foreach ($sortorder as $elt) {
  316. $field = $elt['field'];
  317. if ($field == 'lastname') {
  318. $field = 'name';
  319. }
  320. $description[] = $GLOBALS['attributes'][$field]['label'];
  321. }
  322. return join(', ', $description);
  323. }
  324. /**
  325. * @param integer $i The zero-based index of the column
  326. */
  327. public function getColumnSortDirection($i)
  328. {
  329. $result = $this->getSortInfoForColumn($i);
  330. if (is_null($result)) {
  331. return null;
  332. }
  333. return $result['ascending'] ? 0 : 1;
  334. }
  335. /**
  336. * Determines whether we are sorting on the specified column
  337. *
  338. * @param integer $i The zero-based column index
  339. *
  340. * @return boolean
  341. */
  342. public function isSortColumn($i)
  343. {
  344. return !is_null($this->getSortInfoForColumn($i));
  345. }
  346. /**
  347. * Determines whether this is the first column to sort by
  348. *
  349. * @param integer $i The zero-based column index
  350. *
  351. * @return boolean
  352. */
  353. public function isPrimarySortColumn($i)
  354. {
  355. $result = $this->getSortInfoForColumn($i);
  356. if (is_null($result)) {
  357. return false;
  358. }
  359. return ($result['rank'] == 0);
  360. }
  361. /**
  362. * @param integer $numDisplayed
  363. * @param object $filter A Turba_View_List filter object
  364. *
  365. * @return string
  366. */
  367. protected function _get(&$numDisplayed, $filter)
  368. {
  369. ob_start();
  370. $width = floor(90 / (count($this->columns) + 1));
  371. $own = $GLOBALS['prefs']->getValue('own_contact');
  372. if (strpos($own, ';')) {
  373. list($own_source, $own_id) = explode(';', $own);
  374. } else {
  375. $own_source = $own_id = null;
  376. }
  377. include TURBA_TEMPLATES . '/browse/column_headers.inc';
  378. $numDisplayed = 0;
  379. $this->list->reset();
  380. while ($ob = $this->list->next()) {
  381. if ($filter->skip($ob)) {
  382. continue;
  383. }
  384. include TURBA_TEMPLATES . '/browse/row.inc';
  385. $numDisplayed++;
  386. }
  387. include TURBA_TEMPLATES . '/browse/column_footers.inc';
  388. return ob_get_clean();
  389. }
  390. /**
  391. */
  392. public function getAddSources()
  393. {
  394. global $addSources;
  395. // Create list of lists for Add to.
  396. $addToList = array();
  397. $addToListSources = array();
  398. foreach ($addSources as $src => $srcConfig) {
  399. if (!empty($srcConfig['map']['__type'])) {
  400. $addToListSources[] = array('key' => '',
  401. 'name' => '&nbsp;&nbsp;' . htmlspecialchars($srcConfig['title']),
  402. 'source' => htmlspecialchars($src));
  403. $srcDriver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($src);
  404. try {
  405. $listList = $srcDriver->search(
  406. array('__type' => 'Group'),
  407. array(
  408. array(
  409. 'field' => 'name',
  410. 'ascending' => true
  411. )
  412. ),
  413. 'AND',
  414. array('name')
  415. );
  416. $listList->reset();
  417. $currentList = Horde_Util::getFormData('key');
  418. while ($listObject = $listList->next()) {
  419. if ($listObject->getValue('__key') != $currentList) {
  420. $addToList[] = array(
  421. 'name' => htmlspecialchars($listObject->getValue('name')),
  422. 'source' => htmlspecialchars($src),
  423. 'key' => htmlspecialchars($listObject->getValue('__key'))
  424. );
  425. }
  426. }
  427. } catch (Turba_Exception $e) {
  428. $GLOBALS['notification']->push($e, 'horde.error');
  429. }
  430. }
  431. }
  432. if ($addToListSources) {
  433. if ($addToList) {
  434. array_unshift($addToList, '- - - - - - - - -');
  435. }
  436. $addToList = array_merge(array(_("Create a new Contact List in:")), $addToListSources, $addToList);
  437. $addToListSources = null;
  438. }
  439. return array($addToList, $addToListSources);
  440. }
  441. /* Countable methods. */
  442. /**
  443. * Returns the number of Turba_Objects that are in the list. Use this to
  444. * hide internal implementation details from client objects.
  445. *
  446. * @return integer The number of objects in the list.
  447. */
  448. public function count()
  449. {
  450. return $this->list->count();
  451. }
  452. }