PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/public/admin/page_cities.php

https://gitlab.com/oytunistrator/jobberbase
PHP | 277 lines | 196 code | 70 blank | 11 comment | 27 complexity | bec647fdbcf1a3aac94b480498b4994a MD5 | raw file
  1. <?php
  2. require_once APP_PATH . '_lib/class.CityDAO.php';
  3. class CitiesPage
  4. {
  5. private $template = '';
  6. private $smarty;
  7. private $cityDAO;
  8. public function __construct()
  9. {
  10. $this->cityDAO = CityDAO::getInstance();
  11. }
  12. public function processRequest($action, $cityID, $smarty)
  13. {
  14. $this->smarty = $smarty;
  15. $this->smarty->assign('current_category', 'cities');
  16. switch ($action)
  17. {
  18. case '':
  19. $this->prepareDisplayCities();
  20. break;
  21. case 'prepare-add':
  22. $city = array('name' => '', 'ascii_name' => '');
  23. $this->smarty->assign('action', 'add');
  24. $this->smarty->assign('city', $city);
  25. $this->template = 'city_edit.tpl';
  26. break;
  27. case 'add':
  28. escape($_POST);
  29. $errors = array();
  30. if (isset($GLOBALS['name']) && isset($GLOBALS['ascii_name']))
  31. {
  32. $name = trim($GLOBALS['name']);
  33. $asciiName = trim($GLOBALS['ascii_name']);
  34. $this->validateCity($name, $asciiName, $errors);
  35. if (count($errors) == 0)
  36. {
  37. $cityByAsciiName = $this->getCityByAsciiName($asciiName);
  38. if ($cityByAsciiName)
  39. $errors['asciiName'] = 'Ascii name not unique';
  40. else
  41. {
  42. $this->createCity($name, $asciiName);
  43. $this->prepareDisplayCities();
  44. }
  45. }
  46. // there are errors
  47. if (count($errors))
  48. {
  49. $city['name'] = $_POST['name'];
  50. $city['ascii_name'] = $_POST['ascii_name'];
  51. $this->smarty->assign('action', 'add');
  52. $this->smarty->assign('city', $city);
  53. $this->smarty->assign('errors', $errors);
  54. $this->template = 'city_edit.tpl';
  55. }
  56. }
  57. else
  58. $this->prepareDisplayCities();
  59. break;
  60. case 'prepare-edit':
  61. if (isset($cityID) && $cityID != '')
  62. {
  63. $city = $this->getCityByID($cityID);
  64. if ($city)
  65. {
  66. $this->smarty->assign('action', 'edit');
  67. $this->smarty->assign('city', $city);
  68. $this->template = 'city_edit.tpl';
  69. }
  70. else
  71. $this->prepareDisplayCities();
  72. }
  73. else
  74. $this->prepareDisplayCities();
  75. break;
  76. case 'edit':
  77. escape($_POST);
  78. $errors = array();
  79. if (isset($GLOBALS['name']) && isset($GLOBALS['ascii_name']))
  80. {
  81. $name = trim($GLOBALS['name']);
  82. $asciiName = trim($GLOBALS['ascii_name']);
  83. $cityID = $GLOBALS['id'];
  84. $city = $this->getCityByID($cityID);
  85. $this->validateCity($name, $asciiName, $errors);
  86. if (count($errors) == 0)
  87. {
  88. // if the user has changed the ascii name
  89. if (strcasecmp($city['ascii_name'], $asciiName))
  90. {
  91. $cityByAsciiName = $this->getCityByAsciiName($asciiName);
  92. if ($cityByAsciiName)
  93. $errors['asciiName'] = 'Ascii name not unique';
  94. }
  95. if (count($errors) == 0)
  96. {
  97. $city['name'] = $name;
  98. $city['ascii_name'] = $asciiName;
  99. $this->updateCity($city);
  100. $this->prepareDisplayCities();
  101. }
  102. }
  103. // there are errors
  104. if (count($errors))
  105. {
  106. $city['name'] = $_POST['name'];
  107. $city['ascii_name'] = $_POST['ascii_name'];
  108. $this->smarty->assign('action', 'edit');
  109. $this->smarty->assign('city', $city);
  110. $this->smarty->assign('errors', $errors);
  111. $this->template = 'city_edit.tpl';
  112. }
  113. }
  114. else
  115. $this->prepareDisplayCities();
  116. break;
  117. case 'delete':
  118. if (isset($_POST['cityID']))
  119. {
  120. $cityID = $_POST['cityID'];
  121. $city = $this->getCityByID($cityID);
  122. if ($city)
  123. {
  124. $this->updateJobsForCityDeletion($city);
  125. $this->deleteCity($city);
  126. echo 1;
  127. }
  128. else
  129. echo 0;
  130. exit;
  131. }
  132. else
  133. $this->prepareDisplayCities();
  134. break;
  135. case 'list': ; // do nothing, just fall through
  136. default:
  137. $this->prepareDisplayCities();
  138. }
  139. return $this->template;
  140. }
  141. private function getCityByID($cityID)
  142. {
  143. return $this->cityDAO->getCityByID($cityID);
  144. }
  145. private function prepareAddCity()
  146. {
  147. $city = array('name' => '', 'ascii_name' => '');
  148. $this->smarty->assign('action', 'add');
  149. $this->smarty->assign('city', $city);
  150. }
  151. private function prepareDisplayCities()
  152. {
  153. $cities = $this->getCities();
  154. $this->smarty->assign('cities', $cities);
  155. $this->template = 'cities.tpl';
  156. }
  157. private function getCities()
  158. {
  159. return $this->cityDAO->getCities();
  160. }
  161. private function getCityByAsciiName($asciiName)
  162. {
  163. return $this->cityDAO->getCityByAsciiName($asciiName);
  164. }
  165. private function updateCity($city)
  166. {
  167. $this->cityDAO->updateCity($city);
  168. }
  169. /**
  170. * Validates the name and asciiName and puts the error messages (if any)
  171. * in the $errors array.
  172. * @param $name
  173. * @param $asciiName
  174. * @param $errors
  175. * @return void
  176. */
  177. private function validateCity($name, $asciiName, &$errors)
  178. {
  179. if ($name == '')
  180. $errors['name'] = 'City name';
  181. if ($asciiName == '')
  182. $errors['asciiName'] = 'Ascii name';
  183. else
  184. if (preg_match('/([^a-z0-9\-_]+?)/i', $asciiName))
  185. $errors['asciiName'] = 'Ascii name must contain only alphanumerical characters, dashes and underscores';
  186. }
  187. private function createCity($name, $asciiName)
  188. {
  189. $city = array('name' => $name, 'ascii_name' => $asciiName);
  190. $this->cityDAO->insertCity($city);
  191. }
  192. private function updateJobsForCityDeletion($city)
  193. {
  194. global $db;
  195. $query = 'UPDATE '.DB_PREFIX.'jobs SET
  196. city_id = NULL,
  197. outside_location = "'. $db->real_escape_string($city['name']) .'"
  198. WHERE
  199. city_id = ' . $city['id'];
  200. $db->query($query);
  201. }
  202. private function deleteCity($city)
  203. {
  204. $this->cityDAO->deleteCity($city);
  205. }
  206. }
  207. ?>