extendedprofilewidget.php 21 KB

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