]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ExtendedProfile/lib/extendedprofile.php
Merge branch 'nightly' of git.gnu.io:gnu/gnu-social into nightly
[quix0rs-gnu-social.git] / plugins / ExtendedProfile / lib / 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->getID();
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(' ', Profile_tag::getSelfTagsArray($this->profile));
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 getDateValue($name) {
102         $key = strtolower($name);
103         if (array_key_exists($key, $this->fields)) {
104             return $this->fields[$key][0]->date;
105         } else {
106             return null;
107         }
108     }
109
110     // XXX: getPhones, getIms, and getWebsites pretty much do the same thing,
111     //      so refactor.
112     function getPhones()
113     {
114         $phones = (isset($this->fields['phone'])) ? $this->fields['phone'] : null;
115         $pArrays = array();
116
117         if (empty($phones)) {
118             $pArrays[] = array(
119                 // TRANS: Field label for extended profile properties.
120                 'label' => _m('Phone'),
121                 'index' => 0,
122                 'type'  => 'phone',
123                 'vcard' => 'tel',
124                 'rel'   => 'office',
125                 'value' => null
126             );
127         } else {
128             for ($i = 0; $i < sizeof($phones); $i++) {
129                 $pa = array(
130                     // TRANS: Field label for extended profile properties.
131                     'label' => _m('Phone'),
132                     'type'  => 'phone',
133                     'index' => intval($phones[$i]->value_index),
134                     'rel'   => $phones[$i]->rel,
135                     'value' => $phones[$i]->field_value,
136                     'vcard' => 'tel'
137                 );
138
139                $pArrays[] = $pa;
140             }
141         }
142         return $pArrays;
143     }
144
145     function getIms()
146     {
147         $ims = (isset($this->fields['im'])) ? $this->fields['im'] : null;
148         $iArrays = array();
149
150         if (empty($ims)) {
151             $iArrays[] = array(
152                 // TRANS: Field label for extended profile properties (Instant Messaging).
153                 'label' => _m('IM'),
154                 'type' => 'im'
155             );
156         } else {
157             for ($i = 0; $i < sizeof($ims); $i++) {
158                 $ia = array(
159                     // TRANS: Field label for extended profile properties (Instant Messaging).
160                     'label' => _m('IM'),
161                     'type'  => 'im',
162                     'index' => intval($ims[$i]->value_index),
163                     'rel'   => $ims[$i]->rel,
164                     'value' => $ims[$i]->field_value,
165                 );
166
167                 $iArrays[] = $ia;
168             }
169         }
170         return $iArrays;
171     }
172
173     function getWebsites()
174     {
175         $sites = (isset($this->fields['website'])) ? $this->fields['website'] : null;
176         $wArrays = array();
177
178         if (empty($sites)) {
179             $wArrays[] = array(
180                 // TRANS: Field label for extended profile properties.
181                 'label' => _m('Website'),
182                 'type' => 'website'
183             );
184         } else {
185             for ($i = 0; $i < sizeof($sites); $i++) {
186                 $wa = array(
187                     // TRANS: Field label for extended profile properties.
188                     'label' => _m('Website'),
189                     'type'  => 'website',
190                     'index' => intval($sites[$i]->value_index),
191                     'rel'   => $sites[$i]->rel,
192                     'value' => $sites[$i]->field_value,
193                 );
194
195                 $wArrays[] = $wa;
196             }
197         }
198         return $wArrays;
199     }
200
201     function getExperiences()
202     {
203         $companies = (isset($this->fields['company'])) ? $this->fields['company'] : null;
204         $start = (isset($this->fields['start'])) ? $this->fields['start'] : null;
205         $end   = (isset($this->fields['end'])) ? $this->fields['end'] : null;
206
207         $eArrays = array();
208
209         if (empty($companies)) {
210             $eArrays[] = array(
211                 // TRANS: Field label for extended profile properties.
212                 'label'   => _m('Employer'),
213                 'type'    => 'experience',
214                 'company' => null,
215                 'start'   => null,
216                 'end'     => null,
217                 'current' => false,
218                 'index'   => 0
219             );
220         } else {
221             for ($i = 0; $i < sizeof($companies); $i++) {
222                 $ea = array(
223                     // TRANS: Field label for extended profile properties.
224                     'label'   => _m('Employer'),
225                     'type'    => 'experience',
226                     'company' => $companies[$i]->field_value,
227                     'index'   => intval($companies[$i]->value_index),
228                     'current' => $end[$i]->rel,
229                     'start'   => $start[$i]->date,
230                     'end'     => $end[$i]->date
231                 );
232                $eArrays[] = $ea;
233             }
234         }
235         return $eArrays;
236     }
237
238     function getEducation()
239     {
240         $schools = (isset($this->fields['school'])) ? $this->fields['school'] : null;
241         $degrees = (isset($this->fields['degree'])) ? $this->fields['degree'] : null;
242         $descs = (isset($this->fields['degree_descr'])) ? $this->fields['degree_descr'] : null;
243         $start = (isset($this->fields['school_start'])) ? $this->fields['school_start'] : null;
244         $end = (isset($this->fields['school_end'])) ? $this->fields['school_end'] : null;
245         $iArrays = array();
246
247         if (empty($schools)) {
248             $iArrays[] = array(
249                 'type' => 'education',
250                 // TRANS: Field label for extended profile properties.
251                 'label' => _m('Institution'),
252                 'school' => null,
253                 'degree' => null,
254                 'description' => null,
255                 'start' => null,
256                 'end' => null,
257                 'index' => 0
258             );
259         } else {
260             for ($i = 0; $i < sizeof($schools); $i++) {
261                 $ia = array(
262                     'type'    => 'education',
263                     // TRANS: Field label for extended profile properties.
264                     'label'   => _m('Institution'),
265                     'school'  => $schools[$i]->field_value,
266                     'degree'  => isset($degrees[$i]->field_value) ? $degrees[$i]->field_value : null,
267                     'description' => isset($descs[$i]->field_value) ? $descs[$i]->field_value : null,
268                     'index'   => intval($schools[$i]->value_index),
269                     'start'   => $start[$i]->date,
270                     'end'     => $end[$i]->date
271                 );
272                $iArrays[] = $ia;
273             }
274         }
275
276         return $iArrays;
277     }
278
279     /**
280      *  Return all the sections of the extended profile
281      *
282      * @return array the big list of sections and fields
283      */
284     function getSections()
285     {
286         return array(
287             'basic' => array(
288                 // TRANS: Field label for extended profile properties.
289                 'label' => _m('Personal'),
290                 'fields' => array(
291                     'fullname' => array(
292                         // TRANS: Field label for extended profile properties.
293                         'label' => _m('Full name'),
294                         'profile' => 'fullname',
295                         'vcard' => 'fn',
296                     ),
297                     'title' => array(
298                         // TRANS: Field label for extended profile properties.
299                         'label' => _m('Title'),
300                         'vcard' => 'title',
301                     ),
302                     'manager' => array(
303                         // TRANS: Field label for extended profile properties.
304                         'label' => _m('Manager'),
305                         'type' => 'person',
306                         'vcard' => 'x-manager',
307                     ),
308                     'location' => array(
309                         // TRANS: Field label for extended profile properties.
310                         'label' => _m('Location'),
311                         'profile' => 'location'
312                     ),
313                     'bio' => array(
314                         // TRANS: Field label for extended profile properties.
315                         'label' => _m('Bio'),
316                         'type' => 'textarea',
317                         'profile' => 'bio',
318                     ),
319                     'tags' => array(
320                         // TRANS: Field label for extended profile properties.
321                         'label' => _m('Tags'),
322                         'type' => 'tags',
323                         'profile' => 'tags',
324                     ),
325                 ),
326             ),
327             'contact' => array(
328                 // TRANS: Field label for extended profile properties.
329                 'label' => _m('Contact'),
330                 'fields' => array(
331                     'phone'   => $this->getPhones(),
332                     'im'      => $this->getIms(),
333                     'website' => $this->getWebsites()
334                 ),
335             ),
336             'personal' => array(
337                 // TRANS: Field label for extended profile properties.
338                 'label' => _m('Personal'),
339                 'fields' => array(
340                     'birthday' => array(
341                         // TRANS: Field label for extended profile properties.
342                         'label' => _m('Birthday'),
343                         'type' => 'date',
344                         'vcard' => 'bday',
345                     ),
346                     'spouse' => array(
347                         // TRANS: Field label for extended profile properties.
348                         'label' => _m('Spouse\'s name'),
349                         'vcard' => 'x-spouse',
350                     ),
351                     'kids' => array(
352                         // TRANS: Field label for extended profile properties.
353                         'label' => _m('Kids\' names')
354                     ),
355                 ),
356             ),
357             'experience' => array(
358                 // TRANS: Field label for extended profile properties.
359                 'label' => _m('Work experience'),
360                 'fields' => array(
361                     'experience' => $this->getExperiences()
362                 ),
363             ),
364             'education' => array(
365                 // TRANS: Field label for extended profile properties.
366                 'label' => _m('Education'),
367                 'fields' => array(
368                     'education' => $this->getEducation()
369                 ),
370             ),
371         );
372     }
373 }