]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ExtendedProfile/extendedprofile.php
Extended profile - make education entries save
[quix0rs-gnu-social.git] / plugins / ExtendedProfile / extendedprofile.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 /**
25  * Class to represent extended profile data
26  */
27 class ExtendedProfile
28 {
29     protected $fields;
30
31     /**
32      * Constructor
33      *
34      * @param Profile $profile
35      */
36     function __construct(Profile $profile)
37     {
38         $this->profile  = $profile;
39         $this->user     = $profile->getUser();
40         $this->fields   = $this->loadFields();
41         $this->sections = $this->getSections();
42         //common_debug(var_export($this->sections, true));
43
44         //common_debug(var_export($this->fields, true));
45     }
46
47     /**
48      * Load extended profile fields
49      *
50      * @return array $fields the list of fields
51      */
52     function loadFields()
53     {
54         $detail = new Profile_detail();
55         $detail->profile_id = $this->profile->id;
56         $detail->find();
57
58         $fields = array();
59
60         while ($detail->fetch()) {
61             $fields[$detail->field_name][] = clone($detail);
62         }
63
64         return $fields;
65     }
66
67     /**
68      * Get a the self-tags associated with this profile
69      *
70      * @return string the concatenated string of tags
71      */
72     function getTags()
73     {
74         return implode(' ', $this->user->getSelfTags());
75     }
76
77     /**
78      * Return a simple string value. Checks for fields that should
79      * be stored in the regular profile and returns values from it
80      * if appropriate.
81      *
82      * @param string $name name of the detail field to get the
83      *                     value from
84      *
85      * @return string the value
86      */
87     function getTextValue($name)
88     {
89         $key           = strtolower($name);
90         $profileFields = array('fullname', 'location', 'bio');
91
92         if (in_array($key, $profileFields)) {
93             return $this->profile->$name;
94         } else if (array_key_exists($key, $this->fields)) {
95             return $this->fields[$key][0]->field_value;
96         } else {
97             return null;
98         }
99     }
100
101     function getPhones()
102     {
103         $phones = (isset($this->fields['phone'])) ? $this->fields['phone'] : null;
104         $pArrays = array();
105
106         if (empty($phones)) {
107             $pArrays[] = array(
108                 'label' => _m('Phone'),
109                 'index' => 0,
110                 'type'  => 'phone',
111                 'vcard' => 'tel',
112                 'rel'   => 'office',
113                 'value' => null
114             );
115         } else {
116             for ($i = 0; $i < sizeof($phones); $i++) {
117                 $pa = array(
118                     'label' => _m('Phone'),
119                     'type'  => 'phone',
120                     'index' => intval($phones[$i]->value_index),
121                     'rel'   => $phones[$i]->rel,
122                     'value' => $phones[$i]->field_value,
123                     'vcard' => 'tel'
124                 );
125
126                $pArrays[] = $pa;
127             }
128         }
129         return $pArrays;
130     }
131
132     function getExperiences()
133     {
134         $companies = (isset($this->fields['company'])) ? $this->fields['company'] : null;
135         $start = (isset($this->fields['start'])) ? $this->fields['start'] : null;
136         $end   = (isset($this->fields['end'])) ? $this->fields['end'] : null;
137
138         $eArrays = array();
139
140         if (empty($companies)) {
141             $eArrays[] = array(
142                 'label'   => _m('Employer'),
143                 'type'    => 'experience',
144                 'company' => null,
145                 'start'   => null,
146                 'end'     => null,
147                 'current' => false,
148                 'index'   => 0
149             );
150         } else {
151             for ($i = 0; $i < sizeof($companies); $i++) {
152                 $ea = array(
153                     'label'   => _m('Employer'),
154                     'type'    => 'experience',
155                     'company' => $companies[$i]->field_value,
156                     'index'   => intval($companies[$i]->value_index),
157                     'current' => $end[$i]->rel,
158                     'start'   => $start[$i]->date,
159                     'end'     => $end[$i]->date
160                 );
161                $eArrays[] = $ea;
162             }
163         }
164         return $eArrays;
165     }
166
167     function getEducation()
168     {
169         $schools = (isset($this->fields['school'])) ? $this->fields['school'] : null;
170         $degrees = (isset($this->fields['degree'])) ? $this->fields['degree'] : null;
171         $descs = (isset($this->fields['degree_descr'])) ? $this->fields['degree_descr'] : null;
172         $start = (isset($this->fields['school_start'])) ? $this->fields['school_start'] : null;
173         $end = (isset($this->fields['school_end'])) ? $this->fields['school_end'] : null;
174         $iArrays = array();
175
176         if (empty($schools)) {
177             $iArrays[] = array(
178                 'type' => 'education',
179                 'label' => _m('Institution'),
180                 'school' => null,
181                 'degree' => null,
182                 'description' => null,
183                 'start' => null,
184                 'end' => null,
185                 'index' => 0
186             );
187         } else {
188             for ($i = 0; $i < sizeof($schools); $i++) {
189                 $ia = array(
190                     'type'    => 'education',
191                     'label'   => _m('Institution'),
192                     'school'  => $schools[$i]->field_value,
193                     'degree'  => isset($degrees[$i]->field_value) ? $degrees[$i]->field_value : null,
194                     'description' => isset($descs[$i]->field_value) ? $descs[$i]->field_value : null,
195                     'index'   => intval($schools[$i]->value_index),
196                     'start'   => $start[$i]->date,
197                     'end'     => $end[$i]->date
198                 );
199                $iArrays[] = $ia;
200             }
201         }
202
203         return $iArrays;
204     }
205
206     /**
207      *  Return all the sections of the extended profile
208      *
209      * @return array the big list of sections and fields
210      */
211     function getSections()
212     {
213         return array(
214             'basic' => array(
215                 'label' => _m('Personal'),
216                 'fields' => array(
217                     'fullname' => array(
218                         'label' => _m('Full name'),
219                         'profile' => 'fullname',
220                         'vcard' => 'fn',
221                     ),
222                     'title' => array(
223                         'label' => _m('Title'),
224                         'vcard' => 'title',
225                     ),
226                     'manager' => array(
227                         'label' => _m('Manager'),
228                         'type' => 'person',
229                         'vcard' => 'x-manager',
230                     ),
231                     'location' => array(
232                         'label' => _m('Location'),
233                         'profile' => 'location'
234                     ),
235                     'bio' => array(
236                         'label' => _m('Bio'),
237                         'type' => 'textarea',
238                         'profile' => 'bio',
239                     ),
240                     'tags' => array(
241                         'label' => _m('Tags'),
242                         'type' => 'tags',
243                         'profile' => 'tags',
244                     ),
245                 ),
246             ),
247             'contact' => array(
248                 'label' => _m('Contact'),
249                 'fields' => array(
250                     'phone' => $this->getPhones(),
251                     'im' => array(
252                         'label' => _m('IM'),
253                         'type' => 'im',
254                         'multi' => true,
255                     ),
256                     'website' => array(
257                         'label' => _m('Websites'),
258                         'type' => 'website',
259                         'multi' => true,
260                     ),
261                 ),
262             ),
263             'personal' => array(
264                 'label' => _m('Personal'),
265                 'fields' => array(
266                     'birthday' => array(
267                         'label' => _m('Birthday'),
268                         'type' => 'date',
269                         'vcard' => 'bday',
270                     ),
271                     'spouse' => array(
272                         'label' => _m('Spouse\'s name'),
273                         'vcard' => 'x-spouse',
274                     ),
275                     'kids' => array(
276                         'label' => _m('Kids\' names')
277                     ),
278                 ),
279             ),
280             'experience' => array(
281                 'label' => _m('Work experience'),
282                 'fields' => array(
283                     'experience' => $this->getExperiences()
284                 ),
285             ),
286             'education' => array(
287                 'label' => _m('Education'),
288                 'fields' => array(
289                     'education' => $this->getEducation()
290                 ),
291             ),
292         );
293     }
294 }