PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/classes/field.inc

http://leaguerunner.googlecode.com/
PHP | 213 lines | 204 code | 6 blank | 3 comment | 8 complexity | 1be0f8e04878d0bcbd86debdddb5454c MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. class Field extends LeaguerunnerObject
  3. {
  4. function __construct ( $load_mode = LOAD_RELATED_DATA )
  5. {
  6. // If we have a parent, override the overridables.
  7. if( $this->parent_fid ) {
  8. $parent = field_load( array('fid' => $this->parent_fid) );
  9. $this->name = $parent->name;
  10. $this->code = $parent->code;
  11. $this->region = $parent->region;
  12. $this->location_street = $parent->location_street;
  13. $this->location_city = $parent->location_city;
  14. $this->location_province = $parent->location_province;
  15. $this->driving_directions = $parent->driving_directions;
  16. $this->transit_directions = $parent->transit_directions;
  17. $this->biking_directions = $parent->biking_directions;
  18. $this->parking_details = $parent->parking_details;
  19. $this->washrooms = $parent->washrooms;
  20. $this->public_instructions = $parent->public_instructions;
  21. $this->site_instructions = $parent->site_instructions;
  22. $this->sponsor = $parent->sponsor;
  23. $this->location_url = $parent->location_url;
  24. $this->layout_url = $parent->layout_url;
  25. $this->fullname = join(" ", array($this->name, $this->num));
  26. // Fields may have their own parking details, or inherit from the parent
  27. if (! $this->parking ) {
  28. $this->parking = $parent->parking;
  29. }
  30. }
  31. if( $load_mode == LOAD_OBJECT_ONLY ) {
  32. return;
  33. }
  34. $season = strtolower(variable_get("current_season","Summer"));
  35. $permit_dir = join("/", array(variable_get("league_file_base",'/opt/websites/www.ocua.ca/static-content/leagues'), $season, 'current','permits'));
  36. # Auto-detect the permit URLs
  37. $this->permit_url = '';
  38. if (is_dir($permit_dir)) {
  39. if ($dh = opendir($permit_dir)) {
  40. while (($file = readdir($dh)) !== false) {
  41. if( fnmatch( $this->code . "*", $file) ) {
  42. $this->permit_url .= l($file, variable_get("league_url_base",'http://www.ocua.ca/leagues') . "/$season/current/permits/$file\"") . '<br />';
  43. }
  44. }
  45. }
  46. }
  47. if( ! $this->rating ) {
  48. # If no rating, mark as unknown
  49. $this->rating = '?';
  50. }
  51. return true;
  52. }
  53. function save ()
  54. {
  55. global $dbh;
  56. if(! count($this->_modified_fields)) {
  57. // No modifications, no need to save
  58. return true;
  59. }
  60. if( ! $this->_in_database ) {
  61. if( ! $this->create() ) {
  62. error_exit("Couldn't create field");
  63. }
  64. }
  65. $fields = array();
  66. $fields_data = array();
  67. foreach ( $this->_modified_fields as $key => $value) {
  68. if( !isset($this->{$key}) || ('' == $this->{$key}) ) {
  69. $fields[] = "$key = ?";
  70. $fields_data[] = null;
  71. } else {
  72. $fields[] = "$key = ?";
  73. $fields_data[] = $this->{$key};
  74. }
  75. }
  76. if(count($fields_data) != count($fields)) {
  77. error_exit("Internal error: Incorrect number of fields set");
  78. }
  79. $sql = "UPDATE field SET ";
  80. $sql .= join(", ", $fields);
  81. $sql .= " WHERE fid = ?";
  82. $fields_data[] = $this->fid;
  83. $sth = $dbh->prepare( $sql );
  84. $sth->execute( $fields_data );
  85. if(1 != $sth->rowCount()) {
  86. # Affecting zero rows is possible but usually unwanted
  87. error_exit("Internal error: Strange number of rows affected");
  88. }
  89. unset($this->_modified_fields);
  90. return true;
  91. }
  92. function create ()
  93. {
  94. global $dbh;
  95. if( $this->_in_database ) {
  96. return false;
  97. }
  98. if( ! $this->num ) {
  99. return false;
  100. }
  101. if( ! $this->parent_fid ) {
  102. if( ! $this->code ) {
  103. return false;
  104. }
  105. if( ! $this->name ) {
  106. return false;
  107. }
  108. $sth = $dbh->prepare('INSERT into field (num, name, code) VALUES(?,?,?)');
  109. $sth->execute(array($this->num, $this->name, $this->code));
  110. } else {
  111. $sth = $dbh->prepare('INSERT into field (num, parent_fid) VALUES(?,?)');
  112. $sth->execute(array($this->num, $this->parent_fid));
  113. }
  114. if( 1 != $sth->rowCount() ) {
  115. return false;
  116. }
  117. $sth = $dbh->prepare('SELECT LAST_INSERT_ID() FROM field');
  118. $sth->execute();
  119. $this->fid = $sth->fetchColumn();
  120. return true;
  121. }
  122. function find_others_at_site ()
  123. {
  124. global $dbh;
  125. $sth = $dbh->prepare('SELECT * FROM field WHERE parent_fid = :fid OR fid = :fid ORDER BY num');
  126. if( $this->parent_fid ) {
  127. $sth->execute(array( 'fid' => $this->parent_fid ) );
  128. } else {
  129. $sth->execute(array( 'fid' => $this->fid ) );
  130. }
  131. return $sth;
  132. }
  133. }
  134. function field_query ( $array = array() )
  135. {
  136. global $dbh;
  137. $query = array();
  138. $params = array();
  139. $order = '';
  140. foreach ($array as $key => $value) {
  141. switch( $key ) {
  142. case '_extra':
  143. /* Just slap on any extra query fields desired */
  144. $query[] = $value;
  145. break;
  146. case '_order':
  147. $order = ' ORDER BY ' . $value;
  148. break;
  149. default:
  150. $query[] = "f.$key = ?";
  151. $params[] = $value;
  152. }
  153. }
  154. $sth = $dbh->prepare("SELECT
  155. f.*,
  156. 1 AS _in_database,
  157. CONCAT_WS(' ',f.name,f.num) as fullname
  158. FROM field f
  159. WHERE " . implode(' AND ',$query) . $order);
  160. $sth->execute( $params );
  161. return $sth;
  162. }
  163. function field_rating_values()
  164. {
  165. return array(
  166. 'A' => 'A - Field is top-quality',
  167. 'B' => 'B - Field is in good condition',
  168. 'C' => 'C - Field is acceptable for use',
  169. 'D' => 'D - Field is in poor condition',
  170. '?' => '? - Field is in unknown condition',
  171. );
  172. }
  173. /**
  174. * Wrapper for convenience and backwards-compatibility.
  175. */
  176. function field_load( $array = array() )
  177. {
  178. $result = field_query( $array );
  179. return $result->fetchObject('Field');
  180. }
  181. ?>