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