| 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 | /** Terms associated to profile mentoring */ |
| 23 | class ProfileSettingTerms implements ProfileSetting |
| 24 | { |
| 25 | public function value(ProfilePage &$page, $field, $value, &$success) |
| 26 | { |
| 27 | $success = true; |
| 28 | if (is_null($value)) { |
| 29 | $value = array(); |
| 30 | $res = XDB::query('SELECT e.jtid, e.full_name |
| 31 | FROM profile_mentor_term AS m |
| 32 | INNER JOIN profile_job_term_enum AS e ON (m.jtid = e.jtid) |
| 33 | WHERE m.pid = {?}', |
| 34 | $page->pid()); |
| 35 | $value = $res->fetchAllAssoc(); |
| 36 | } elseif (!is_array($value)) { |
| 37 | $value = array(); |
| 38 | } elseif (count($value) > 20) { |
| 39 | Platal::page()->trigError("Le nombre de mots clefs d'expertise est limité à 20."); |
| 40 | $success = false; |
| 41 | } else { |
| 42 | $missing_full_names = array(); |
| 43 | foreach ($value as &$term) if (empty($term['full_name'])) { |
| 44 | $missing_full_names[] = $term['jtid']; |
| 45 | } |
| 46 | if (count($missing_full_names)) { |
| 47 | $res = XDB::query('SELECT jtid, full_name |
| 48 | FROM profile_job_term_enum |
| 49 | WHERE jtid IN {?}', |
| 50 | $missing_full_names); |
| 51 | $term_id_to_name = $res->fetchAllAssoc('jtid', false); |
| 52 | foreach ($value as &$term) { |
| 53 | if (empty($term['full_name'])) { |
| 54 | $term['full_name'] = $term_id_to_name[$term['jtid']]; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | ksort($value); |
| 60 | foreach ($value as &$sss) { |
| 61 | ksort($sss); |
| 62 | } |
| 63 | return $value; |
| 64 | } |
| 65 | |
| 66 | public function save(ProfilePage &$page, $field, $value) |
| 67 | { |
| 68 | |
| 69 | XDB::execute("DELETE FROM profile_mentor_term |
| 70 | WHERE pid = {?}", |
| 71 | $page->pid()); |
| 72 | if (!count($value)) { |
| 73 | return; |
| 74 | } |
| 75 | $mentor_term_values = array(); |
| 76 | foreach ($value as &$term) { |
| 77 | $mentor_term_values[] = '('.XDB::escape($page->pid()).', '.XDB::escape($term['jtid']).')'; |
| 78 | } |
| 79 | XDB::execute('INSERT INTO profile_mentor_term (pid, jtid) |
| 80 | VALUES '.implode(',', $mentor_term_values)); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | public function getText($value) { |
| 85 | $terms = array(); |
| 86 | foreach ($value as &$term) { |
| 87 | $terms[] = $term['full_name']; |
| 88 | } |
| 89 | return implode(', ', $terms); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | class ProfileSettingCountry implements ProfileSetting |
| 94 | { |
| 95 | public function value(ProfilePage &$page, $field, $value, &$success) |
| 96 | { |
| 97 | $success = true; |
| 98 | if (is_null($value)) { |
| 99 | $value = array(); |
| 100 | $res = XDB::iterRow("SELECT m.country, gc.country |
| 101 | FROM profile_mentor_country AS m |
| 102 | INNER JOIN geoloc_countries AS gc ON (m.country = gc.iso_3166_1_a2) |
| 103 | WHERE m.pid = {?}", |
| 104 | $page->pid()); |
| 105 | while (list($id, $name) = $res->next()) { |
| 106 | $value[$id] = $name; |
| 107 | } |
| 108 | } else if (!is_array($value)) { |
| 109 | $value = array(); |
| 110 | } else if (count($value) > 10) { |
| 111 | Platal::page()->trigError("Le nombre de secteurs d'expertise est limité à 10"); |
| 112 | $success = false; |
| 113 | } |
| 114 | ksort($value); |
| 115 | return $value; |
| 116 | } |
| 117 | |
| 118 | public function save(ProfilePage &$page, $field, $value) |
| 119 | { |
| 120 | XDB::execute("DELETE FROM profile_mentor_country |
| 121 | WHERE pid = {?}", |
| 122 | $page->pid()); |
| 123 | foreach ($value as $id=>&$name) { |
| 124 | XDB::execute("INSERT INTO profile_mentor_country (pid, country) |
| 125 | VALUES ({?}, {?})", |
| 126 | $page->pid(), $id); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | public function getText($value) { |
| 131 | return implode(', ', $value); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | class ProfilePageMentor extends ProfilePage |
| 137 | { |
| 138 | protected $pg_template = 'profile/mentor.tpl'; |
| 139 | |
| 140 | public function __construct(PlWizard &$wiz) |
| 141 | { |
| 142 | parent::__construct($wiz); |
| 143 | $this->settings['expertise'] = null; |
| 144 | $this->settings['terms'] = new ProfileSettingTerms(); |
| 145 | $this->settings['countries'] = new ProfileSettingCountry(); |
| 146 | } |
| 147 | |
| 148 | protected function _fetchData() |
| 149 | { |
| 150 | $res = XDB::query("SELECT expertise |
| 151 | FROM profile_mentor |
| 152 | WHERE pid = {?}", |
| 153 | $this->pid()); |
| 154 | $this->values['expertise'] = $res->fetchOneCell(); |
| 155 | } |
| 156 | |
| 157 | protected function _saveData() |
| 158 | { |
| 159 | if ($this->changed['expertise']) { |
| 160 | $expertise = trim($this->values['expertise']); |
| 161 | if (empty($expertise)) { |
| 162 | XDB::execute("DELETE FROM profile_mentor |
| 163 | WHERE pid = {?}", |
| 164 | $this->pid()); |
| 165 | $this->values['expertise'] = null; |
| 166 | } else { |
| 167 | XDB::execute('INSERT INTO profile_mentor (pid, expertise) |
| 168 | VALUES ({?}, {?}) |
| 169 | ON DUPLICATE KEY UPDATE expertise = VALUES(expertise)', |
| 170 | $this->pid(), $expertise); |
| 171 | $this->values['expertise'] = $expertise; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public function _prepare(PlPage &$page, $id) |
| 177 | { |
| 178 | $page->assign('countryList', XDB::iterator("SELECT iso_3166_1_a2, country |
| 179 | FROM geoloc_countries |
| 180 | ORDER BY country")); |
| 181 | $page->assign('hrpid', $this->profile->hrpid); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 186 | ?> |