PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/app/models/location.php

http://noserub.googlecode.com/
PHP | 155 lines | 121 code | 15 blank | 19 comment | 7 complexity | 0bd5313bc9f19574e64b9ee9355692ad MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /* SVN FILE: $Id:$ */
  3. class Location extends AppModel {
  4. public $belongsTo = array('Identity');
  5. public $hasMany = array(
  6. 'Entry' => array(
  7. 'foreignKey' => 'foreign_key',
  8. 'conditions' => array(
  9. 'Entry.model' => 'location'
  10. )
  11. ),
  12. 'Event'
  13. );
  14. public $actsAs = array(
  15. 'Sluggable' => array(
  16. 'label' => 'name',
  17. 'translation' => 'utf-8'
  18. ));
  19. public function getTypes() {
  20. return array(
  21. 0 => __('Unknown', true),
  22. 1 => __('Art & Entertainment', true),
  23. 2 => __('Education', true),
  24. 3 => __('Eating & Drinking', true),
  25. 4 => __('Events', true),
  26. 5 => __('Health & Beauty', true),
  27. 6 => __('Hotel & Holidays', true),
  28. 7 => __('Nightlife', true),
  29. 8 => __('Services', true),
  30. 9 => __('Shopping', true),
  31. 10 => __('Sports', true),
  32. 11 => __('Transportation', true),
  33. 12 => __('Other', true)
  34. );
  35. }
  36. public function add($data) {
  37. $this->create();
  38. if($this->save($data)) {
  39. // now set UID
  40. $url = Router::url('/locations/view/' . $this->id . '/', true);
  41. $this->saveField('uid', md5($url));
  42. return true;
  43. }
  44. }
  45. /**
  46. * Returns the last $limit locations this user created
  47. *
  48. * @param int $identity_id
  49. * @param int $limit
  50. *
  51. * @return array
  52. */
  53. public function getNew($identity_id, $limit = 5, $type = 'all') {
  54. return $this->find($type, array(
  55. 'contain' => false,
  56. 'conditions' => array(
  57. 'identity_id' => $identity_id
  58. ),
  59. 'limit' => $limit,
  60. 'order' => 'created DESC'
  61. ));
  62. }
  63. /**
  64. * get all locations for the logged in identity
  65. *
  66. * @param int $identity_id
  67. *
  68. * @return array
  69. */
  70. public function get($identity_id) {
  71. return $this->find('all', array(
  72. 'contain' => false,
  73. 'conditions' => array(
  74. 'Location.identity_id' => $identity_id
  75. ),
  76. 'order' => 'Location.name'
  77. ));
  78. }
  79. public function setTo($identity_id, $location_id) {
  80. # check, that this location belongs to that identity
  81. $this->contain();
  82. $location = $this->findById($location_id);
  83. if($location['Location']['identity_id'] != $identity_id) {
  84. return false;
  85. }
  86. $this->Identity->id = $identity_id;
  87. $frontpage_updates = $this->Identity->field('frontpage_updates');
  88. $this->Identity->Entry->setLocation($identity_id, $location, $frontpage_updates == 0);
  89. return true;
  90. }
  91. public function export($identity_id) {
  92. $this->contain();
  93. $data = $this->findAllByIdentityId($identity_id);
  94. $locations = array();
  95. foreach($data as $item) {
  96. $location = $item['Location'];
  97. unset($location['id']);
  98. unset($location['identity_id']);
  99. unset($location['created']);
  100. unset($location['modified']);
  101. $locations[] = $location;
  102. }
  103. return $locations;
  104. }
  105. public function import($identity_id, $data) {
  106. foreach($data as $item) {
  107. # first check, if we already have a location by this name
  108. $this->contain();
  109. $conditions = array(
  110. 'Location.identity_id' => $identity_id,
  111. 'Location.name' => $item['name']
  112. );
  113. if (!$this->hasAny($conditions)) {
  114. $this->create();
  115. $saveable = array('identity_id', 'name', 'address', 'latitude', 'longitude');
  116. $item['identity_id'] = $identity_id;
  117. if(!$this->save($item, true, $saveable)) {
  118. return false;
  119. }
  120. }
  121. }
  122. return true;
  123. }
  124. public function updateLastActivity() {
  125. if(!$this->exists()) {
  126. return false;
  127. }
  128. $this->saveField('last_activity', date('Y-m-d H:i:s'));
  129. }
  130. public function saveInContext($data) {
  131. Context::write('location', array(
  132. 'id' => $data['Location']['id'],
  133. 'slug' => $data['Location']['slug'],
  134. 'name' => $data['Location']['name'],
  135. 'latitude' => $data['Location']['latitude'],
  136. 'longitude' => $data['Location']['longitude'],
  137. 'last_activity' => $data['Location']['last_activity']
  138. ));
  139. }
  140. }