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