extendedprofilewidget.php 21 KB

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