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

/webapp/_lib/model/class.OptionMySQLDAO.php

https://github.com/unruthless/ThinkUp
PHP | 198 lines | 138 code | 15 blank | 45 comment | 21 complexity | 965410a0d68989498f383816c6d2f2da MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/_lib/model/class.OptionMySQLDAO.php
  5. *
  6. * Copyright (c) 2009-2011 Mark Wilkie
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * Option Data Access Object
  25. *
  26. * The data access object for retrieving and saving generic ThinkUp options and their values.
  27. *
  28. * @license http://www.gnu.org/licenses/gpl.html
  29. * @copyright 2009-2011 Mark Wilkie, Gina Trapani
  30. * @author Mark Wilkie <mwilkie[at]gmail[dot]com>
  31. */
  32. class OptionMySQLDAO extends PDODAO implements OptionDAO {
  33. public function insertOption($namespace, $name, $value) {
  34. $option = $this->getOptionByName($namespace, $name);
  35. if($option) {
  36. throw new DuplicateOptionException("An option with the namespace $namespace and name $name exists");
  37. }
  38. $q = 'INSERT INTO #prefix#options
  39. (namespace, option_name, option_value, created, last_updated)
  40. VALUES
  41. (:namespace, :option_name, :option_value, now(), now())';
  42. $stmt = $this->execute($q,
  43. array(':namespace' => $namespace, ':option_name' => $name, ':option_value' => $value) );
  44. $this->clearSessionData($namespace);
  45. return $this->getInsertId($stmt);
  46. }
  47. public function updateOption($id, $value, $name = null) {
  48. $option = $this->getOption($id);
  49. if($option) {
  50. $q = 'UPDATE #prefix#options set option_value = :option_value, last_updated = now() ';
  51. if($name) {
  52. $q .= ', option_name = :option_name';
  53. }
  54. $q .= ' WHERE option_id = :option_id';
  55. $data = array(':option_id' => $id, ':option_value' => $value);
  56. if($name) {
  57. $data[':option_name'] = $name;
  58. }
  59. $stmt = $this->execute($q, $data);
  60. $this->clearSessionData($option->namespace);
  61. return $this->getUpdateCount($stmt);
  62. } else {
  63. return 0;
  64. }
  65. }
  66. public function updateOptionByName($namespace, $name, $value) {
  67. $q = 'UPDATE #prefix#options set option_value = :option_value, last_updated = now()
  68. WHERE namespace = :namespace AND option_name = :option_name';
  69. $binds = array(':namespace' => $namespace, ':option_name' => $name, 'option_value' => $value);
  70. $stmt = $this->execute($q, $binds);
  71. $this->clearSessionData($namespace);
  72. return $this->getUpdateCount($stmt);
  73. }
  74. public function getOptionByName($namespace, $name){
  75. $q = 'SELECT option_id, namespace, option_name, option_value FROM #prefix#options
  76. WHERE namespace = :namespace AND option_name = :option_name';
  77. $stmt = $this->execute($q, array(':namespace' => $namespace, ':option_name' => $name));
  78. $option = $this->getDataRowAsObject($stmt, 'Option');
  79. return $option;
  80. }
  81. public function getOption($option_id){
  82. $q = 'SELECT option_id, namespace, option_name, option_value FROM #prefix#options
  83. WHERE option_id = :option_id';
  84. $stmt = $this->execute($q, array(':option_id' => $option_id));
  85. $option = $this->getDataRowAsObject($stmt, 'Option');
  86. return $option;
  87. }
  88. public function deleteOption($option_id){
  89. $option = $this->getOption($option_id);
  90. if($option) {
  91. $q = 'DELETE FROM #prefix#options WHERE option_id = :option_id';
  92. $stmt = $this->execute($q, array(':option_id' => $option_id));
  93. $this->clearSessionData($option->namespace);
  94. return $this->getUpdateCount($stmt);
  95. $this->clearSessionData($namespace);
  96. } else {
  97. return 0;
  98. }
  99. }
  100. public function deleteOptionByName($namespace, $name){
  101. $q = 'DELETE FROM #prefix#options WHERE namespace = :namespace AND option_name = :name';
  102. $stmt = $this->execute($q, array(':namespace' => $namespace, ':name' => $name));
  103. $this->clearSessionData($namespace);
  104. return $this->getUpdateCount($stmt);
  105. }
  106. public function getOptions($namespace, $cached = false) {
  107. $data = null;
  108. if($cached) {
  109. $data = $this->getSessionData($namespace);
  110. }
  111. if(is_null($data)) {
  112. $q = 'SELECT option_id, namespace, option_name, option_value
  113. FROM #prefix#options
  114. WHERE namespace = :namespace';
  115. $stmt = $this->execute($q, array(':namespace' => $namespace));
  116. $res = $this->getDataRowsAsArrays($stmt);
  117. if(count($res ) == 0) {
  118. $data = null;
  119. } else {
  120. $data = array();
  121. foreach($res as $option_array) {
  122. $option = new Option($option_array);
  123. $data[$option->option_name] = $option;
  124. }
  125. }
  126. }
  127. if($cached) {
  128. $this->setSessionData($namespace, $data);
  129. }
  130. return $data;
  131. }
  132. public function getOptionValue($namespace, $name, $cached = false) {
  133. $options = $this->getOptions($namespace, $cached);
  134. if($options && isset($options[$name])) {
  135. return $options[$name]->option_value;
  136. } else {
  137. return null;
  138. }
  139. }
  140. /**
  141. * Gets option data from session using namespace as a key
  142. * @param $namespace
  143. * @retrun $array Hash of option data
  144. */
  145. public function getSessionData($namespace) {
  146. $key = 'options_data:' . $namespace;
  147. if(SessionCache::isKeySet($key) ) {
  148. return SessionCache::get($key);
  149. } else {
  150. return null;
  151. }
  152. }
  153. /**
  154. * Sets option data in the session using namespace as a key
  155. * @param $namespace
  156. * @param array Hash of option data
  157. * @retrun $array Hash of option data
  158. */
  159. public function setSessionData($namespace, $data) {
  160. $key = 'options_data:' . $namespace;
  161. SessionCache::put($key, $data);
  162. }
  163. /**
  164. * Clears session data by namespace
  165. * @param $namespace
  166. */
  167. public function clearSessionData($namespace) {
  168. $key = 'options_data:' . $namespace;
  169. if( SessionCache::isKeySet($key)) {
  170. SessionCache::unsetKey($key);
  171. }
  172. }
  173. public function isOptionsTable() {
  174. $q = "show tables like '#prefix#options'";
  175. $stmt = $this->execute($q);
  176. $data = $this->getDataRowAsArray($stmt);
  177. if($data) {
  178. return true;
  179. } else {
  180. return false;
  181. }
  182. }
  183. }