Goteo /library/page.php

Language PHP Lines 286
MD5 Hash 99438178cf1954d74041e2445e6bf9ff
Repository https://github.com/alugo/Goteo.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/*
 *  Copyright (C) 2012 Platoniq y Fundación Fuentes Abiertas (see README for details)
 *	This file is part of Goteo.
 *
 *  Goteo is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Goteo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with Goteo.  If not, see <http://www.gnu.org/licenses/agpl.txt>.
 *
 */

namespace Goteo\Library {

	use Goteo\Core\Model,
        Goteo\Core\Exception;

	/*
	 * Clase para gestionar el contenido de las páginas institucionales
	 */
    class Page {

        public
            $id,
            $lang,
            $node,
            $name,
            $description,
            $url,
            $content,
            $pendiente; // para si esta pendiente de traduccion

        static public function get ($id, $node = \GOTEO_NODE, $lang = \LANG) {

            // buscamos la página para este nodo en este idioma
			$sql = "SELECT  page.id as id,
                            IFNULL(page_node.name, IFNULL(original.name, page.name)) as name,
                            IFNULL(page_node.description, IFNULL(original.description, page.description)) as description,
                            page.url as url,
                            IFNULL(page_node.lang, '$lang') as lang,
                            IFNULL(page_node.node, '$node') as node,
                            IFNULL(page_node.content, original.content) as content
                     FROM page
                     LEFT JOIN page_node
                        ON  page_node.page = page.id
                        AND page_node.lang = :lang
                        AND page_node.node = :node
                     LEFT JOIN page_node as original
                        ON  original.page = page.id
                        AND original.node = :node
                        AND original.lang = 'es'
                     WHERE page.id = :id
                ";

			$query = Model::query($sql, array(
                                            ':id' => $id,
                                            ':lang' => $lang,
                                            ':node' => $node
                                        )
                                    );
			$page = $query->fetchObject(__CLASS__);
            return $page;
		}

		/*
		 *  Metodo para la lista de páginas
		 */
		public static function getAll($lang = \LANG, $node = \GOTEO_NODE) {
            $pages = array();

            try {

                $values = array(':lang' => $lang, ':node' => $node);

                if ($node != \GOTEO_NODE) {
                    $sqlFilter .= " WHERE page.id IN ('about', 'contact', 'press', 'service')";
                }

                $sql = "SELECT
                            page.id as id,
                            IFNULL(page_node.name, IFNULL(original.name, page.name)) as name,
                            IFNULL(page_node.description, IFNULL(original.description, page.description)) as description,
                            IF(page_node.content IS NULL, 1, 0) as pendiente,
                            page.url as url
                        FROM page
                        LEFT JOIN page_node
                            ON  page_node.page = page.id
                            AND page_node.lang = :lang
                            AND page_node.node = :node
                         LEFT JOIN page_node as original
                            ON  original.page = page.id
                            AND original.node = :node
                            AND original.lang = 'es'
                        $sqlFilter
                        ORDER BY pendiente DESC, name ASC
                        ";

                $query = Model::query($sql, $values);
                foreach ($query->fetchAll(\PDO::FETCH_CLASS, __CLASS__) as $page) {
                    $pages[] = $page;
                }
                return $pages;
            } catch (\PDOException $e) {
                throw new Exception('FATAL ERROR SQL: ' . $e->getMessage() . "<br />$sql<br /><pre>" . print_r($values, 1) . "</pre>");
            }
		}

		/*
		 *  Lista simple de páginas
		 */
		public static function getList($node = \GOTEO_NODE) {
            $pages = array();

            try {

                if ($node != \GOTEO_NODE) {
                    $sqlFilter = " WHERE page.id IN ('about', 'contact', 'press', 'service')";
                } else {
                    $sqlFilter = '';
                }

                $values = array(':lang' => 'es', ':node' => $node);

                $sql = "SELECT
                            page.id as id,
                            IFNULL(page_node.name, page.name) as name,
                            IFNULL(page_node.description, page.description) as description,
                            page.url as url
                        FROM page
                        LEFT JOIN page_node
                           ON  page_node.page = page.id
                           AND page_node.lang = :lang
                           AND page_node.node = :node
                        $sqlFilter
                        ORDER BY name ASC
                        ";

                $query = Model::query($sql, $values);
                foreach ($query->fetchAll(\PDO::FETCH_CLASS, __CLASS__) as $page) {
                    $pages[] = $page;
                }
                return $pages;
            } catch (\PDOException $e) {
                throw new Exception('FATAL ERROR SQL: ' . $e->getMessage() . "<br />$sql<br /><pre>" . print_r($values, 1) . "</pre>");
            }
		}

        public function validate(&$errors = array()) {

            $allok = true;

            if (empty($this->id)) {
                $errors[] = 'Registro sin id';
                $allok = false;
            }

            if (empty($this->lang)) {
                $errors[] = 'Registro sin lang';
                $allok = false;
            }

            if (empty($this->node)) {
                $errors[] = 'Registro sin node';
                $allok = false;
            }

            if (empty($this->name)) {
                $errors[] = 'Registro sin nombre';
                $allok = false;
            }

            return $allok;
        }

		/*
		 *  Esto se usara para la gestión de contenido
		 */
		public function save(&$errors = array()) {
            if(!$this->validate($errors)) { return false; }

  			try {
                $values = array(
                    ':page' => $this->id,
                    ':lang' => $this->lang,
                    ':node' => $this->node,
                    ':name' => $this->name,
                    ':description' => $this->description,
                    ':contenido' => $this->content
                );

				$sql = "REPLACE INTO page_node
                            (page, node, lang, name, description, content)
                        VALUES
                            (:page, :node, :lang, :name, :description, :contenido)
                        ";
				if (Model::query($sql, $values)) {
                    return true;
                } else {
                    $errors[] = "Ha fallado $sql con <pre>" . print_r($values, 1) . "</pre>";
                    return false;
                }
                
			} catch(\PDOException $e) {
                $errors[] = 'Error sql al grabar el contenido de la pagina. ' . $e->getMessage();
                return false;
			}

		}

		/*
		 *  Esto se usara para la gestión de contenido
		 */
		public function add(&$errors = array()) {

  			try {
                $values = array(
                    ':id' => $this->id,
                    ':name' => $this->name,
                    ':url' => '/about/'.$this->id
                );

				$sql = "INSERT INTO page
                            (id, name, url)
                        VALUES
                            (:id, :name, :url)
                        ";
				if (Model::query($sql, $values)) {
                    return true;
                } else {
                    $errors[] = "Ha fallado $sql con <pre>" . print_r($values, 1) . "</pre>";
                    return false;
                }

			} catch(\PDOException $e) {
                $errors[] = 'Error sql al grabar el contenido de la pagina. ' . $e->getMessage();
                return false;
			}

		}

        /**
         * PAra actualizar solamente el contenido
         * @param <type> $errors
         * @return <type>
         */
		public function update($id, $lang, $node, $name, $description, $content, &$errors = array()) {
  			try {
                $values = array(
                    ':page' => $id,
                    ':lang' => $lang,
                    ':node' => $node,
                    ':name' => $name,
                    ':description' => $description,
                    ':content' => $content
                );

				$sql = "REPLACE INTO page_node
                            (page, node, lang, name, description, content)
                        VALUES
                            (:page, :node, :lang, :name, :description, :content)
                        ";
				if (Model::query($sql, $values)) {
                    return true;
                } else {
                    $errors[] = "Ha fallado $sql con <pre>" . print_r($values, 1) . "</pre>";
                    return false;
                }

			} catch(\PDOException $e) {
                $errors[] = 'Error sql al grabar el contenido de la pagina. ' . $e->getMessage();
                return false;
			}

		}


	}
}
Back to Top