PageRenderTime 27ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/oc-includes/osclass/model/Page.php

https://github.com/hashemgamal/OSClass
PHP | 402 lines | 212 code | 59 blank | 131 comment | 30 complexity | a7f181f747ddda3bb9ee10e8ef3c50f2 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * OSClass – software for creating and publishing online classified advertising platforms
  4. *
  5. * Copyright (C) 2010 OSCLASS
  6. *
  7. * This program is free software: you can redistribute it and/or modify it under the terms
  8. * of the GNU Affero General Public License as published by the Free Software Foundation,
  9. * either version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public
  16. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class Page extends DAO
  19. {
  20. /**
  21. * The columns defined in page table.
  22. *
  23. * @access private
  24. * @var array
  25. */
  26. private $columns;
  27. /**
  28. * The columns defined in page description table.
  29. *
  30. * @access private
  31. * @var array
  32. */
  33. private $columns_desc;
  34. private static $instance ;
  35. public function __construct() {
  36. parent::__construct();
  37. $this->columns = array('pk_i_id', 's_internal_name', 'b_indelible', 'dt_pub_date, dt_mod_date');
  38. $this->columns_desc = array('fk_i_pages_id', 'fk_c_locale_code', 's_title', 's_text');
  39. }
  40. public static function newInstance() {
  41. if(!self::$instance instanceof self) {
  42. self::$instance = new self ;
  43. }
  44. return self::$instance ;
  45. }
  46. /**
  47. * Return's the name of the table.
  48. *
  49. * @return string table name.
  50. */
  51. public function getTableName()
  52. {
  53. return DB_TABLE_PREFIX . 't_pages';
  54. }
  55. /**
  56. * Return's the name of the description table.
  57. *
  58. * @return string description table name.
  59. */
  60. public function getDescriptionTableName()
  61. {
  62. return DB_TABLE_PREFIX . 't_pages_description';
  63. }
  64. /**
  65. * Find a page by page id.
  66. *
  67. * @param int $id Page id.
  68. * @param string $locale By default is null but you can specify locale code.
  69. * @return array Page information. If there's no information, return an empty array.
  70. */
  71. public function findByPrimaryKey($id, $locale = null)
  72. {
  73. $sql = 'SELECT ' . implode(', ', $this->columns) . ' FROM ' . $this->getTableName();
  74. $sql .= ' WHERE ' . $this->getPrimaryKey() . ' = ' . $id;
  75. $row = $this->conn->osc_dbFetchResult($sql);
  76. if(is_null($row)) {
  77. return array();
  78. }
  79. $sql_desc = 'SELECT ' . implode(', ', $this->columns_desc) . ' FROM ';
  80. $sql_desc .= $this->getDescriptionTableName() . ' WHERE fk_i_pages_id = ' . $id;
  81. if(!is_null($locale)) {
  82. $sql_desc .= ' AND fk_c_locale_code = \'' . $locale . '\' ';
  83. }
  84. $sub_rows = $this->conn->osc_dbFetchResults($sql_desc);
  85. $row['locale'] = array();
  86. foreach($sub_rows as $sub_row) {
  87. $row['locale'][$sub_row['fk_c_locale_code']] = $sub_row;
  88. }
  89. return $row;
  90. }
  91. /**
  92. * Find a page by internal name.
  93. *
  94. * @param string $intName Internal name of the page to find.
  95. * @param string $locale Locale string.
  96. * @return array It returns page fields. If it has no results, it returns an empty array.
  97. */
  98. public function findByInternalName($intName, $locale = null)
  99. {
  100. $sql = 'SELECT ' . implode(', ', $this->columns) . ' FROM ' . $this->getTableName();
  101. $sql .= ' WHERE s_internal_name = \'' . $intName . '\'';
  102. $row = $this->conn->osc_dbFetchResult($sql);
  103. if(count($row) == 0) {
  104. return array();
  105. }
  106. $result = $this->extendDescription($row, $locale);
  107. return $result;
  108. }
  109. /**
  110. * Get all the pages with the parameters you choose.
  111. *
  112. * @param bool $indelible It's true if the page is indelible and false if not.
  113. * @param string $locale It's
  114. * @param int $start
  115. * @param int $limit
  116. * @return array Return all the pages that have been found with the criteria selected. If there's no pages, the
  117. * result is an empty array.
  118. */
  119. public function listAll($indelible = null, $locale = null, $start = null, $limit = null)
  120. {
  121. $sql = 'SELECT ' . implode(', ', $this->columns) . ' FROM ' . $this->getTableName();
  122. $aConditions = array();
  123. if(!is_null($indelible)) {
  124. $aConditions['b_indelible'] = ($indelible) ? '1' : '0';
  125. }
  126. if(count($aConditions) > 0) {
  127. $sql .= ' WHERE ';
  128. $aResultConditions = array();
  129. foreach($aConditions as $k => $v) {
  130. $aResultConditions[] = $k . ' = \'' . $v . '\'';
  131. }
  132. $sql .= implode(' AND ', $aResultConditions);
  133. }
  134. if(!is_null($limit)) {
  135. $sql .= ' LIMIT ';
  136. if(!is_null($start)) {
  137. $sql .= $start . ',';
  138. }
  139. $sql .= $limit;
  140. }
  141. $aPages = $this->conn->osc_dbFetchResults($sql);
  142. if(count($aPages) == 0) {
  143. return array();
  144. }
  145. $result = array();
  146. foreach($aPages as $aPage) {
  147. $data = $this->extendDescription($aPage, $locale);
  148. if(count($data) > 0) {
  149. $result[] = $data;
  150. }
  151. unset($data);
  152. }
  153. return $result;
  154. }
  155. /**
  156. * An array with data of some page, returns the title and description in every language available
  157. *
  158. * @param array $aPage
  159. * @return array Page information, title and description in every language available
  160. */
  161. public function extendDescription($aPage, $locale = null)
  162. {
  163. $sql = sprintf('SELECT * FROM %s ', $this->getDescriptionTableName());
  164. $sql .= sprintf('WHERE fk_i_pages_id = %d', $aPage['pk_i_id']);
  165. if(!is_null($locale)) {
  166. $sql .= ' AND fk_c_locale_code = \'' . $locale . '\'';
  167. }
  168. $descriptions = $this->conn->osc_dbFetchResults($sql);
  169. if(count($descriptions) == 0) {
  170. return array();
  171. }
  172. $aPage['locale'] = array();
  173. foreach($descriptions as $desc) {
  174. if( !empty($desc['s_title']) || !empty($desc['s_text']) ) {
  175. $aPage['locale'][$desc['fk_c_locale_code']] = $desc;
  176. }
  177. }
  178. return $aPage;
  179. }
  180. /**
  181. * Delete a page by id number.
  182. *
  183. * @param int $id Page id which is going to be deleted
  184. * @return bool True on successful removal, false on failure
  185. */
  186. public function deleteByPrimaryKey($id)
  187. {
  188. $this->conn->osc_dbExec('DELETE FROM %s WHERE fk_i_pages_id = %d', $this->getDescriptionTableName(), $id);
  189. $result = $this->delete(array('pk_i_id' => $id));
  190. if($result > 0) {
  191. return true;
  192. }
  193. return false;
  194. }
  195. /**
  196. * Delete a page by internal name.
  197. *
  198. * @param string $intName Page internal name which is going to be deleted
  199. * @return bool True on successful removal, false on failure
  200. */
  201. public function deleteByInternalName($intName)
  202. {
  203. $row = $this->conn->findByInternalName($intName);
  204. if(!isset($row)) {
  205. return false;
  206. }
  207. return $this->deleteByPrimaryKey($id);
  208. }
  209. /**
  210. * Insert a new page. You have to pass all the parameters
  211. *
  212. * @param array $aFields Fields to be inserted in pages table
  213. * @param array $aFieldsDescription An array with the titles and descriptions in every language.
  214. * @return boolean True if the insert has been done well and false if not.
  215. */
  216. public function insert($aFields, $aFieldsDescription = null)
  217. {
  218. $sql = 'INSERT INTO ' . $this->getTableName() . ' (s_internal_name, b_indelible, dt_pub_date, dt_mod_date)';
  219. $sql .= ' VALUES (\'' . $aFields['s_internal_name'] . '\', ' . '\'' . $aFields['b_indelible'] . '\'';
  220. $sql .= ', NOW(), NOW())';
  221. $this->conn->osc_dBExec($sql);
  222. $id = $this->conn->get_last_id();
  223. if($this->conn->get_affected_rows() == 0) {
  224. return false;
  225. }
  226. foreach($aFieldsDescription as $k => $v) {
  227. $affected_rows = $this->insertDescription($id, $k, $v['s_title'], $v['s_text']);
  228. if(!$affected_rows) {
  229. return false;
  230. }
  231. }
  232. return true;
  233. }
  234. /**
  235. * Insert the content (title and description) of a page.
  236. *
  237. * @param int $id Id of the page, it would be the foreign key
  238. * @param string $locale OSCLocale code of the language
  239. * @param string $title Text to be inserted in s_title
  240. * @param string $text Text to be inserted in s_text
  241. * @return bool True if the insert has been done well and false if not.
  242. */
  243. private function insertDescription($id, $locale, $title, $text)
  244. {
  245. $title = addslashes($title);
  246. $text = addslashes($text);
  247. $sql = 'INSERT INTO ' . $this->getDescriptionTableName() . ' (fk_i_pages_id, fk_c_locale_code, s_title, ';
  248. $sql .= 's_text) VALUES (' . sprintf('%d, \'%s\', \'%s\', \'%s\')', $id, $locale, $title, $text);
  249. $this->conn->osc_dBExec($sql);
  250. if($this->conn->get_affected_rows() == 0) {
  251. return false;
  252. }
  253. return true;
  254. }
  255. /**
  256. * Update the content (title and description) of a page
  257. *
  258. * @param int $id Id of the page id is going to be modified
  259. * @param string $locale Locale code of the language
  260. * @param string $title Text to be updated in s_title
  261. * @param string $text Text to be updated in s_text
  262. * @return int Number of affected rows.
  263. */
  264. public function updateDescription($id, $locale, $title, $text)
  265. {
  266. $conditions = array('fk_c_locale_code' => $locale, 'fk_i_pages_id' => $id);
  267. $exist= $this->existDescription($conditions);
  268. if(!$exist) {
  269. $result = $this->insertDescription($id, $locale, $title, $text);
  270. return $result;
  271. }
  272. $sql = 'UPDATE ' . $this->getDescriptionTableName() . ' SET ';
  273. $sql .= ' s_title = \'' . addslashes($title) . '\', s_text = \'' . addslashes($text) . '\'';
  274. $sql .= ' WHERE fk_c_locale_code = \'' . $locale . '\' AND fk_i_pages_id = ' . $id;
  275. $this->conn->osc_dbExec($sql);
  276. $result = $this->conn->get_affected_rows();
  277. return $result;
  278. }
  279. /**
  280. * Check if depending the conditions, the row exists in de DB.
  281. *
  282. * @param array $conditions
  283. * @return bool Return true if exists and false if not.
  284. */
  285. public function existDescription($conditions) {
  286. $where = array();
  287. foreach($conditions as $key => $value) {
  288. if($key == DB_CUSTOM_COND)
  289. $where[] = $value;
  290. else
  291. $where[] = $key . ' = ' . $this->formatValue($value);
  292. }
  293. $where = implode(' AND ', $where);
  294. $sql = sprintf("SELECT COUNT(*) FROM %s WHERE " . $where, $this->getDescriptionTableName());
  295. $result = $this->conn->osc_dbFetchValue($sql);
  296. return (bool) $result;
  297. }
  298. /**
  299. * It change the internal name of a page. Here you don't check if in indelible or not the page.
  300. *
  301. * @param int $id The id of the page to be changed.
  302. * @param string $intName The new internal name.
  303. * @return int Number of affected rows.
  304. */
  305. public function updateInternalName($id, $intName)
  306. {
  307. $fields = array('s_internal_name' => $intName,
  308. 'dt_mod_date' => DB_FUNC_NOW);
  309. $where = array('pk_i_id' => $id);
  310. $result = $this->update($fields, $where);
  311. return $result;
  312. }
  313. /**
  314. * Check if a page id is indelible
  315. *
  316. * @param int $id Page id
  317. * @return true if it's indelible, false in case not
  318. */
  319. function isIndelible($id)
  320. {
  321. $page = $this->findByPrimaryKey($id);
  322. if($page['b_indelible'] == 1) {
  323. return true;
  324. }
  325. return false;
  326. }
  327. /**
  328. * Check if Internal Name exists with another id
  329. *
  330. * @param int $id page id
  331. * @param string $internalName page internal name
  332. * @return true if internal name exists, false if not
  333. */
  334. function internalNameExists($id, $internalName)
  335. {
  336. $result = $this->listWhere('s_internal_name = \'' . $internalName . '\' AND pk_i_id <> ' . $id );
  337. if(count($result) > 0) {
  338. return true;
  339. }
  340. return false;
  341. }
  342. }
  343. ?>