Merge remote branch 'origin/platal-1.0.0' into platal-1.0.1
[platal.git] / classes / jobterms.php
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.', IF(rf.jtid_1 IS NULL, 1, 0) AS leaf
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 LEFT JOIN profile_job_term_relation AS rf ON (rf.jtid_1 = e.jtid AND rf.computed = "original")
52 WHERE r.jtid_1 = {?} AND r.computed = "original"
53 GROUP BY e.jtid
54 ORDER BY e.name',
55 $parent_jtid);
56 }
57
58 /**
59 * Display a JSon page containing the sub-branches of a branch in the job terms tree.
60 * @param $page the Platal page
61 * @param $filter filter helps to display only jobterms that are contained in jobs or in mentors
62 *
63 * @param Env::i('jtid') job term id of the parent branch, if none trunk will be used
64 * @param Env::v('attrfunc') the name of a javascript function that will be called when a branch
65 * is chosen
66 * @param Env::v('treeid') tree id that will be given as first argument of attrfunc function
67 * the second argument will be the chosen job term id and the third one the chosen job's full name.
68 */
69 static public function ajaxGetBranch(&$page, $filter = self::ALL) {
70 pl_content_headers('application/json');
71 $page->changeTpl('include/jobterms.branch.tpl', NO_SKIN);
72 $subTerms = self::getSubTerms(Env::v('jtid'), $filter);
73 $page->assign('subTerms', $subTerms);
74 switch ($filter) {
75 case self::ONLY_JOBS:
76 $page->assign('filter', 'camarade');
77 break;
78 case self::ONLY_MENTORS:
79 $page->assign('filter', 'mentor');
80 break;
81 }
82 $page->assign('jtid', Env::v('jtid'));
83 $page->assign('attrfunc', Env::v('attrfunc'));
84 $page->assign('treeid', Env::v('treeid'));
85 }
86
87 static public function jsAddTree($platalpage, $domElement = '.term_tree', $treeid = '', $attrfunc = '') {
88 return '$("'.addslashes($domElement).'").jstree({
89 "core" : {"strings":{"loading":"Chargement ..."}},
90 "plugins" : ["themes","json_data"],
91 "themes" : { "url" : platal_baseurl + "css/jstree.css" },
92 "json_data" : { "ajax" : {
93 "url" : platal_baseurl + "'.addslashes($platalpage).'",
94 "data" : function(nod) {
95 var jtid = 0;
96 if (nod != -1) {
97 jtid = nod.attr("id").replace(/^.*_([0-9]+)$/, "$1");
98 }
99 return { "jtid" : jtid, "treeid" : "'.addslashes($treeid).'", "attrfunc" : "'.addslashes($attrfunc).'" }
100 }
101 }} });';
102 }
103
104 /**
105 * Extract search token from term
106 * @param $term a utf-8 string that can contain any char
107 * @param an array of elementary tokens
108 */
109 static public function tokenize($term)
110 {
111 $term = mb_strtoupper(replace_accent($term));
112 $term = str_replace(array('/', ',', '(', ')', '"', '&', '»', '«'), ' ', $term);
113 $tokens = explode(' ', $term);
114 static $not_tokens = array('ET','AND','DE','DES','DU','D\'','OU','L\'','LA','LE','LES','PAR','AU','AUX','EN','SUR','UN','UNE','IN');
115 foreach ($tokens as &$t) {
116 $t = preg_replace(array('/^-+/','/-+$/'), '', $t);
117 if (substr($t, 1, 1) == '\'' && in_array(substr($t, 0, 2), $not_tokens)) {
118 $t = substr($t, 2);
119 }
120 if (strlen($t) == 1 || in_array($t, $not_tokens)) {
121 $t = false;
122 continue;
123 }
124 }
125 return array_filter($tokens);
126 }
127
128 /**
129 * Create the INNER JOIN query to restrict search to some job terms
130 * @param $tokens an array of the job terms to look for (LIKE comp)
131 * @param $table_alias the alias or name of the table with a jtid field to restrict
132 * @return a partial SQL query
133 */
134 static public function token_join_query(array $tokens, $table_alias) {
135 $joins = '';
136 $i = 0;
137 foreach ($tokens as $t) {
138 ++$i;
139 $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).')';
140 }
141 return $joins;
142 }
143 }
144
145 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
146 ?>