extendedprofilewidget.php 21 KB

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