PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/library/page.php

https://github.com/alugo/Goteo
PHP | 286 lines | 205 code | 42 blank | 39 comment | 16 complexity | 99438178cf1954d74041e2445e6bf9ff MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. * Copyright (C) 2012 Platoniq y Fundación Fuentes Abiertas (see README for details)
  4. * This file is part of Goteo.
  5. *
  6. * Goteo is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Goteo is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Goteo. If not, see <http://www.gnu.org/licenses/agpl.txt>.
  18. *
  19. */
  20. namespace Goteo\Library {
  21. use Goteo\Core\Model,
  22. Goteo\Core\Exception;
  23. /*
  24. * Clase para gestionar el contenido de las páginas institucionales
  25. */
  26. class Page {
  27. public
  28. $id,
  29. $lang,
  30. $node,
  31. $name,
  32. $description,
  33. $url,
  34. $content,
  35. $pendiente; // para si esta pendiente de traduccion
  36. static public function get ($id, $node = \GOTEO_NODE, $lang = \LANG) {
  37. // buscamos la página para este nodo en este idioma
  38. $sql = "SELECT page.id as id,
  39. IFNULL(page_node.name, IFNULL(original.name, page.name)) as name,
  40. IFNULL(page_node.description, IFNULL(original.description, page.description)) as description,
  41. page.url as url,
  42. IFNULL(page_node.lang, '$lang') as lang,
  43. IFNULL(page_node.node, '$node') as node,
  44. IFNULL(page_node.content, original.content) as content
  45. FROM page
  46. LEFT JOIN page_node
  47. ON page_node.page = page.id
  48. AND page_node.lang = :lang
  49. AND page_node.node = :node
  50. LEFT JOIN page_node as original
  51. ON original.page = page.id
  52. AND original.node = :node
  53. AND original.lang = 'es'
  54. WHERE page.id = :id
  55. ";
  56. $query = Model::query($sql, array(
  57. ':id' => $id,
  58. ':lang' => $lang,
  59. ':node' => $node
  60. )
  61. );
  62. $page = $query->fetchObject(__CLASS__);
  63. return $page;
  64. }
  65. /*
  66. * Metodo para la lista de páginas
  67. */
  68. public static function getAll($lang = \LANG, $node = \GOTEO_NODE) {
  69. $pages = array();
  70. try {
  71. $values = array(':lang' => $lang, ':node' => $node);
  72. if ($node != \GOTEO_NODE) {
  73. $sqlFilter .= " WHERE page.id IN ('about', 'contact', 'press', 'service')";
  74. }
  75. $sql = "SELECT
  76. page.id as id,
  77. IFNULL(page_node.name, IFNULL(original.name, page.name)) as name,
  78. IFNULL(page_node.description, IFNULL(original.description, page.description)) as description,
  79. IF(page_node.content IS NULL, 1, 0) as pendiente,
  80. page.url as url
  81. FROM page
  82. LEFT JOIN page_node
  83. ON page_node.page = page.id
  84. AND page_node.lang = :lang
  85. AND page_node.node = :node
  86. LEFT JOIN page_node as original
  87. ON original.page = page.id
  88. AND original.node = :node
  89. AND original.lang = 'es'
  90. $sqlFilter
  91. ORDER BY pendiente DESC, name ASC
  92. ";
  93. $query = Model::query($sql, $values);
  94. foreach ($query->fetchAll(\PDO::FETCH_CLASS, __CLASS__) as $page) {
  95. $pages[] = $page;
  96. }
  97. return $pages;
  98. } catch (\PDOException $e) {
  99. throw new Exception('FATAL ERROR SQL: ' . $e->getMessage() . "<br />$sql<br /><pre>" . print_r($values, 1) . "</pre>");
  100. }
  101. }
  102. /*
  103. * Lista simple de páginas
  104. */
  105. public static function getList($node = \GOTEO_NODE) {
  106. $pages = array();
  107. try {
  108. if ($node != \GOTEO_NODE) {
  109. $sqlFilter = " WHERE page.id IN ('about', 'contact', 'press', 'service')";
  110. } else {
  111. $sqlFilter = '';
  112. }
  113. $values = array(':lang' => 'es', ':node' => $node);
  114. $sql = "SELECT
  115. page.id as id,
  116. IFNULL(page_node.name, page.name) as name,
  117. IFNULL(page_node.description, page.description) as description,
  118. page.url as url
  119. FROM page
  120. LEFT JOIN page_node
  121. ON page_node.page = page.id
  122. AND page_node.lang = :lang
  123. AND page_node.node = :node
  124. $sqlFilter
  125. ORDER BY name ASC
  126. ";
  127. $query = Model::query($sql, $values);
  128. foreach ($query->fetchAll(\PDO::FETCH_CLASS, __CLASS__) as $page) {
  129. $pages[] = $page;
  130. }
  131. return $pages;
  132. } catch (\PDOException $e) {
  133. throw new Exception('FATAL ERROR SQL: ' . $e->getMessage() . "<br />$sql<br /><pre>" . print_r($values, 1) . "</pre>");
  134. }
  135. }
  136. public function validate(&$errors = array()) {
  137. $allok = true;
  138. if (empty($this->id)) {
  139. $errors[] = 'Registro sin id';
  140. $allok = false;
  141. }
  142. if (empty($this->lang)) {
  143. $errors[] = 'Registro sin lang';
  144. $allok = false;
  145. }
  146. if (empty($this->node)) {
  147. $errors[] = 'Registro sin node';
  148. $allok = false;
  149. }
  150. if (empty($this->name)) {
  151. $errors[] = 'Registro sin nombre';
  152. $allok = false;
  153. }
  154. return $allok;
  155. }
  156. /*
  157. * Esto se usara para la gestión de contenido
  158. */
  159. public function save(&$errors = array()) {
  160. if(!$this->validate($errors)) { return false; }
  161. try {
  162. $values = array(
  163. ':page' => $this->id,
  164. ':lang' => $this->lang,
  165. ':node' => $this->node,
  166. ':name' => $this->name,
  167. ':description' => $this->description,
  168. ':contenido' => $this->content
  169. );
  170. $sql = "REPLACE INTO page_node
  171. (page, node, lang, name, description, content)
  172. VALUES
  173. (:page, :node, :lang, :name, :description, :contenido)
  174. ";
  175. if (Model::query($sql, $values)) {
  176. return true;
  177. } else {
  178. $errors[] = "Ha fallado $sql con <pre>" . print_r($values, 1) . "</pre>";
  179. return false;
  180. }
  181. } catch(\PDOException $e) {
  182. $errors[] = 'Error sql al grabar el contenido de la pagina. ' . $e->getMessage();
  183. return false;
  184. }
  185. }
  186. /*
  187. * Esto se usara para la gestión de contenido
  188. */
  189. public function add(&$errors = array()) {
  190. try {
  191. $values = array(
  192. ':id' => $this->id,
  193. ':name' => $this->name,
  194. ':url' => '/about/'.$this->id
  195. );
  196. $sql = "INSERT INTO page
  197. (id, name, url)
  198. VALUES
  199. (:id, :name, :url)
  200. ";
  201. if (Model::query($sql, $values)) {
  202. return true;
  203. } else {
  204. $errors[] = "Ha fallado $sql con <pre>" . print_r($values, 1) . "</pre>";
  205. return false;
  206. }
  207. } catch(\PDOException $e) {
  208. $errors[] = 'Error sql al grabar el contenido de la pagina. ' . $e->getMessage();
  209. return false;
  210. }
  211. }
  212. /**
  213. * PAra actualizar solamente el contenido
  214. * @param <type> $errors
  215. * @return <type>
  216. */
  217. public function update($id, $lang, $node, $name, $description, $content, &$errors = array()) {
  218. try {
  219. $values = array(
  220. ':page' => $id,
  221. ':lang' => $lang,
  222. ':node' => $node,
  223. ':name' => $name,
  224. ':description' => $description,
  225. ':content' => $content
  226. );
  227. $sql = "REPLACE INTO page_node
  228. (page, node, lang, name, description, content)
  229. VALUES
  230. (:page, :node, :lang, :name, :description, :content)
  231. ";
  232. if (Model::query($sql, $values)) {
  233. return true;
  234. } else {
  235. $errors[] = "Ha fallado $sql con <pre>" . print_r($values, 1) . "</pre>";
  236. return false;
  237. }
  238. } catch(\PDOException $e) {
  239. $errors[] = 'Error sql al grabar el contenido de la pagina. ' . $e->getMessage();
  240. return false;
  241. }
  242. }
  243. }
  244. }