]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ExtendedProfile/lib/extendedprofilewidget.php
Merge branch 'bashrc/remove_google_references' into 'master'
[quix0rs-gnu-social.git] / plugins / ExtendedProfile / lib / extendedprofilewidget.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 for outputting a widget to display or edit
26  * extended profiles
27  */
28 class ExtendedProfileWidget extends Form
29 {
30     const EDITABLE = true;
31
32     /**
33      * The parent profile
34      *
35      * @var Profile
36      */
37     protected $profile;
38
39     /**
40      * The extended profile
41      *
42      * @var Extended_profile
43      */
44     protected $ext;
45
46     /**
47      * Constructor
48      *
49      * @param XMLOutputter  $out
50      * @param Profile       $profile
51      * @param boolean       $editable
52      */
53     public function __construct(XMLOutputter $out=null, Profile $profile=null, $editable=false)
54     {
55         parent::__construct($out);
56
57         $this->profile = $profile;
58         $this->ext = new ExtendedProfile($this->profile);
59
60         $this->editable = $editable;
61     }
62
63     /**
64      * Show the extended profile, or the edit form
65      */
66     public function show()
67     {
68         if ($this->editable) {
69             parent::show();
70         } else {
71             $this->showSections();
72         }
73     }
74
75     /**
76      * Show form data
77      */
78     public function formData()
79     {
80         // For JQuery UI modal dialog
81         $this->out->elementStart(
82             'div',
83             // TRANS: Title for extended profile entry deletion dialog.
84             array('id' => 'confirm-dialog', 'title' => _m('Confirmation Required'))
85         );
86         // TRANS: Confirmation text for extended profile entry deletion dialog.
87         $this->out->text(_m('Really delete this entry?'));
88         $this->out->elementEnd('div');
89         $this->showSections();
90     }
91
92     /**
93      * Show each section of the extended profile
94      */
95     public function showSections()
96     {
97         $sections = $this->ext->getSections();
98         foreach ($sections as $name => $section) {
99             $this->showExtendedProfileSection($name, $section);
100         }
101     }
102
103     /**
104      * Show an extended profile section
105      *
106      * @param string $name      name of the section
107      * @param array  $section   array of fields for the section
108      */
109     protected function showExtendedProfileSection($name, $section)
110     {
111         $this->out->element('h3', null, $section['label']);
112         $this->out->elementStart('table', array('class' => 'extended-profile'));
113
114         foreach ($section['fields'] as $fieldName => $field) {
115
116             switch($fieldName) {
117             case 'phone':
118             case 'im':
119             case 'website':
120             case 'experience':
121             case 'education':
122                 $this->showMultiple($fieldName, $field);
123                 break;
124             default:
125                 $this->showExtendedProfileField($fieldName, $field);
126             }
127         }
128         $this->out->elementEnd('table');
129     }
130
131     /**
132      * Show an extended profile field
133      *
134      * @param string $name  name of the field
135      * @param array  $field set of key/value pairs for the field
136      */
137     protected function showExtendedProfileField($name, $field)
138     {
139         $this->out->elementStart('tr');
140
141         $this->out->element('th', str_replace(' ','_',strtolower($field['label'])), $field['label']);
142
143         $this->out->elementStart('td');
144         if ($this->editable) {
145             $this->showEditableField($name, $field);
146         } else {
147             $this->showFieldValue($name, $field);
148         }
149         $this->out->elementEnd('td');
150
151         $this->out->elementEnd('tr');
152     }
153
154     protected function showMultiple($name, $fields) {
155         foreach ($fields as $field) {
156             $this->showExtendedProfileField($name, $field);
157         }
158     }
159
160     // XXX: showPhone, showIm and showWebsite all work the same, so
161     //      combine
162     protected function showPhone($name, $field)
163     {
164         $this->out->elementStart('div', array('class' => 'phone-display'));
165         if (!empty($field['value'])) {
166             $this->out->text($field['value']);
167             if (!empty($field['rel'])) {
168                // TRANS: Value between parentheses (phone number, website, or IM address).
169                $outtext = sprintf(_m('(%s)'),$field['rel']);
170                $this->out->text(' '.$outtext);
171             }
172         }
173         $this->out->elementEnd('div');
174     }
175
176     protected function showIm($name, $field)
177     {
178         $this->out->elementStart('div', array('class' => 'im-display'));
179         $this->out->text($field['value']);
180         if (!empty($field['rel'])) {
181             // TRANS: Value between parentheses (phone number, website, or IM address).
182             $outtext = sprintf(_m('(%s)'),$field['rel']);
183             $this->out->text(' '.$outtext);
184         }
185         $this->out->elementEnd('div');
186     }
187
188     protected function showWebsite($name, $field)
189     {
190         $this->out->elementStart('div', array('class' => 'website-display'));
191
192         $url = $field['value'];
193
194         $this->out->element(
195             "a",
196             array(
197                 'href'   => $url,
198                 'class'  => 'extended-profile-link',
199                 'target' => "_blank"
200             ),
201             $url
202         );
203
204         if (!empty($field['rel'])) {
205             // TRANS: Value between parentheses (phone number, website, or IM address).
206             $outtext = sprintf(_m('(%s)'),$field['rel']);
207             $this->out->text(' '.$outtext);
208         }
209         $this->out->elementEnd('div');
210     }
211
212     protected function showEditableIm($name, $field)
213     {
214         $index = isset($field['index']) ? $field['index'] : 0;
215         $id    = "extprofile-$name-$index";
216         $rel   = $id . '-rel';
217         $this->out->elementStart(
218             'div', array(
219                 'id' => $id . '-edit',
220                 'class' => 'im-item'
221             )
222         );
223         $this->out->input(
224             $id,
225             null,
226             isset($field['value']) ? $field['value'] : null
227         );
228         $this->out->dropdown(
229             $id . '-rel',
230             'Type',
231             array(
232                 'jabber' => 'Jabber',
233                 'gtalk'  => 'GTalk',
234                 'aim'    => 'AIM',
235                 'yahoo'  => 'Yahoo! Messenger',
236                 'msn'    => 'MSN',
237                 'skype'  => 'Skype',
238                 'other'  => 'Other'
239             ),
240             null,
241             false,
242             isset($field['rel']) ? $field['rel'] : null
243         );
244
245         $this->showMultiControls();
246         $this->out->elementEnd('div');
247     }
248
249     protected function showEditablePhone($name, $field)
250     {
251         $index = isset($field['index']) ? $field['index'] : 0;
252         $id    = "extprofile-$name-$index";
253         $rel   = $id . '-rel';
254         $this->out->elementStart(
255             'div', array(
256                 'id' => $id . '-edit',
257                 'class' => 'phone-item'
258             )
259         );
260         $this->out->input(
261             $id,
262             null,
263             isset($field['value']) ? $field['value'] : null
264         );
265         $this->out->dropdown(
266             $id . '-rel',
267             'Type',
268             array(
269                 'office' => 'Office',
270                 'mobile' => 'Mobile',
271                 'home'   => 'Home',
272                 'pager'  => 'Pager',
273                 'other'  => 'Other'
274             ),
275             null,
276             false,
277             isset($field['rel']) ? $field['rel'] : null
278         );
279
280         $this->showMultiControls();
281         $this->out->elementEnd('div');
282     }
283
284     protected function showEditableWebsite($name, $field)
285     {
286         $index = isset($field['index']) ? $field['index'] : 0;
287         $id    = "extprofile-$name-$index";
288         $rel   = $id . '-rel';
289         $this->out->elementStart(
290             'div', array(
291                 'id' => $id . '-edit',
292                 'class' => 'website-item'
293             )
294         );
295         $this->out->input(
296             $id,
297             null,
298             isset($field['value']) ? $field['value'] : null
299         );
300         $this->out->dropdown(
301             $id . '-rel',
302             'Type',
303             array(
304                 'blog'     => 'Blog',
305                 'homepage' => 'Homepage',
306                 'facebook' => 'Facebook',
307                 'linkedin' => 'LinkedIn',
308                 'flickr'   => 'Flickr',
309                 'other'    => 'Other',
310                 'twitter'  => 'Twitter'
311             ),
312             null,
313             false,
314             isset($field['rel']) ? $field['rel'] : null
315         );
316
317         $this->showMultiControls();
318         $this->out->elementEnd('div');
319     }
320
321     protected function showExperience($name, $field)
322     {
323         $this->out->elementStart('div', 'experience-item');
324         // TRANS: Field label in experience area of extended profile.
325         $this->out->element('div', 'label', _m('Company'));
326
327         if (!empty($field['company'])) {
328             $this->out->element('div', 'field', $field['company']);
329
330             // TRANS: Field label in extended profile (when did one start a position or education).
331             $this->out->element('div', 'label', _m('Start'));
332             $this->out->element(
333                 'div',
334                 array('class' => 'field date'),
335                 date('j M Y', strtotime($field['start'])
336                 )
337             );
338             // TRANS: Field label in extended profile (when did one end a position or education).
339             $this->out->element('div', 'label', _m('End'));
340             $this->out->element(
341                 'div',
342                 array('class' => 'field date'),
343                 date('j M Y', strtotime($field['end'])
344                 )
345             );
346
347             if ($field['current']) {
348                 $this->out->element(
349                     'div',
350                     array('class' => 'field current'),
351                     // TRANS: Field value in experience area of extended profile (one still holds a position).
352                     _m('(Current)')
353                 );
354             }
355         }
356         $this->out->elementEnd('div');
357     }
358
359     protected function showEditableExperience($name, $field)
360     {
361         $index = isset($field['index']) ? $field['index'] : 0;
362         $id    = "extprofile-$name-$index";
363         $this->out->elementStart(
364             'div', array(
365                 'id' => $id . '-edit',
366                 'class' => 'experience-item'
367             )
368         );
369
370         // TRANS: Field label in experience edit area of extended profile (which company does one work for).
371         $this->out->element('div', 'label', _m('Company'));
372         $this->out->input(
373             $id,
374             null,
375             isset($field['company']) ? $field['company'] : null
376         );
377
378         // TRANS: Field label in extended profile (when did one start a position or education).
379         $this->out->element('div', 'label', _m('Start'));
380         $this->out->input(
381             $id . '-start',
382             null,
383             isset($field['start']) ? date('j M Y', strtotime($field['start'])) : null
384         );
385
386             // TRANS: Field label in extended profile (when did one end a position or education).
387         $this->out->element('div', 'label', _m('End'));
388
389         $this->out->input(
390             $id . '-end',
391             null,
392             isset($field['end']) ? date('j M Y', strtotime($field['end'])) : null
393         );
394         $this->out->hidden(
395             $id . '-current',
396             'false'
397         );
398         $this->out->elementStart('div', 'current-checkbox');
399         $this->out->checkbox(
400             $id . '-current',
401             // TRANS: Checkbox label in experience edit area of extended profile (one still works at a company).
402             _m('Current'),
403             $field['current']
404         );
405         $this->out->elementEnd('div');
406
407         $this->showMultiControls();
408         $this->out->elementEnd('div');
409     }
410
411     protected function showEducation($name, $field)
412     {
413         $this->out->elementStart('div', 'education-item');
414         // TRANS: Field label in education area of extended profile.
415         $this->out->element('div', 'label', _m('Institution'));
416         if (!empty($field['school'])) {
417             $this->out->element('div', 'field', $field['school']);
418             // TRANS: Field label in extended profile for specifying an academic degree.
419             $this->out->element('div', 'label', _m('Degree'));
420             $this->out->element('div', 'field', $field['degree']);
421             // TRANS: Field label in education area of extended profile.
422             $this->out->element('div', 'label', _m('Description'));
423             $this->out->element('div', 'field', $field['description']);
424             // TRANS: Field label in extended profile (when did one start a position or education).
425             $this->out->element('div', 'label', _m('Start'));
426             $this->out->element(
427                 'div',
428                 array('class' => 'field date'),
429                 date('j M Y', strtotime($field['start'])
430                 )
431             );
432             // TRANS: Field label in extended profile (when did one end a position or education).
433             $this->out->element('div', 'label', _m('End'));
434             $this->out->element(
435                 'div',
436                 array('class' => 'field date'),
437                 date('j M Y', strtotime($field['end'])
438                 )
439             );
440         }
441         $this->out->elementEnd('div');
442     }
443
444     protected function showEditableEducation($name, $field)
445     {
446         $index = isset($field['index']) ? $field['index'] : 0;
447         $id    = "extprofile-$name-$index";
448         $this->out->elementStart(
449             'div', array(
450                 'id' => $id . '-edit',
451                 'class' => 'education-item'
452             )
453         );
454         // TRANS: Field label in education edit area of extended profile.
455         $this->out->element('div', 'label', _m('Institution'));
456         $this->out->input(
457             $id,
458             null,
459             isset($field['school']) ? $field['school'] : null
460         );
461
462         // TRANS: Field label in extended profile for specifying an academic degree.
463         $this->out->element('div', 'label', _m('Degree'));
464         $this->out->input(
465             $id . '-degree',
466             null,
467             isset($field['degree']) ? $field['degree'] : null
468         );
469
470         // TRANS: Field label in education edit area of extended profile.
471         $this->out->element('div', 'label', _m('Description'));
472
473         $this->out->textarea(
474             $id . '-description',
475             null,
476             isset($field['description']) ? $field['description'] : null
477         );
478
479         // TRANS: Field label in extended profile (when did one start a position or education).
480         $this->out->element('div', 'label', _m('Start'));
481         $this->out->input(
482             $id . '-start',
483             null,
484             // @todo FIXME: does date format need i18n? If so, should probly be dealt with in core.
485             isset($field['start']) ? date('j M Y', strtotime($field['start'])) : null
486         );
487
488         // TRANS: Field label in extended profile (when did one end a position or education).
489         $this->out->element('div', 'label', _m('End'));
490         $this->out->input(
491             $id . '-end',
492             null,
493             // @todo FIXME: does date format need i18n? If so, should probly be dealt with in core.
494             isset($field['end']) ? date('j M Y', strtotime($field['end'])) : null
495         );
496
497         $this->showMultiControls();
498         $this->out->elementEnd('div');
499     }
500
501     function showMultiControls()
502     {
503         $this->out->element(
504             'a',
505             array(
506                 'class' => 'remove_row',
507                 'href' => 'javascript://',
508                 'style' => 'display: none;'
509             ),
510             '-'
511         );
512
513         $this->out->element(
514             'a',
515             array(
516                 'class' => 'add_row',
517                 'href' => 'javascript://',
518                 'style' => 'display: none;'
519             ),
520             // TRANS: Link description in extended profile page to add another profile element.
521             _m('Add another item')
522         );
523     }
524
525     /**
526      * Outputs the value of a field
527      *
528      * @param string $name  name of the field
529      * @param array  $field set of key/value pairs for the field
530      */
531     protected function showFieldValue($name, $field)
532     {
533         $type = strval(@$field['type']);
534
535         switch($type)
536         {
537         case '':
538         case 'text':
539         case 'textarea':
540             $this->out->text($this->ext->getTextValue($name));
541             break;
542         case 'date':
543             $value = $this->ext->getDateValue($name);
544             if (!empty($value)) {
545                 $this->out->element(
546                     'div',
547                     array('class' => 'field date'),
548                     date('j M Y', strtotime($value))
549                 );
550             }
551             break;
552         case 'person':
553             $this->out->text($this->ext->getTextValue($name));
554             break;
555         case 'tags':
556             $this->out->text($this->ext->getTags());
557             break;
558         case 'phone':
559             $this->showPhone($name, $field);
560             break;
561         case 'website':
562             $this->showWebsite($name, $field);
563             break;
564         case 'im':
565             $this->showIm($name, $field);
566             break;
567         case 'experience':
568             $this->showExperience($name, $field);
569             break;
570         case 'education':
571             $this->showEducation($name, $field);
572             break;
573         default:
574             $this->out->text("TYPE: $type");
575         }
576     }
577
578     /**
579      * Show an editable version of the field
580      *
581      * @param string $name  name fo the field
582      * @param array  $field array of key/value pairs for the field
583      */
584     protected function showEditableField($name, $field)
585     {
586         $out = $this->out;
587
588         $type = strval(@$field['type']);
589         $id = "extprofile-" . $name;
590
591         $value = 'placeholder';
592
593         switch ($type) {
594         case '':
595         case 'text':
596             $out->input($id, null, $this->ext->getTextValue($name));
597             break;
598         case 'date':
599             $value = $this->ext->getDateValue($name);
600             $out->input(
601                 $id,
602                 null,
603                 empty($value) ? null : date('j M Y', strtotime($value))
604             );
605             break;
606         case 'person':
607             $out->input($id, null, $this->ext->getTextValue($name));
608             break;
609         case 'textarea':
610             $out->textarea($id, null,  $this->ext->getTextValue($name));
611             break;
612         case 'tags':
613             $out->input($id, null, $this->ext->getTags());
614             break;
615         case 'phone':
616             $this->showEditablePhone($name, $field);
617             break;
618         case 'im':
619             $this->showEditableIm($name, $field);
620             break;
621         case 'website':
622             $this->showEditableWebsite($name, $field);
623             break;
624         case 'experience':
625             $this->showEditableExperience($name, $field);
626             break;
627         case 'education':
628             $this->showEditableEducation($name, $field);
629             break;
630         default:
631             // TRANS: Field label for undefined field in extended profile.
632             $out->input($id, null, sprintf(_m('TYPE: %s'),$type));
633         }
634     }
635
636     /**
637      * Action elements
638      *
639      * @return void
640      */
641     function formActions()
642     {
643         $this->out->submit(
644             'save',
645             // TRANS: Button text for saving extended profile properties.
646             _m('BUTTON','Save'),
647             'submit form_action-secondary',
648             'save',
649             // TRANS: .
650             // TRANS: Button title for saving extended profile properties.
651             _m('Save details')
652        );
653     }
654
655     /**
656      * ID of the form
657      *
658      * @return string ID of the form
659      */
660     function id()
661     {
662         return 'profile-details-' . $this->profile->id;
663     }
664
665     /**
666      * class of the form
667      *
668      * @return string of the form class
669      */
670     function formClass()
671     {
672         return 'form_profile_details form_settings';
673     }
674
675     /**
676      * Action of the form
677      *
678      * @return string URL of the action
679      */
680     function action()
681     {
682         return common_local_url('profiledetailsettings');
683     }
684 }