PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/template.php

https://github.com/alugo/Goteo
PHP | 212 lines | 148 code | 30 blank | 34 comment | 8 complexity | 43765b1ac47e704fa335b386f8005c85 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 las plantillas de los emails automáticos
  25. */
  26. class Template {
  27. public
  28. $id,
  29. $lang,
  30. $name,
  31. $purpose,
  32. $title,
  33. $text;
  34. static public function get ($id, $lang = \LANG) {
  35. // buscamos la página para este nodo en este idioma
  36. $sql = "SELECT template.id as id,
  37. template.name as name,
  38. template.group as `group`,
  39. template.purpose as purpose,
  40. IFNULL(template_lang.title, template.title) as title,
  41. IFNULL(template_lang.text, template.text) as text
  42. FROM template
  43. LEFT JOIN template_lang
  44. ON template_lang.id = template.id
  45. AND template_lang.lang = :lang
  46. WHERE template.id = :id
  47. ";
  48. $query = Model::query($sql, array(
  49. ':id' => $id,
  50. ':lang' => $lang
  51. )
  52. );
  53. $template = $query->fetchObject(__CLASS__);
  54. return $template;
  55. }
  56. /*
  57. * Metodo para la lista de páginas
  58. */
  59. public static function getAll($filters = array()) {
  60. $templates = array();
  61. try {
  62. $values = array(':lang' => \LANG);
  63. $sqlFilter = '';
  64. $and = "WHERE";
  65. if (!empty($filters['group'])) {
  66. $sqlFilter .= " $and template.`group` = :group";
  67. $and = "AND";
  68. $values[':group'] = "{$filters['group']}";
  69. }
  70. if (!empty($filters['name'])) {
  71. $sqlFilter .= " $and (template.`name` LIKE :name OR template.`purpose` LIKE :name OR template.`title` LIKE :name)";
  72. $and = "AND";
  73. $values[':name'] = "%{$filters['name']}%";
  74. }
  75. $sql = "SELECT
  76. template.id as id,
  77. template.name as name,
  78. template.purpose as purpose,
  79. IFNULL(template_lang.title, template.title) as title,
  80. IFNULL(template_lang.text, template.text) as text
  81. FROM template
  82. LEFT JOIN template_lang
  83. ON template_lang.id = template.id
  84. AND template_lang.lang = :lang
  85. $sqlFilter
  86. ORDER BY name ASC
  87. ";
  88. $query = Model::query($sql, $values);
  89. foreach ($query->fetchAll(\PDO::FETCH_CLASS, __CLASS__) as $template) {
  90. $templates[] = $template;
  91. }
  92. return $templates;
  93. } catch (\PDOException $e) {
  94. throw new Exception(Text::_('No se ha grabado correctamente. ') . $e->getMessage() . "<br />$sql<br /><pre>" . print_r($values, 1) . "</pre>");
  95. }
  96. }
  97. /*
  98. * Lista de plantillas para filtro
  99. */
  100. public static function getAllMini() {
  101. $templates = array();
  102. try {
  103. $sql = "SELECT
  104. template.id as id,
  105. template.name as name
  106. FROM template
  107. ORDER BY name ASC
  108. ";
  109. $query = Model::query($sql);
  110. foreach ($query->fetchAll(\PDO::FETCH_OBJ) as $template) {
  111. $templates[$template->id] = $template->name;
  112. }
  113. return $templates;
  114. } catch (\PDOException $e) {
  115. throw new Exception(Text::_('No se ha grabado correctamente. ') . $e->getMessage() );
  116. }
  117. }
  118. public function validate(&$errors = array()) {
  119. $allok = true;
  120. if (empty($this->id)) {
  121. $errors[] = Text::_('Registro sin id');
  122. $allok = false;
  123. }
  124. if (empty($this->title)) {
  125. $errors[] = Text::_('Registro sin titulo');
  126. $allok = false;
  127. }
  128. if (empty($this->text)) {
  129. $errors[] = Text::_('Registro sin contenido');
  130. $allok = false;
  131. }
  132. return $allok;
  133. }
  134. /*
  135. * Esto se usara para la gestión de contenido
  136. */
  137. public function save(&$errors = array()) {
  138. if(!$this->validate($errors)) { return false; }
  139. try {
  140. $values = array(
  141. ':template' => $this->id,
  142. ':name' => $this->name,
  143. ':group' => $this->group,
  144. ':purpose' => $this->purpose,
  145. ':title' => $this->title,
  146. ':text' => $this->text
  147. );
  148. $sql = "REPLACE INTO template
  149. (id, name, purpose, title, text, `group`)
  150. VALUES
  151. (:template, :name, :purpose, :title, :text, :group)
  152. ";
  153. if (Model::query($sql, $values)) {
  154. return true;
  155. } else {
  156. $errors[] = "Ha fallado $sql con <pre>" . print_r($values, 1) . "</pre>";
  157. return false;
  158. }
  159. } catch(\PDOException $e) {
  160. $errors[] = 'Error sql al grabar el contenido de la palntilla. ' . $e->getMessage();
  161. return false;
  162. }
  163. }
  164. /*
  165. * Grupos de plantillas
  166. */
  167. static public function groups()
  168. {
  169. $groups = array(
  170. 'general' => Text::_('Propósito general'),
  171. 'access' => Text::_('Registro y acceso usuario'),
  172. 'project' => Text::_('Actividad proyecto'),
  173. 'tips' => Text::_('Auto-tips difusión'),
  174. 'invest' => Text::_('Proceso aporte'),
  175. 'contact' => Text::_('Comunicación'),
  176. 'advice' => Text::_('Avisos al autor')
  177. );
  178. \asort($groups);
  179. return $groups;
  180. }
  181. }
  182. }