[Isocial-svn] SF.net SVN: isocial: [111]
Status: Pre-Alpha
Brought to you by:
aguidrevitch
From: <agu...@us...> - 2008-03-21 19:58:54
|
Revision: 111 http://isocial.svn.sourceforge.net/isocial/?rev=111&view=rev Author: aguidrevitch Date: 2008-03-21 12:59:00 -0700 (Fri, 21 Mar 2008) Log Message: ----------- city autocompletion improved Modified Paths: -------------- app/application_controller.php app/helpers/city_helper.php app/models/city.php app/views/profile/basic.tpl public/javascripts/autocomplete.js Modified: app/application_controller.php =================================================================== --- app/application_controller.php 2008-03-20 20:03:08 UTC (rev 110) +++ app/application_controller.php 2008-03-21 19:59:00 UTC (rev 111) @@ -183,39 +183,53 @@ function _to_base64_utf8 ($value) { return "=?utf-8?B?" . base64_encode($value) . "?="; } + + function _get_conditions_for_city( $to_ascii = true) { + $query = $this->params['city']; + $aquery = utf8_to_ascii($query); + if ($to_ascii && $aquery != $query) { + $conditions = array( + 'name LIKE ? OR name LIKE ?', $query . '%', $aquery . '%' + ); + } else { + $conditions = array( + 'name LIKE ?', $query . '%' + ); + } + return $conditions; + } function auto_complete_for_city () { if (empty($this->params['city'])) { $this->renderNothing(); } else { - $query = $this->params['city']; - $aquery = utf8_to_ascii($query); - if ($aquery != $query) { - $conditions = array( - 'name LIKE ? OR name LIKE ?', $query . '%', $aquery . '%' - ); - } else { - $conditions = array( - 'name LIKE ?', $query . '%' - ); - } - $entries = $this->City->find('all', array( - 'conditions' => $conditions, + 'conditions' => $this->_get_conditions_for_city(), 'include' => 'country', 'limit' => 10, 'order' => 'name' ) ); - if (!empty($entries)) { - $this->renderText( $this->city_helper->auto_complete_result($entries, $query, $this->params['city']) ); - } else { - $this->renderNothing(); - } + + $this->renderText(empty($entries) ? ' ' : $this->city_helper->auto_complete_result($entries, $this->params['city'])); } } + function get_id_for_city () { + if (empty($this->params['city'])) { + $this->renderNothing(); + } else { + $entry = $this->City->findFirst( + array( + 'conditions' => $this->_get_conditions_for_city(false), + ) + ); + + $this->renderText(empty($entry) ? ' ' : $entry->toJson()); + } + } + function unread_messages_count () { return $this->current_user ? $this->Message->unread_messages_count($this->current_user->getId()) : 0; } Modified: app/helpers/city_helper.php =================================================================== --- app/helpers/city_helper.php 2008-03-20 20:03:08 UTC (rev 110) +++ app/helpers/city_helper.php 2008-03-21 19:59:00 UTC (rev 111) @@ -25,36 +25,36 @@ function city_field($object, $method, $tag_options = array(), $completion_options = array()) { $this->object =& $this->_controller->{$object}; - $this->value = ''; - if ($this->object && $city_id = $this->object->get($method)) { - $city = $this->_controller->City->find($city_id); - if (!empty($city)) { - $this->value = $city->name; + $this->value = ''; + if ($this->object && $city_id = $this->object->get($method)) { + $city = $this->_controller->City->find($city_id); + if (!empty($city)) { + $this->value = $city->name; + } } - } - $tag_options = array_merge(array( - 'value' => $this->value, - 'name' => "city", - 'id' => "auto_{$object}_{$method}", - 'onblur' => "city_updated(this, '{$object}_{$method}_prev', '{$object}_{$method}')", - 'autocomplete' => 'on', + $tag_options = array_merge(array( + 'value' => $this->value, + 'name' => "city", + 'id' => "auto_{$object}_{$method}", + 'onblur' => "city_updated(this, '{$object}_{$method}_prev', '{$object}_{$method}')", + 'autocomplete' => 'on', ), $tag_options); - $completion_options = array_merge(array( - 'skip_style' => true, - 'url' => array('action' => 'auto_complete_for_city'), - 'frequency' => 0.4, - 'indicator' => "'auto_{$object}_{$method}'", - 'afterUpdateElement' => "function (text, li) { \$('{$object}_{$method}').value = li.id; \$('{$object}_{$method}_prev').value = text.value; }" + $completion_options = array_merge(array( + 'skip_style' => true, + 'url' => array('action' => 'auto_complete_for_city'), + 'frequency' => 0.4, + 'indicator' => "'auto_{$object}_{$method}'", + 'afterUpdateElement' => "function (text, li) { \$('{$object}_{$method}').value = li.id; \$('{$object}_{$method}_prev').value = text.value; }" ), $completion_options); $hidden = $this->_controller->form_helper->hidden_field($object, $method); $hidden_prev = $this->_controller->form_helper->hidden_field(null, null, array('id' => "{$object}_{$method}_prev", 'name' => "{$object}_{$method}_prev", 'value' => $this->value)); $text_field = $this->_controller->form_helper->text_field(null, null, $tag_options); - $div = TagHelper::content_tag('div', '', array('id' => "auto_{$object}_{$method}_auto_complete", 'class' => 'auto_complete')); + $div = TagHelper::content_tag('div', '', array('id' => "auto_{$object}_{$method}_auto_complete", 'class' => 'auto_complete')); $javascript = $this->auto_complete_field("auto_{$object}_{$method}", $completion_options); - return $hidden . $hidden_prev . $text_field . $div . $javascript; + return $hidden . $hidden_prev . $text_field . $div . $javascript; } } Modified: app/models/city.php =================================================================== --- app/models/city.php 2008-03-20 20:03:08 UTC (rev 110) +++ app/models/city.php 2008-03-21 19:59:00 UTC (rev 111) @@ -3,7 +3,7 @@ class City extends VotableActiveRecord { var $belongs_to = array('Country'); - var $votes_model = 'ReligiousViewVote'; + var $votes_model = 'CityViewVote'; } class CityVote extends ActiveRecord Modified: app/views/profile/basic.tpl =================================================================== --- app/views/profile/basic.tpl 2008-03-20 20:03:08 UTC (rev 110) +++ app/views/profile/basic.tpl 2008-03-21 19:59:00 UTC (rev 111) @@ -29,7 +29,7 @@ <label></label> <input class="submitinput" type="submit" value="_{Save Changes}"> <input class="cancelinput" type="button" value="_{Cancel}"> - <a href="javascript: alert($F('basic_city') + ':' + $F('basic_city_prev'))">asdfsadf</a> + <a href="javascript: alert($F('basic_profile_city_id'))">(debug)</a> </div> </div> </form> Modified: public/javascripts/autocomplete.js =================================================================== --- public/javascripts/autocomplete.js 2008-03-20 20:03:08 UTC (rev 110) +++ public/javascripts/autocomplete.js 2008-03-21 19:59:00 UTC (rev 111) @@ -14,14 +14,31 @@ }); -function city_updated (input, prev, hidden) { +function city_updated (input, prev, hidden, form) { if ($F(input) && (!$F(hidden) || $F(input) != $F(prev))) { - $(input).value = ''; - $(hidden).value = ''; + $(hidden).value = ''; $(prev).value = ''; - new Effect.Highlight(input, { keepBackgroundImage: true, startcolor: '#ff0000', afterFinish: function (obj) { obj.element.setStyle('') } }); + + // now we should re-check + // whether the city is in the database and put it's id to the hidden + new Ajax.Request('/profile/get_id_for_city', { + postBody: 'city=' + $F(input), + onSuccess: function(response) { + if (response.responseText.isJSON()) { + var data = response.responseText.evalJSON(true); + if (data.id) { + $(hidden).value = data.id; + $(input).value = data.name; + $(prev).value = data.name; + } + } else { + $(form).show(); + } + } + }); + } else if (!$F(input)) { - $(hidden).value = ''; + $(hidden).value = ''; $(prev).value = ''; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |