/backend/modules/location/actions/edit.php

https://github.com/jasonweng/forkcms · PHP · 151 lines · 86 code · 21 blank · 44 comment · 25 complexity · e2ed0e45697408eef39d58b4bcac463f MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Fork CMS.
  4. *
  5. * For the full copyright and license information, please view the license
  6. * file that was distributed with this source code.
  7. */
  8. /**
  9. * This is the edit-action, it will display a form to create a new item
  10. *
  11. * @author Matthias Mullie <matthias@mullie.eu>
  12. */
  13. class BackendLocationEdit extends BackendBaseActionEdit
  14. {
  15. /**
  16. * Execute the action
  17. */
  18. public function execute()
  19. {
  20. $this->id = $this->getParameter('id', 'int');
  21. // does the item exists
  22. if($this->id !== null && BackendLocationModel::exists($this->id))
  23. {
  24. parent::execute();
  25. $this->getData();
  26. $this->loadForm();
  27. $this->validateForm();
  28. $this->parse();
  29. $this->display();
  30. }
  31. // no item found, throw an exception, because somebody is fucking with our URL
  32. else $this->redirect(BackendModel::createURLForAction('index') . '&error=non-existing');
  33. }
  34. /**
  35. * Get the data
  36. */
  37. private function getData()
  38. {
  39. $this->record = (array) BackendLocationModel::get($this->id);
  40. // no item found, throw an exceptions, because somebody is fucking with our URL
  41. if(empty($this->record)) $this->redirect(BackendModel::createURLForAction('index') . '&error=non-existing');
  42. }
  43. /**
  44. * Load the form
  45. */
  46. private function loadForm()
  47. {
  48. $this->frm = new BackendForm('edit');
  49. $this->frm->addText('title', $this->record['title'], null, 'inputText title', 'inputTextError title');
  50. $this->frm->addEditor('text', $this->record['text']);
  51. $this->frm->addText('street', $this->record['street']);
  52. $this->frm->addText('number', $this->record['number']);
  53. $this->frm->addText('zip', $this->record['zip']);
  54. $this->frm->addText('city', $this->record['city']);
  55. $this->frm->addDropdown('country', SpoonLocale::getCountries(BL::getInterfaceLanguage()), $this->record['country']);
  56. }
  57. /**
  58. * Parse the form
  59. */
  60. protected function parse()
  61. {
  62. parent::parse();
  63. $settings = BackendModel::getModuleSettings();
  64. // assign to template
  65. $this->tpl->assign('item', $this->record);
  66. $this->tpl->assign('settings', $settings['location']);
  67. // assign message if address was not be geocoded
  68. if($this->record['lat'] == null || $this->record['lng'] == null) $this->tpl->assign('errorMessage', BL::err('AddressCouldNotBeGeocoded'));
  69. }
  70. /**
  71. * Validate the form
  72. */
  73. private function validateForm()
  74. {
  75. if($this->frm->isSubmitted())
  76. {
  77. $this->frm->cleanupFields();
  78. // validate fields
  79. $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired'));
  80. $this->frm->getField('street')->isFilled(BL::err('FieldIsRequired'));
  81. $this->frm->getField('number')->isFilled(BL::err('FieldIsRequired'));
  82. $this->frm->getField('zip')->isFilled(BL::err('FieldIsRequired'));
  83. $this->frm->getField('city')->isFilled(BL::err('FieldIsRequired'));
  84. if($this->frm->isCorrect())
  85. {
  86. // build item
  87. $item['id'] = $this->id;
  88. $item['language'] = BL::getWorkingLanguage();
  89. $item['extra_id'] = $this->record['extra_id'];
  90. $item['title'] = $this->frm->getField('title')->getValue();
  91. $item['text'] = $this->frm->getField('text')->getValue();
  92. $item['street'] = $this->frm->getField('street')->getValue();
  93. $item['number'] = $this->frm->getField('number')->getValue();
  94. $item['zip'] = $this->frm->getField('zip')->getValue();
  95. $item['city'] = $this->frm->getField('city')->getValue();
  96. $item['country'] = $this->frm->getField('country')->getValue();
  97. // check if it's neccessary to geocode again
  98. if($this->record['lat'] === null || $this->record['lng'] === null || $item['street'] != $this->record['street'] || $item['number'] != $this->record['number'] || $item['zip'] != $this->record['zip'] || $item['city'] != $this->record['city'] || $item['country'] != $this->record['country'])
  99. {
  100. // geocode address
  101. $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($item['street'] . ' ' . $item['number'] . ', ' . $item['zip'] . ' ' . $item['city'] . ', ' . SpoonLocale::getCountry($item['country'], BL::getWorkingLanguage())) . '&sensor=false';
  102. $geocode = json_decode(SpoonHTTP::getContent($url));
  103. $item['lat'] = isset($geocode->results[0]->geometry->location->lat) ? $geocode->results[0]->geometry->location->lat : null;
  104. $item['lng'] = isset($geocode->results[0]->geometry->location->lng) ? $geocode->results[0]->geometry->location->lng : null;
  105. }
  106. // old values are still good
  107. else
  108. {
  109. $item['lat'] = $this->record['lat'];
  110. $item['lng'] = $this->record['lng'];
  111. }
  112. // insert the item
  113. $id = BackendLocationModel::update($item);
  114. // edit search index
  115. // @todo why is this commented out?
  116. // if(is_callable(array('BackendSearchModel', 'editIndex'))) BackendSearchModel::editIndex($this->getModule(), (int) $id, array('title' => $item['title'], 'text' => $item['text']));
  117. // everything is saved, so redirect to the overview
  118. if($item['lat'] && $item['lng'])
  119. {
  120. // trigger event
  121. BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $item));
  122. // redirect
  123. $this->redirect(BackendModel::createURLForAction('index') . '&report=edited&var=' . urlencode($item['title']) . '&highlight=row-' . $id);
  124. }
  125. // could not geocode, redirect to edit
  126. else $this->redirect(BackendModel::createURLForAction('edit') . '&id=' . $item['id']);
  127. }
  128. }
  129. }
  130. }