PageRenderTime 66ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/s3db3.5.10/pearlib/Structures/DataGrid/Column.php

https://code.google.com/p/s3db/
PHP | 206 lines | 67 code | 16 blank | 123 comment | 8 complexity | beece608d121255384a0dbc3272f2625 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2005 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Andrew Nagy <asnagy@webitecture.org> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Column.php,v 1.12 2005/01/10 15:19:54 asnagy Exp $
  20. /**
  21. * Structures_DataGrid_Column Class
  22. *
  23. * This class represents a single column for the DataGrid.
  24. *
  25. * @version $Revision: 1.12 $
  26. * @author Andrew S. Nagy <asnagy@webitecture.org>
  27. * @access public
  28. * @package Structures_DataGrid
  29. * @category Structures
  30. */
  31. class Structures_DataGrid_Column
  32. {
  33. /**
  34. * The name of the column
  35. * @var string
  36. */
  37. var $columnName;
  38. /**
  39. * The name of the field to map to
  40. * @var string
  41. */
  42. var $fieldName;
  43. /**
  44. * The field name to order by. Optional
  45. * @var array
  46. */
  47. var $orderBy;
  48. /**
  49. * The attributes to use for the cell. Optional
  50. * @var array
  51. */
  52. var $attribs;
  53. /**
  54. * The value to be used if a cell is empty
  55. * @var string
  56. */
  57. var $autoFillValue;
  58. /**
  59. * A function to be called for each cell to modify the output
  60. * @var array
  61. */
  62. var $formatter;
  63. /**
  64. * Constructor
  65. *
  66. * Creates default table style settings
  67. *
  68. * @param string $columnName The name of the column to be printed
  69. * @param string $fieldName The name of the field for the column
  70. * to be mapped to
  71. * @param string $orderBy The field to order the data by
  72. * @param string $attribs The HTML attributes for the TR tag
  73. * @param boolean $autoFill Whether or not to use the autoFill
  74. * @param string $autoFillValue The value to use for the autoFill
  75. * @param string $formatter A defined function to call upon
  76. * rendering to allow for special
  77. * formatting. This allows for
  78. * call-back function to print out a
  79. * link or a form element, or whatever
  80. * you can possibly think of.
  81. * @access public
  82. */
  83. function Structures_DataGrid_Column($columnName, $fieldName = null,
  84. $orderBy = null, $attribs = array(),
  85. $autoFillValue = null,
  86. $formatter = null)
  87. {
  88. $this->columnName = $columnName;
  89. $this->fieldName = $fieldName;
  90. $this->orderBy = $orderBy;
  91. $this->attribs = $attribs;
  92. $this->autoFillValue = $autoFillValue;
  93. $this->formatter = $formatter;
  94. }
  95. /**
  96. * Set Auto Fill Value
  97. *
  98. * Defines a value to be printed if a cell in the column is null.
  99. *
  100. * @param string $str The value to use for the autoFill
  101. * @access public
  102. */
  103. function setAutoFillValue($str)
  104. {
  105. $this->autoFillValue = $str;
  106. }
  107. /**
  108. * Set Formatter
  109. *
  110. * Defines the function and paramters to be called by the formatter method.
  111. *
  112. * @access public
  113. */
  114. function setFormatter($str)
  115. {
  116. $this->formatter = $str;
  117. }
  118. /**
  119. * Formatter
  120. *
  121. * Calls a predefined function to develop custom output for the column. The
  122. * defined function can accept paramaters so that each cell in the column
  123. * can be unique based on the record. The function will also automatically
  124. * receive the record array as a parameter. All parameters passed into the
  125. * function will be in one array.
  126. *
  127. * Example:
  128. * <code>
  129. * <?php
  130. * ...
  131. * $linkTitle = 'Edit';
  132. * $column->formatter = 'printLink($linkTitle=' . $linkTitle . ')';
  133. * $dg->addColumn($column);
  134. * $dg->render();
  135. * function printLink($params) {
  136. * extract($params);
  137. * return '<a href="edit.php?id=' . $record['id'] . ">' . $linkTitle .
  138. * '</a>';
  139. * }
  140. * ?>
  141. * </code>
  142. *
  143. * @access public
  144. * @todo This method needs to be intuituve and more flexible,
  145. * possibly a seperate column object?
  146. */
  147. function formatter($record)
  148. {
  149. // Define any parameters
  150. if ($size = strpos($this->formatter, '(')) {
  151. // Retrieve the name of the function to call
  152. $formatter = substr($this->formatter, 0, $size);
  153. if (strstr($formatter, '->')) {
  154. $formatter = explode('->', $formatter);
  155. } elseif (strstr($formatter, '::')) {
  156. $formatter = explode('::', $formatter);
  157. }
  158. // Build the list of parameters
  159. $length = strlen($this->formatter) - $size - 2;
  160. $parameters = substr($this->formatter, $size + 1, $length);
  161. $parameters = split(',', $parameters);
  162. // Process the parameters
  163. $paramList = array();
  164. $paramList['record'] = $record; // Auto pass the record array in
  165. foreach($parameters as $param) {
  166. $param = str_replace('$', '', $param);
  167. if (strpos($param, '=') != false) {
  168. $vars = split('=', $param);
  169. $paramList[trim($vars[0])] = trim($vars[1]);
  170. } else {
  171. $paramList[$param] = $$param;
  172. }
  173. }
  174. } else {
  175. $formatter = $this->formatter;
  176. $paramList = null;
  177. }
  178. // Call the formatter
  179. if (is_callable($formatter)) {
  180. $result = call_user_func($formatter, $paramList);
  181. } else {
  182. //$result = new PEAR_Error('Unable to process formatter');
  183. $result = false;
  184. PEAR::raiseError('Unable to process formatter', '1',
  185. PEAR_ERROR_TRIGGER);
  186. }
  187. return $result;
  188. }
  189. }
  190. ?>