]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ExtendedProfile/lib/extendedprofilewidget.php
initialize fave, sub, and membership URIs
[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                 'google'   => 'Google Profile',
310                 'other'    => 'Other',
311                 'twitter'  => 'Twitter'
312             ),
313             null,
314             false,
315             isset($field['rel']) ? $field['rel'] : null
316         );
317
318         $this->showMultiControls();
319         $this->out->elementEnd('div');
320     }
321
322     protected function showExperience($name, $field)
323     {
324         $this->out->elementStart('div', 'experience-item');
325         // TRANS: Field label in experience area of extended profile.
326         $this->out->element('div', 'label', _m('Company'));
327
328         if (!empty($field['company'])) {
329             $this->out->element('div', 'field', $field['company']);
330
331             // TRANS: Field label in extended profile (when did one start a position or education).
332             $this->out->element('div', 'label', _m('Start'));
333             $this->out->element(
334                 'div',
335                 array('class' => 'field date'),
336                 date('j M Y', strtotime($field['start'])
337                 )
338             );
339             // TRANS: Field label in extended profile (when did one end a position or education).
340             $this->out->element('div', 'label', _m('End'));
341             $this->out->element(
342                 'div',
343                 array('class' => 'field date'),
344                 date('j M Y', strtotime($field['end'])
345                 )
346             );
347
348             if ($field['current']) {
349                 $this->out->element(
350                     'div',
351                     array('class' => 'field current'),
352                     // TRANS: Field value in experience area of extended profile (one still holds a position).
353                     _m('(Current)')
354                 );
355             }
356         }
357         $this->out->elementEnd('div');
358     }
359
360     protected function showEditableExperience($name, $field)
361     {
362         $index = isset($field['index']) ? $field['index'] : 0;
363         $id    = "extprofile-$name-$index";
364         $this->out->elementStart(
365             'div', array(
366                 'id' => $id . '-edit',
367                 'class' => 'experience-item'
368             )
369         );
370
371         // TRANS: Field label in experience edit area of extended profile (which company does one work for).
372         $this->out->element('div', 'label', _m('Company'));
373         $this->out->input(
374             $id,
375             null,
376             isset($field['company']) ? $field['company'] : null
377         );
378
379         // TRANS: Field label in extended profile (when did one start a position or education).
380         $this->out->element('div', 'label', _m('Start'));
381         $this->out->input(
382             $id . '-start',
383             null,
384             isset($field['start']) ? date('j M Y', strtotime($field['start'])) : null
385         );
386
387             // TRANS: Field label in extended profile (when did one end a position or education).
388         $this->out->element('div', 'label', _m('End'));
389
390         $this->out->input(
391             $id . '-end',
392             null,
393             isset($field['end']) ? date('j M Y', strtotime($field['end'])) : null
394         );
395         $this->out->hidden(
396             $id . '-current',
397             'false'
398         );
399         $this->out->elementStart('div', 'current-checkbox');
400         $this->out->checkbox(
401             $id . '-current',
402             // TRANS: Checkbox label in experience edit area of extended profile (one still works at a company).
403             _m('Current'),
404             $field['current']
405         );
406         $this->out->elementEnd('div');
407
408         $this->showMultiControls();
409         $this->out->elementEnd('div');
410     }
411
412     protected function showEducation($name, $field)
413     {
414         $this->out->elementStart('div', 'education-item');
415         // TRANS: Field label in education area of extended profile.
416         $this->out->element('div', 'label', _m('Institution'));
417         if (!empty($field['school'])) {
418             $this->out->element('div', 'field', $field['school']);
419             // TRANS: Field label in extended profile for specifying an academic degree.
420             $this->out->element('div', 'label', _m('Degree'));
421             $this->out->element('div', 'field', $field['degree']);
422             // TRANS: Field label in education area of extended profile.
423             $this->out->element('div', 'label', _m('Description'));
424             $this->out->element('div', 'field', $field['description']);
425             // TRANS: Field label in extended profile (when did one start a position or education).
426             $this->out->element('div', 'label', _m('Start'));
427             $this->out->element(
428                 'div',
429                 array('class' => 'field date'),
430                 date('j M Y', strtotime($field['start'])
431                 )
432             );
433             // TRANS: Field label in extended profile (when did one end a position or education).
434             $this->out->element('div', 'label', _m('End'));
435             $this->out->element(
436                 'div',
437                 array('class' => 'field date'),
438                 date('j M Y', strtotime($field['end'])
439                 )
440             );
441         }
442         $this->out->elementEnd('div');
443     }
444
445     protected function showEditableEducation($name, $field)
446     {
447         $index = isset($field['index']) ? $field['index'] : 0;
448         $id    = "extprofile-$name-$index";
449         $this->out->elementStart(
450             'div', array(
451                 'id' => $id . '-edit',
452                 'class' => 'education-item'
453             )
454         );
455         // TRANS: Field label in education edit area of extended profile.
456         $this->out->element('div', 'label', _m('Institution'));
457         $this->out->input(
458             $id,
459             null,
460             isset($field['school']) ? $field['school'] : null
461         );
462
463         // TRANS: Field label in extended profile for specifying an academic degree.
464         $this->out->element('div', 'label', _m('Degree'));
465         $this->out->input(
466             $id . '-degree',
467             null,
468             isset($field['degree']) ? $field['degree'] : null
469         );
470
471         // TRANS: Field label in education edit area of extended profile.
472         $this->out->element('div', 'label', _m('Description'));
473
474         $this->out->textarea(
475             $id . '-description',
476             null,
477             isset($field['description']) ? $field['description'] : null
478         );
479
480         // TRANS: Field label in extended profile (when did one start a position or education).
481         $this->out->element('div', 'label', _m('Start'));
482         $this->out->input(
483             $id . '-start',
484             null,
485             // @todo FIXME: does date format need i18n? If so, should probly be dealt with in core.
486             isset($field['start']) ? date('j M Y', strtotime($field['start'])) : null
487         );
488
489         // TRANS: Field label in extended profile (when did one end a position or education).
490         $this->out->element('div', 'label', _m('End'));
491         $this->out->input(
492             $id . '-end',
493             null,
494             // @todo FIXME: does date format need i18n? If so, should probly be dealt with in core.
495             isset($field['end']) ? date('j M Y', strtotime($field['end'])) : null
496         );
497
498         $this->showMultiControls();
499         $this->out->elementEnd('div');
500     }
501
502     function showMultiControls()
503     {
504         $this->out->element(
505             'a',
506             array(
507                 'class' => 'remove_row',
508                 'href' => 'javascript://',
509                 'style' => 'display: none;'
510             ),
511             '-'
512         );
513
514         $this->out->element(
515             'a',
516             array(
517                 'class' => 'add_row',
518                 'href' => 'javascript://',
519                 'style' => 'display: none;'
520             ),
521             // TRANS: Link description in extended profile page to add another profile element.
522             _m('Add another item')
523         );
524     }
525
526     /**
527      * Outputs the value of a field
528      *
529      * @param string $name  name of the field
530      * @param array  $field set of key/value pairs for the field
531      */
532     protected function showFieldValue($name, $field)
533     {
534         $type = strval(@$field['type']);
535
536         switch($type)
537         {
538         case '':
539         case 'text':
540         case 'textarea':
541             $this->out->text($this->ext->getTextValue($name));
542             break;
543         case 'date':
544             $value = $this->ext->getDateValue($name);
545             if (!empty($value)) {
546                 $this->out->element(
547                     'div',
548                     array('class' => 'field date'),
549                     date('j M Y', strtotime($value))
550                 );
551             }
552             break;
553         case 'person':
554             $this->out->text($this->ext->getTextValue($name));
555             break;
556         case 'tags':
557             $this->out->text($this->ext->getTags());
558             break;
559         case 'phone':
560             $this->showPhone($name, $field);
561             break;
562         case 'website':
563             $this->showWebsite($name, $field);
564             break;
565         case 'im':
566             $this->showIm($name, $field);
567             break;
568         case 'experience':
569             $this->showExperience($name, $field);
570             break;
571         case 'education':
572             $this->showEducation($name, $field);
573             break;
574         default:
575             $this->out->text("TYPE: $type");
576         }
577     }
578
579     /**
580      * Show an editable version of the field
581      *
582      * @param string $name  name fo the field
583      * @param array  $field array of key/value pairs for the field
584      */
585     protected function showEditableField($name, $field)
586     {
587         $out = $this->out;
588
589         $type = strval(@$field['type']);
590         $id = "extprofile-" . $name;
591
592         $value = 'placeholder';
593
594         switch ($type) {
595         case '':
596         case 'text':
597             $out->input($id, null, $this->ext->getTextValue($name));
598             break;
599         case 'date':
600             $value = $this->ext->getDateValue($name);
601             $out->input(
602                 $id,
603                 null,
604                 empty($value) ? null : date('j M Y', strtotime($value))
605             );
606             break;
607         case 'person':
608             $out->input($id, null, $this->ext->getTextValue($name));
609             break;
610         case 'textarea':
611             $out->textarea($id, null,  $this->ext->getTextValue($name));
612             break;
613         case 'tags':
614             $out->input($id, null, $this->ext->getTags());
615             break;
616         case 'phone':
617             $this->showEditablePhone($name, $field);
618             break;
619         case 'im':
620             $this->showEditableIm($name, $field);
621             break;
622         case 'website':
623             $this->showEditableWebsite($name, $field);
624             break;
625         case 'experience':
626             $this->showEditableExperience($name, $field);
627             break;
628         case 'education':
629             $this->showEditableEducation($name, $field);
630             break;
631         default:
632             // TRANS: Field label for undefined field in extended profile.
633             $out->input($id, null, sprintf(_m('TYPE: %s'),$type));
634         }
635     }
636
637     /**
638      * Action elements
639      *
640      * @return void
641      */
642     function formActions()
643     {
644         $this->out->submit(
645             'save',
646             // TRANS: Button text for saving extended profile properties.
647             _m('BUTTON','Save'),
648             'submit form_action-secondary',
649             'save',
650             // TRANS: .
651             // TRANS: Button title for saving extended profile properties.
652             _m('Save details')
653        );
654     }
655
656     /**
657      * ID of the form
658      *
659      * @return string ID of the form
660      */
661     function id()
662     {
663         return 'profile-details-' . $this->profile->id;
664     }
665
666     /**
667      * class of the form
668      *
669      * @return string of the form class
670      */
671     function formClass()
672     {
673         return 'form_profile_details form_settings';
674     }
675
676     /**
677      * Action of the form
678      *
679      * @return string URL of the action
680      */
681     function action()
682     {
683         return common_local_url('profiledetailsettings');
684     }
685 }