Commit | Line | Data |
---|---|---|
3ac45f10 PC |
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 | ||
f31c2a24 PC |
28 | /** Retreives subterms of a term to display in a tree |
29 | * @param $parent_jtid the id of the parent jobterm | |
30 | * @param $filter enable filtering on jobterms that have been used in jobs or in mentors, in | |
31 | * that case, each row as an 'nb' field that counts all profiles that match | |
32 | * @param $token_filter a text (usually ending by %) the retreived subterms to jobterms that | |
33 | * contains this tokens or that one of their children contains it. | |
34 | * @return an iterator over jobterms with jtid, name, full_name and nb | |
35 | */ | |
36 | static public function getSubTerms($parent_jtid, $filter = self::ALL, $token_filter = null) { | |
3ac45f10 PC |
37 | switch ($filter) { |
38 | case self::ALL: | |
39 | default: | |
40 | $table = ''; | |
41 | break; | |
42 | case self::ONLY_JOBS: | |
43 | $table = 'profile_job_term'; | |
44 | break; | |
45 | case self::ONLY_MENTORS: | |
46 | $table = 'profile_mentor_term'; | |
47 | break; | |
48 | } | |
49 | if ($table) { | |
50 | $count = ', COUNT(DISTINCT j.pid) AS nb'; | |
51 | $join = ' INNER JOIN profile_job_term_relation AS r2 ON (r2.jtid_1 = e.jtid) | |
52 | INNER JOIN '.$table.' AS j ON (j.jtid = r2.jtid_2)'; | |
53 | } else { | |
54 | $count = $join = ''; | |
55 | } | |
f31c2a24 PC |
56 | if (!empty($token_filter)) { |
57 | $join .= ' INNER JOIN profile_job_term_relation AS rtf ON (rtf.jtid_1 = e.jtid) '. | |
58 | self::token_join_query(self::tokenize($token_filter), 'rtf', 'jtid_2'); | |
59 | } | |
f2d397df | 60 | return XDB::iterator('SELECT e.jtid, e.name, e.full_name'.$count.', IF(rf.jtid_1 IS NULL, 1, 0) AS leaf |
3ac45f10 PC |
61 | FROM profile_job_term_enum AS e |
62 | INNER JOIN profile_job_term_relation AS r ON (r.jtid_2 = e.jtid)'.$join.' | |
f2d397df | 63 | LEFT JOIN profile_job_term_relation AS rf ON (rf.jtid_1 = e.jtid AND rf.computed = "original") |
3ac45f10 PC |
64 | WHERE r.jtid_1 = {?} AND r.computed = "original" |
65 | GROUP BY e.jtid | |
66 | ORDER BY e.name', | |
67 | $parent_jtid); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Display a JSon page containing the sub-branches of a branch in the job terms tree. | |
72 | * @param $page the Platal page | |
73 | * @param $filter filter helps to display only jobterms that are contained in jobs or in mentors | |
74 | * | |
75 | * @param Env::i('jtid') job term id of the parent branch, if none trunk will be used | |
76 | * @param Env::v('attrfunc') the name of a javascript function that will be called when a branch | |
77 | * is chosen | |
78 | * @param Env::v('treeid') tree id that will be given as first argument of attrfunc function | |
79 | * the second argument will be the chosen job term id and the third one the chosen job's full name. | |
f31c2a24 PC |
80 | * @param Env::v('text_filter') a string (usually ending by %) that will be used to filter |
81 | * subbranches, keeping only the one containing this text in its title or in the title of one of | |
82 | * its subbranches. | |
3ac45f10 PC |
83 | */ |
84 | static public function ajaxGetBranch(&$page, $filter = self::ALL) { | |
85 | pl_content_headers('application/json'); | |
86 | $page->changeTpl('include/jobterms.branch.tpl', NO_SKIN); | |
f31c2a24 | 87 | $subTerms = self::getSubTerms(Env::v('jtid'), $filter, Env::v('text_filter')); |
3ac45f10 PC |
88 | $page->assign('subTerms', $subTerms); |
89 | switch ($filter) { | |
90 | case self::ONLY_JOBS: | |
91 | $page->assign('filter', 'camarade'); | |
92 | break; | |
93 | case self::ONLY_MENTORS: | |
94 | $page->assign('filter', 'mentor'); | |
95 | break; | |
96 | } | |
f2d397df | 97 | $page->assign('jtid', Env::v('jtid')); |
f31c2a24 | 98 | $page->assign('text_filter', Env::v('text_filter')); |
3ac45f10 PC |
99 | $page->assign('attrfunc', Env::v('attrfunc')); |
100 | $page->assign('treeid', Env::v('treeid')); | |
101 | } | |
102 | ||
103 | static public function jsAddTree($platalpage, $domElement = '.term_tree', $treeid = '', $attrfunc = '') { | |
104 | return '$("'.addslashes($domElement).'").jstree({ | |
105 | "core" : {"strings":{"loading":"Chargement ..."}}, | |
106 | "plugins" : ["themes","json_data"], | |
107 | "themes" : { "url" : platal_baseurl + "css/jstree.css" }, | |
108 | "json_data" : { "ajax" : { | |
109 | "url" : platal_baseurl + "'.addslashes($platalpage).'", | |
110 | "data" : function(nod) { | |
111 | var jtid = 0; | |
112 | if (nod != -1) { | |
113 | jtid = nod.attr("id").replace(/^.*_([0-9]+)$/, "$1"); | |
114 | } | |
f31c2a24 PC |
115 | return { |
116 | "jtid" : jtid, | |
117 | "treeid" : "'.addslashes($treeid).'", | |
118 | "attrfunc" : "'.addslashes($attrfunc).'" | |
119 | } | |
3ac45f10 PC |
120 | } |
121 | }} });'; | |
122 | } | |
123 | ||
124 | /** | |
125 | * Extract search token from term | |
126 | * @param $term a utf-8 string that can contain any char | |
f31c2a24 | 127 | * @return an array of elementary tokens |
3ac45f10 PC |
128 | */ |
129 | static public function tokenize($term) | |
130 | { | |
131 | $term = mb_strtoupper(replace_accent($term)); | |
132 | $term = str_replace(array('/', ',', '(', ')', '"', '&', '»', '«'), ' ', $term); | |
133 | $tokens = explode(' ', $term); | |
134 | static $not_tokens = array('ET','AND','DE','DES','DU','D\'','OU','L\'','LA','LE','LES','PAR','AU','AUX','EN','SUR','UN','UNE','IN'); | |
135 | foreach ($tokens as &$t) { | |
136 | $t = preg_replace(array('/^-+/','/-+$/'), '', $t); | |
137 | if (substr($t, 1, 1) == '\'' && in_array(substr($t, 0, 2), $not_tokens)) { | |
138 | $t = substr($t, 2); | |
139 | } | |
140 | if (strlen($t) == 1 || in_array($t, $not_tokens)) { | |
141 | $t = false; | |
142 | continue; | |
143 | } | |
144 | } | |
145 | return array_filter($tokens); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Create the INNER JOIN query to restrict search to some job terms | |
150 | * @param $tokens an array of the job terms to look for (LIKE comp) | |
151 | * @param $table_alias the alias or name of the table with a jtid field to restrict | |
f31c2a24 | 152 | * @param $table_field the name of the field to restrict in table_alias, usually jtid |
3ac45f10 PC |
153 | * @return a partial SQL query |
154 | */ | |
f31c2a24 | 155 | static public function token_join_query(array $tokens, $table_alias, $table_field = 'jtid') { |
3ac45f10 PC |
156 | $joins = ''; |
157 | $i = 0; | |
158 | foreach ($tokens as $t) { | |
159 | ++$i; | |
f31c2a24 | 160 | $joins .= ' INNER JOIN profile_job_term_search AS s'.$i.' ON(s'.$i.'.jtid = '.$table_alias.'.'.$table_field.' AND s'.$i.'.search LIKE '.XDB::escape($t).')'; |
3ac45f10 PC |
161 | } |
162 | return $joins; | |
163 | } | |
164 | } | |
165 | ||
166 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
167 | ?> |