Source for file data-defs.php

Documentation is available at data-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: data-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for managing DATA of various types. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package utils */* Dat_keyvalues
  27. * This class is a simple container class. It specifically deals
  28. * with a collection of key=>value pairs in an internal array,
  29. * and provides a few high-level functions to set these and
  30. * read them out. Used in combo elements for forms.
  31. * @package utils
  32. */
  33. class dat_keyvalues {
  34. /** The array containing data in key=>value pairs */
  35.  
  36. var $data = array();
  37. /** The total data items in our array */
  38.  
  39. var $count = 0;
  40. // ....................................................................
  41. /** Constructor */
  42.  
  43. function dat_keyvalues() {}
  44. // ....................................................................
  45. /**
  46. * Returns status of data.
  47. * Returns true if the container has some data in it, else
  48. * returns false.
  49. * @returns bool Status of data in the container.
  50. */
  51. function hasdata() {
  52. return (isset($this->data) && count($this->data) > 0);
  53. }
  54. // ....................................................................
  55. /**
  56. * Add query data
  57. * Add items from a query. The 'keyfield' parameter contains the
  58. * name of the field to get key value from, and 'displayfields' is
  59. * expected to contain a list of fields separated by the pipe "|"
  60. * char. Literals are also supported by using a pseudo-fieldname in
  61. * the list prefixed by a "#" char. In this case everything after the
  62. * "#" will be used as the content of that field.
  63. * @param object $query Pointer to an executed Axyl query object
  64. * @param string $keyfield Name of the key field to use.
  65. * @param string $displayfields List of display fields separated with "|".
  66. * @see add_item()
  67. */
  68. function add_querydata($query, $keyfield, $displayfields) {
  69. if ($query->hasdata) {
  70. $dispflds = explode("|", $displayfields);
  71. do {
  72. $key = $query->field($keyfield);
  73. $value = "";
  74. reset($dispflds);
  75. foreach ($dispflds as $fld) {
  76. if (substr($fld, 0, 1) == "#") {
  77. $value .= substr($fld, 1);
  78. }
  79. elseif (substr($fld, 0, 1) == "^") {
  80. $fld = substr($fld, 1);
  81. $value .= strtoupper($query->field($fld));
  82. }
  83. else {
  84. $value .= $query->field($fld);
  85. }
  86. }
  87. $this->additem($key, $value);
  88. } while ($query->get_next());
  89. }
  90. }
  91. // ....................................................................
  92. /**
  93. * Adds a key=>value pair item into the collection.
  94. * @param mixed $key The key value to use
  95. * @param mixed $value The value to assign to the key
  96. */
  97. function additem($key, $value) {
  98. $this->data[$key] = $value;
  99. $this->count += 1;
  100. }
  101. // ....................................................................
  102. /** Clears all key=>value pair items from the collection. */
  103.  
  104. function clearitems() {
  105. $this->data = array();
  106. $this->count = 0;
  107. }
  108. // ....................................................................
  109. /**
  110. * Reset the data
  111. * Resets the internal array to the start. This is used when
  112. * you need to repeatedly traverse the data.
  113. */
  114. function reset() {
  115. if (isset($this->data)) reset($this->data);
  116. }
  117. // ....................................................................
  118. /**
  119. * Find ordinal array index
  120. * Finds the ordinal array index of the element with key $key.
  121. * @param mixed $key The key of the value to find the index of
  122. * @returns integer The index of the given key
  123. */
  124. function getitemindex($key) {
  125. $this->reset();
  126. $ix=0; $foundix=0;
  127. while ( list($searchkey, $val) = each($this->data) ) {
  128. $ix += 1;
  129. if ($key == $searchkey) {
  130. $foundix = $ix;
  131. break;
  132. }
  133. }
  134. $this->reset();
  135. return $foundix;
  136. }
  137. // ....................................................................
  138. /**
  139. * Return item value
  140. * Returns the value of the data item with the given key
  141. * @param mixed $key The key of the value to find the index of
  142. * @returns mixed The value of the item
  143. */
  144. function getitem($key) {
  145. if (isset($this->data[$key])) return $this->data[$key];
  146. else return "";
  147. }
  148. } // dat_keyvalues class
  149. // ----------------------------------------------------------------------
  150.  
  151. ?>

Documentation generated by phpDocumentor 1.3.0RC3