PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/oc-admin/pages.php

https://code.google.com/
PHP | 249 lines | 194 code | 30 blank | 25 comment | 47 complexity | 8cf9dc0ce8bbdbd7de7f86f542156c87 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');
  2. /*
  3. * OSCLass รข&#x20AC;&#x201C; software for creating and publishing online classified
  4. * advertising platforms
  5. *
  6. * Copyright (C) 2010 OSCLASS
  7. *
  8. * This program is free software: you can redistribute it and/or
  9. * modify it under the terms of the GNU Affero General Public License
  10. * as published by the Free Software Foundation, either version 3 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. class CAdminPages extends AdminSecBaseModel
  22. {
  23. //specific for this class
  24. private $pageManager ;
  25. function __construct()
  26. {
  27. parent::__construct() ;
  28. //specific things for this class
  29. $this->pageManager = Page::newInstance() ;
  30. }
  31. //Business Layer...
  32. function doModel()
  33. {
  34. parent::doModel() ;
  35. //specific things for this class
  36. switch($this->action) {
  37. case 'edit':
  38. if(Params::getParam("id")=='') {
  39. $this->redirectTo(osc_admin_base_url(true)."?page=pages");
  40. }
  41. $form = count(Session::newInstance()->_getForm());
  42. $keepForm = count(Session::newInstance()->_getKeepForm());
  43. if($form == 0 || $form == $keepForm) {
  44. Session::newInstance()->_dropKeepForm();
  45. }
  46. $this->_exportVariableToView("page", $this->pageManager->findByPrimaryKey(Params::getParam("id")));
  47. $this->doView("pages/frm.php");
  48. break;
  49. case 'edit_post':
  50. $id = Params::getParam("id");
  51. $s_internal_name = Params::getParam("s_internal_name");
  52. $s_internal_name = osc_sanitizeString($s_internal_name) ;
  53. $aFieldsDescription = array();
  54. $postParams = Params::getParamsAsArray('', false);
  55. $not_empty = false;
  56. foreach ($postParams as $k => $v) {
  57. if(preg_match('|(.+?)#(.+)|', $k, $m)) {
  58. if($m[2]=='s_title' && $v!='') { $not_empty = true; };
  59. $aFieldsDescription[$m[1]][$m[2]] = $v;
  60. }
  61. }
  62. Session::newInstance()->_setForm('aFieldsDescription',$aFieldsDescription);
  63. if( $s_internal_name == '' ) {
  64. osc_add_flash_error_message(_m('You have to set an internal name'), 'admin');
  65. $this->redirectTo(osc_admin_base_url(true)."?page=pages&action=edit&id=" . $id);
  66. }
  67. if( !WebThemes::newInstance()->isValidPage($s_internal_name) ) {
  68. osc_add_flash_error_message(_m('You have to set a different internal name'), 'admin');
  69. $this->redirectTo(osc_admin_base_url(true)."?page=pages&action=edit&id=" . $id);
  70. }
  71. Session::newInstance()->_setForm('s_internal_name',$s_internal_name);
  72. if($not_empty) {
  73. foreach($aFieldsDescription as $k => $_data) {
  74. $this->pageManager->updateDescription($id, $k, $_data['s_title'], $_data['s_text']);
  75. }
  76. if(!$this->pageManager->internalNameExists($id, $s_internal_name)) {
  77. if(!$this->pageManager->isIndelible($id)) {
  78. $this->pageManager->updateInternalName($id, $s_internal_name);
  79. }
  80. Session::newInstance()->_clearVariables();
  81. osc_add_flash_ok_message(_m('The page has been updated'), 'admin');
  82. $this->redirectTo(osc_admin_base_url(true)."?page=pages");
  83. }
  84. osc_add_flash_error_message(_m("You can't repeat internal name"), 'admin');
  85. } else {
  86. osc_add_flash_error_message(_m("The page couldn't be updated, at least one title should not be empty"), 'admin') ;
  87. }
  88. $this->redirectTo(osc_admin_base_url(true)."?page=pages&action=edit&id=" . $id);
  89. break;
  90. case 'add':
  91. $form = count(Session::newInstance()->_getForm());
  92. $keepForm = count(Session::newInstance()->_getKeepForm());
  93. if($form == 0 || $form == $keepForm) {
  94. Session::newInstance()->_dropKeepForm();
  95. }
  96. $this->_exportVariableToView("page", array());
  97. $this->doView("pages/frm.php");
  98. break;
  99. case 'add_post':
  100. $s_internal_name = Params::getParam("s_internal_name");
  101. $s_internal_name = osc_sanitizeString($s_internal_name) ;
  102. $aFieldsDescription = array();
  103. $postParams = Params::getParamsAsArray('', false);
  104. $not_empty = false;
  105. foreach($postParams as $k => $v) {
  106. if(preg_match('|(.+?)#(.+)|', $k, $m)) {
  107. if($m[2]=='s_title' && $v!='') {
  108. $not_empty = true;
  109. }
  110. $aFieldsDescription[$m[1]][$m[2]] = $v;
  111. }
  112. }
  113. Session::newInstance()->_setForm('aFieldsDescription',$aFieldsDescription);
  114. if( $s_internal_name == '' ) {
  115. osc_add_flash_error_message(_m('You have to set an internal name'), 'admin');
  116. $this->redirectTo(osc_admin_base_url(true)."?page=pages&action=add");
  117. }
  118. if( !WebThemes::newInstance()->isValidPage($s_internal_name) ) {
  119. osc_add_flash_error_message(_m('You have to set a different internal name'), 'admin');
  120. $this->redirectTo(osc_admin_base_url(true)."?page=pages&action=add");
  121. }
  122. $aFields = array('s_internal_name' => $s_internal_name, 'b_indelible' => '0');
  123. Session::newInstance()->_setForm('s_internal_name',$s_internal_name);
  124. $page = $this->pageManager->findByInternalName($s_internal_name);
  125. if(!isset($page['pk_i_id'])) {
  126. if($not_empty) {
  127. $result = $this->pageManager->insert($aFields, $aFieldsDescription) ;
  128. Session::newInstance()->_clearVariables();
  129. osc_add_flash_ok_message(_m('The page has been added'), 'admin') ;
  130. $this->redirectTo(osc_admin_base_url(true)."?page=pages");
  131. } else {
  132. osc_add_flash_error_message(_m("The page couldn't be added, at least one title should not be empty"), 'admin') ;
  133. }
  134. } else {
  135. osc_add_flash_error_message(_m("Oops! That internal name is already in use. We can't make the changes"), 'admin') ;
  136. }
  137. $this->redirectTo(osc_admin_base_url(true)."?page=pages&action=add");
  138. break;
  139. case 'delete':
  140. $id = Params::getParam("id");
  141. $page_deleted_correcty = 0;
  142. $page_deleted_error = 0;
  143. $page_indelible = 0;
  144. if(!is_array($id)) {
  145. $id = array($id);
  146. }
  147. foreach($id as $_id) {
  148. $result = (int) $this->pageManager->deleteByPrimaryKey($_id);
  149. switch ($result) {
  150. case -1:
  151. $page_indelible++;
  152. break;
  153. case 0:
  154. $page_deleted_error++;
  155. break;
  156. case 1:
  157. $page_deleted_correcty++;
  158. }
  159. }
  160. if($page_indelible > 0) {
  161. if($page_indelible == 1) {
  162. osc_add_flash_error_message( _m("One page can't be deleted because it is indelible"), 'admin');
  163. } else {
  164. osc_add_flash_error_message(sprintf(_m("%s pages couldn't be deleted because they are indelible"), $page_indelible), 'admin');
  165. }
  166. }
  167. if($page_deleted_error > 0) {
  168. if($page_deleted_error == 1) {
  169. osc_add_flash_error_message(_m("One page couldn't be deleted"), 'admin');
  170. } else {
  171. osc_add_flash_error_message(sprintf(_m("%s pages couldn't be deleted"), $page_deleted_error), 'admin');
  172. }
  173. }
  174. if($page_deleted_correcty > 0) {
  175. if($page_deleted_correcty == 1) {
  176. osc_add_flash_ok_message(_m('One page has been deleted correctly'), 'admin');
  177. } else {
  178. osc_add_flash_ok_message(sprintf(_m('%s pages have been deleted correctly'), $page_deleted_correcty), 'admin');
  179. }
  180. }
  181. $this->redirectTo(osc_admin_base_url(true) . "?page=pages");
  182. break;
  183. default:
  184. if( Params::getParam('iDisplayLength') == '' ) {
  185. Params::setParam('iDisplayLength', 10 );
  186. }
  187. $this->_exportVariableToView('iDisplayLength', Params::getParam('iDisplayLength'));
  188. require_once(osc_admin_base_path() . 'ajax/pages_processing.php');
  189. $params = Params::getParamsAsArray('get');
  190. $pages_processing = new PagesProcessing( $params );
  191. $aData = $pages_processing->result( $params );
  192. $page = (int)Params::getParam('iPage');
  193. if(count($aData['aaData']) == 0 && $page!=1) {
  194. $total = (int)$aData['iTotalDisplayRecords'];
  195. $maxPage = ceil( $total / (int)$aData['iDisplayLength'] ) ;
  196. $url = osc_admin_base_url(true).'?'.$_SERVER['QUERY_STRING'];
  197. if($maxPage==0) {
  198. $url = preg_replace('/&iPage=(\d)+/', '&iPage=1', $url) ;
  199. $this->redirectTo($url) ;
  200. }
  201. if($page > 1) {
  202. $url = preg_replace('/&iPage=(\d)+/', '&iPage='.$maxPage, $url) ;
  203. $this->redirectTo($url) ;
  204. }
  205. }
  206. $this->_exportVariableToView('aPages', $aData);
  207. $this->doView("pages/index.php");
  208. break;
  209. }
  210. }
  211. //hopefully generic...
  212. function doView($file)
  213. {
  214. osc_current_admin_theme_path($file) ;
  215. Session::newInstance()->_clearVariables();
  216. }
  217. }
  218. /* file end: ./oc-admin/pages.php */
  219. ?>