| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | class JobTerms { |
| 23 | |
| 24 | const ALL = 'all'; |
| 25 | const ONLY_JOBS = 'jobs'; |
| 26 | const ONLY_MENTORS = 'mentors'; |
| 27 | |
| 28 | static public function getSubTerms($parent_jtid, $filter = self::ALL) { |
| 29 | switch ($filter) { |
| 30 | case self::ALL: |
| 31 | default: |
| 32 | $table = ''; |
| 33 | break; |
| 34 | case self::ONLY_JOBS: |
| 35 | $table = 'profile_job_term'; |
| 36 | break; |
| 37 | case self::ONLY_MENTORS: |
| 38 | $table = 'profile_mentor_term'; |
| 39 | break; |
| 40 | } |
| 41 | if ($table) { |
| 42 | $count = ', COUNT(DISTINCT j.pid) AS nb'; |
| 43 | $join = ' INNER JOIN profile_job_term_relation AS r2 ON (r2.jtid_1 = e.jtid) |
| 44 | INNER JOIN '.$table.' AS j ON (j.jtid = r2.jtid_2)'; |
| 45 | } else { |
| 46 | $count = $join = ''; |
| 47 | } |
| 48 | return XDB::iterator('SELECT e.jtid, e.name, e.full_name'.$count.' |
| 49 | FROM profile_job_term_enum AS e |
| 50 | INNER JOIN profile_job_term_relation AS r ON (r.jtid_2 = e.jtid)'.$join.' |
| 51 | WHERE r.jtid_1 = {?} AND r.computed = "original" |
| 52 | GROUP BY e.jtid |
| 53 | ORDER BY e.name', |
| 54 | $parent_jtid); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Display a JSon page containing the sub-branches of a branch in the job terms tree. |
| 59 | * @param $page the Platal page |
| 60 | * @param $filter filter helps to display only jobterms that are contained in jobs or in mentors |
| 61 | * |
| 62 | * @param Env::i('jtid') job term id of the parent branch, if none trunk will be used |
| 63 | * @param Env::v('attrfunc') the name of a javascript function that will be called when a branch |
| 64 | * is chosen |
| 65 | * @param Env::v('treeid') tree id that will be given as first argument of attrfunc function |
| 66 | * the second argument will be the chosen job term id and the third one the chosen job's full name. |
| 67 | */ |
| 68 | static public function ajaxGetBranch(&$page, $filter = self::ALL) { |
| 69 | pl_content_headers('application/json'); |
| 70 | $page->changeTpl('include/jobterms.branch.tpl', NO_SKIN); |
| 71 | $subTerms = self::getSubTerms(Env::v('jtid'), $filter); |
| 72 | $page->assign('subTerms', $subTerms); |
| 73 | switch ($filter) { |
| 74 | case self::ONLY_JOBS: |
| 75 | $page->assign('filter', 'camarade'); |
| 76 | break; |
| 77 | case self::ONLY_MENTORS: |
| 78 | $page->assign('filter', 'mentor'); |
| 79 | break; |
| 80 | } |
| 81 | $page->assign('attrfunc', Env::v('attrfunc')); |
| 82 | $page->assign('treeid', Env::v('treeid')); |
| 83 | } |
| 84 | |
| 85 | static public function jsAddTree($platalpage, $domElement = '.term_tree', $treeid = '', $attrfunc = '') { |
| 86 | return '$("'.addslashes($domElement).'").jstree({ |
| 87 | "core" : {"strings":{"loading":"Chargement ..."}}, |
| 88 | "plugins" : ["themes","json_data"], |
| 89 | "themes" : { "url" : platal_baseurl + "css/jstree.css" }, |
| 90 | "json_data" : { "ajax" : { |
| 91 | "url" : platal_baseurl + "'.addslashes($platalpage).'", |
| 92 | "data" : function(nod) { |
| 93 | var jtid = 0; |
| 94 | if (nod != -1) { |
| 95 | jtid = nod.attr("id").replace(/^.*_([0-9]+)$/, "$1"); |
| 96 | } |
| 97 | return { "jtid" : jtid, "treeid" : "'.addslashes($treeid).'", "attrfunc" : "'.addslashes($attrfunc).'" } |
| 98 | } |
| 99 | }} });'; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Extract search token from term |
| 104 | * @param $term a utf-8 string that can contain any char |
| 105 | * @param an array of elementary tokens |
| 106 | */ |
| 107 | static public function tokenize($term) |
| 108 | { |
| 109 | $term = mb_strtoupper(replace_accent($term)); |
| 110 | $term = str_replace(array('/', ',', '(', ')', '"', '&', '»', '«'), ' ', $term); |
| 111 | $tokens = explode(' ', $term); |
| 112 | static $not_tokens = array('ET','AND','DE','DES','DU','D\'','OU','L\'','LA','LE','LES','PAR','AU','AUX','EN','SUR','UN','UNE','IN'); |
| 113 | foreach ($tokens as &$t) { |
| 114 | $t = preg_replace(array('/^-+/','/-+$/'), '', $t); |
| 115 | if (substr($t, 1, 1) == '\'' && in_array(substr($t, 0, 2), $not_tokens)) { |
| 116 | $t = substr($t, 2); |
| 117 | } |
| 118 | if (strlen($t) == 1 || in_array($t, $not_tokens)) { |
| 119 | $t = false; |
| 120 | continue; |
| 121 | } |
| 122 | } |
| 123 | return array_filter($tokens); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Create the INNER JOIN query to restrict search to some job terms |
| 128 | * @param $tokens an array of the job terms to look for (LIKE comp) |
| 129 | * @param $table_alias the alias or name of the table with a jtid field to restrict |
| 130 | * @return a partial SQL query |
| 131 | */ |
| 132 | static public function token_join_query(array $tokens, $table_alias) { |
| 133 | $joins = ''; |
| 134 | $i = 0; |
| 135 | foreach ($tokens as $t) { |
| 136 | ++$i; |
| 137 | $joins .= ' INNER JOIN profile_job_term_search AS s'.$i.' ON(s'.$i.'.jtid = '.$table_alias.'.jtid AND s'.$i.'.search LIKE '.XDB::escape($t).')'; |
| 138 | } |
| 139 | return $joins; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 144 | ?> |