From 658cffd4cd8d42cb5c394663b00514dae036f834 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Sun, 2 Oct 2011 21:46:53 +0200 Subject: [PATCH] Adapts new autocomplete to jobterms. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- htdocs/javascript/profile.js | 30 +++--------- modules/profile.php | 106 ++++++++++++++++++++++++++--------------- modules/search.php | 7 +-- templates/profile/jobs.job.tpl | 11 ++--- templates/profile/mentor.tpl | 14 +++--- templates/search/referent.tpl | 23 +++++---- 6 files changed, 102 insertions(+), 89 deletions(-) diff --git a/htdocs/javascript/profile.js b/htdocs/javascript/profile.js index a8b944d..7152981 100644 --- a/htdocs/javascript/profile.js +++ b/htdocs/javascript/profile.js @@ -657,44 +657,28 @@ function removeJobTerm() } /** - * Prepare display for autocomplete suggestions in job terms - * @param row an array of (title of term, id of term) - * @return text to display - * If id is negative, it is because there are too much terms to - * be displayed. - */ -function displayJobTerm(row) -{ - if (row[1] < 0) { - return '... parcourir les résultats dans un arbre ...'; - } - return row[0]; -} - -/** * Function called when a job term has been selected from autocompletion * in search * @param li is the list item (
  • ) that has been clicked * The context is the jsquery autocomplete object. */ -function selectJobTerm(li) +function selectJobTerm(id, value, jobid) { - var jobid = this.extraParams.jobid; - if (li.extra[0] >= 0) { - addJobTerm(jobid,li.extra[0],$(li).text()); + if (id >= 0) { + addJobTerm(jobid, id, value); } var search_input; if (jobid < 0) { search_input = $('.term_search')[0]; } else { - search_input = $('#jobs_'+jobid+' .term_search')[0]; + search_input = $('#jobs_' + jobid + ' .term_search')[0]; } - if (li.extra[0] >= 0) { + if (id >= 0) { search_input.value = ''; search_input.focus(); } else { - search_input.value = li.selectValue.replace(/%$/,''); - toggleJobTermsTree(jobid, li.selectValue); + search_input.value = value.replace(/%$/, ''); + toggleJobTermsTree(jobid, ''); // Use given value instead } } diff --git a/modules/profile.php b/modules/profile.php index f516a2b..07e9189 100644 --- a/modules/profile.php +++ b/modules/profile.php @@ -561,7 +561,7 @@ class ProfileModule extends PLModule { pl_content_headers("text/plain"); - $q = Env::v('q').'%'; + $q = Env::v('term') . '%'; $tokens = JobTerms::tokenize($q); if (count($tokens) == 0) { exit; @@ -569,51 +569,79 @@ class ProfileModule extends PLModule sort($tokens); $q_normalized = implode(' ', $tokens); - // try to look in cached results + // Try to look in cached results. + $cached = false; $cache = XDB::query('SELECT result FROM search_autocomplete - WHERE name = {?} AND - query = {?} AND - generated > NOW() - INTERVAL 1 DAY', - $type, $q_normalized); - if ($res = $cache->fetchOneCell()) { - echo $res; - die(); - } - - $joins = JobTerms::token_join_query($tokens, 'e'); - if ($type == 'mentor') { - $count = ', COUNT(DISTINCT pid) AS nb'; - $countjoin = ' INNER JOIN profile_job_term_relation AS r ON(r.jtid_1 = e.jtid) INNER JOIN profile_mentor_term AS m ON(r.jtid_2 = m.jtid)'; - $countorder = 'nb DESC, '; + WHERE name = {?} AND query = {?} AND generated > NOW() - INTERVAL 1 DAY', + $type, $q_normalized); + + if ($cache->numRows() > 0) { + $cached = true; + $data = explode("\n", $cache->fetchOneCell()); + $list = array(); + foreach ($data as $line) { + if ($line != '') { + $aux = explode("\t", $line); + if ($type == 'mentor') { + $item = array( + 'field' => $aux[0], + 'nb' => $aux[1], + 'id' => $aux[2] + ); + $item['value'] = SearchModule::format_autocomplete($item); + } else { + $item = array( + 'value' => $aux[0], + 'id' => $aux[1] + ); + } + array_push($list, $item); + } + } } else { - $count = $countjoin = $countorder = ''; - } - $list = XDB::iterator('SELECT e.jtid AS id, e.full_name AS field'.$count.' - FROM profile_job_term_enum AS e '.$joins.$countjoin.' - GROUP BY e.jtid - ORDER BY '.$countorder.'field - LIMIT 11'); - $nbResults = 0; - $res = ''; - while ($result = $list->next()) { - $nbResults++; - if ($nbResults == 11) { - $res .= $q."|-1\n"; + $joins = JobTerms::token_join_query($tokens, 'e'); + if ($type == 'mentor') { + $count = ', COUNT(DISTINCT pid) AS nb'; + $countjoin = ' INNER JOIN profile_job_term_relation AS r ON(r.jtid_1 = e.jtid) INNER JOIN profile_mentor_term AS m ON(r.jtid_2 = m.jtid)'; + $countorder = 'nb DESC, '; } else { - $res .= $result['field'].'|'; - if ($count) { - $res .= $result['nb'].'|'; + $count = $countjoin = $countorder = ''; + } + $list = XDB::fetchAllAssoc('SELECT e.jtid AS id, e.full_name AS field' . $count . ' + FROM profile_job_term_enum AS e ' . $joins . $countjoin . ' + GROUP BY e.jtid + ORDER BY ' . $countorder . 'field + LIMIT ' . DirEnumeration::AUTOCOMPLETE_LIMIT); + $to_cache = ''; + if ($type == 'mentor') { + foreach ($list as &$item) { + $to_cache .= $item['field'] . "\t" . $item['nb'] . "\t" . $item['id'] . "\n"; + $item['value'] = SearchModule::format_autocomplete($item); + } + } else { + foreach ($list as &$item) { + $to_cache .= $item['field'] . "\t" . $item['id'] . "\n"; + $item['value'] = $item['field']; } - $res .= $result['id']; } - $res .= "\n"; } - XDB::query('INSERT INTO search_autocomplete (name, query, result, generated) - VALUES ({?}, {?}, {?}, NOW()) - ON DUPLICATE KEY UPDATE result = VALUES(result), generated = VALUES(generated)', - $type, $q_normalized, $res); - echo $res; + + if (count($list) == DirEnumeration::AUTOCOMPLETE_LIMIT && $type == 'nomentor') { + $list[] = array( + 'value' => '… parcourir les résultats dans un arbre …', + 'field' => '', + 'id' => -1 + ); + } + + if (!$cached) { + XDB::query('INSERT INTO search_autocomplete (name, query, result, generated) + VALUES ({?}, {?}, {?}, NOW()) + ON DUPLICATE KEY UPDATE result = VALUES(result), generated = VALUES(generated)', + $type, $q_normalized, $to_cache); + } + echo json_encode($list); exit(); } diff --git a/modules/search.php b/modules/search.php index edf62ef..fc2b811 100644 --- a/modules/search.php +++ b/modules/search.php @@ -213,7 +213,7 @@ class SearchModule extends PLModule $page->assign('public_directory',0); } - private function format_autocomplete(array $item) + static public function format_autocomplete(array $item) { return $item['field'] . ' (' . $item['nb'] . ' camarade' . ($item['nb'] > 1 ? 's' : '') . ')'; } @@ -262,7 +262,7 @@ class SearchModule extends PLModule 'nb' => $aux[1], 'id' => $aux[2] ); - $item['value'] = $this->format_autocomplete($item); + $item['value'] = self::format_autocomplete($item); array_push($list, $item); } } @@ -292,7 +292,7 @@ class SearchModule extends PLModule $to_cache = ''; foreach ($list as &$item) { $to_cache .= $item['field'] . "\t" . $item['nb'] . "\t" . $item['id'] . "\n"; - $item['value'] = $this->format_autocomplete($item); + $item['value'] = self::format_autocomplete($item); } } @@ -414,6 +414,7 @@ class SearchModule extends PLModule $wp->buildCache(); $page->setTitle('Emploi et Carrières'); + $page->addJsLink('jquery.ui.xorg.js'); // Count mentors $res = XDB::query("SELECT count(distinct pid) FROM profile_mentor_term"); diff --git a/templates/profile/jobs.job.tpl b/templates/profile/jobs.job.tpl index 25cc2ca..fef99e9 100644 --- a/templates/profile/jobs.job.tpl +++ b/templates/profile/jobs.job.tpl @@ -141,13 +141,12 @@ {foreach from=$job.terms item=term} addJobTerm("{$i}", "{$term.jtid}", "{$term.full_name|replace:'"':'\\"'}"); {/foreach} - $('#jobs_{$i} .term_search').autocomplete($.plURL('profile/jobterms'), + $('#jobs_{$i} .term_search').autocomplete( {ldelim} - "formatItem" : displayJobTerm, - "extraParams" : {ldelim} "jobid" : "{$i}" {rdelim}, - "width" : $('#jobs_{$i} .term_search').width()*2, - "onItemSelect" : selectJobTerm, - "matchSubset" : false + source: $.plURL('profile/jobterms'), + select: function(event, ui) {ldelim} + selectJobTerm(ui.item.id, ui.item.value, {$i}); + {rdelim} {rdelim}); {rdelim}); /* ]]> */ diff --git a/templates/profile/mentor.tpl b/templates/profile/mentor.tpl index 83c1585..86d7e17 100644 --- a/templates/profile/mentor.tpl +++ b/templates/profile/mentor.tpl @@ -187,14 +187,12 @@ {foreach from=$terms item=term} addJobTerm(-1, "{$term.jtid}", "{$term.full_name|replace:'"':'\\"'}"); {/foreach} - $('.term_search').autocomplete($.plURL('profile/jobterms'), - {ldelim} - "formatItem" : displayJobTerm, - "extraParams" : {ldelim} "jobid" : "-1" {rdelim}, - "width" : $('.term_search').width()*2, - "onItemSelect" : selectJobTerm, - "matchSubset" : false - {rdelim}); + $('.term_search').autocomplete({ldelim} + source: $.plURL('profile/jobterms'), + select: function(event, ui) {ldelim} + selectJobTerm(ui.item.id, ui.item.value, -1); + {rdelim} + {rdelim}); {rdelim}); /* ]]> */ diff --git a/templates/search/referent.tpl b/templates/search/referent.tpl index f11a265..0b1f12e 100644 --- a/templates/search/referent.tpl +++ b/templates/search/referent.tpl @@ -48,12 +48,12 @@ function toggleJobTermsTree() } /** Function called by autocomplete when a term is selected */ -function selectJobTerm(li) +function selectJobTerm(id, value) { - if (li.extra[1] < 0) { + if (value < 0) { return; } - chooseJobTermInTree(null,li.extra[1],li.selectValue); + chooseJobTermInTree(null, id, value); } /** Prepares display for a jobterm in select's dropdown @@ -146,13 +146,16 @@ function validateSearchForm(f) {literal} $(function() { createJobTermsTree('#mentoring_terms', 'profile/ajax/tree/jobterms/mentors', 'mentor', 'chooseJobTermInTree'); - $("#jobtermText").autocomplete(baseurl + "autocomplete", - { - "selectOnly":1, - "width":$("#jobtermText").width()*2, - "onItemSelect" : selectJobTerm, - "formatItem" : displayJobTerm, - "matchSubset" : false + $('#jobtermText').autocomplete({ + source: $.plURL(baseurl + 'autocomplete'), + select: function(event, ui) { + selectJobTerm(ui.item.id, ui.item.value); + }, + change: function(event, ui) { + if (ui.item != null && ui.item.field != null) { + $(this).val(ui.item.field); + } + } }); $('#jobTermsTreeToggle').click(toggleJobTermsTree); {/literal} -- 2.1.4