PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Ingot/JQuery/JqGrid/Column/Decorator/Link.php

https://github.com/ibschreiber/com.ibschreiber.iphp
PHP | 83 lines | 38 code | 13 blank | 32 comment | 7 complexity | 7b5a83857c33919e0d10c239808d8bb7 MD5 | raw file
  1. <?php
  2. /**
  3. * @see Ingot_JQuery_JqGrid_Column_Decorator_Abstract
  4. */
  5. require_once 'Ingot/JQuery/JqGrid/Column/Decorator/Abstract.php';
  6. /**
  7. * Decorate a column which contains HTML links
  8. *
  9. * @package Ingot_JQuery_JqGrid
  10. * @copyright Copyright (c) 2005-2009 Warrant Group Ltd. (http://www.warrant-group.com)
  11. * @author Andy Roberts
  12. */
  13. class Ingot_JQuery_JqGrid_Column_Decorator_Link extends Ingot_JQuery_JqGrid_Column_Decorator_Abstract
  14. {
  15. protected $_options = array();
  16. /**
  17. * Constructor
  18. *
  19. * @return void
  20. */
  21. public function __construct($column, $options = array())
  22. {
  23. $this->_column = $column;
  24. $this->_options = $options;
  25. $this->decorate();
  26. }
  27. /*
  28. * Decorate column to display URL links
  29. *
  30. * @return void
  31. */
  32. public function decorate()
  33. {
  34. if (! isset($this->_options['link'])) {
  35. throw new Ingot_JQuery_JqGrid_Exception('A valid link must be supplied.');
  36. }
  37. if (!isset($this->_options['column'])) {
  38. $this->_options['column'] = array();
  39. } elseif ( (isset($this->_options['column']) && ! is_array($this->_options['column']))) {
  40. $this->_options['column'] = array(
  41. $this->_options['column']
  42. );
  43. }
  44. }
  45. /**
  46. * Build a link contain column values using a string composed of zero or more
  47. * directives as per vsprintf().
  48. *
  49. * Additional columns can be supplied, if the link needs to access different
  50. * column values.
  51. *
  52. * @param array $row
  53. */
  54. public function cellValue($row)
  55. {
  56. // Count the number of arguments to be formatted
  57. $countArg = substr_count($this->_options['link'], '%');
  58. if ($countArg > 0) {
  59. // If no columns have been supplied, format link using current column names
  60. if (count($this->_options['column']) == 0) {
  61. $column = array_fill(1, $countArg, $row[$this->_column->getName()]);
  62. } else {
  63. // If columns have been defined, format link using user defined column names
  64. $column = array_intersect_key($row, array_flip($this->_options['column']));
  65. }
  66. $link = vsprintf($this->_options['link'], $column);
  67. }
  68. return "<a href=\"" . $link . "\">" . $row[$this->getName()] . "</a>";
  69. }
  70. }