profiledetailsettings.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. class ProfileDetailSettingsAction extends ProfileSettingsAction
  23. {
  24. function title()
  25. {
  26. // TRANS: Title for extended profile settings.
  27. return _m('Extended profile settings');
  28. }
  29. /**
  30. * Instructions for use
  31. *
  32. * @return instructions for use
  33. */
  34. function getInstructions()
  35. {
  36. // TRANS: Usage instructions for profile settings.
  37. return _m('You can update your personal profile info here '.
  38. 'so people know more about you.');
  39. }
  40. function showStylesheets() {
  41. parent::showStylesheets();
  42. $this->cssLink('plugins/ExtendedProfile/css/profiledetail.css');
  43. return true;
  44. }
  45. function showScripts() {
  46. parent::showScripts();
  47. $this->script('plugins/ExtendedProfile/js/profiledetail.js');
  48. return true;
  49. }
  50. function handlePost()
  51. {
  52. // CSRF protection
  53. $token = $this->trimmed('token');
  54. if (!$token || $token != common_session_token()) {
  55. $this->showForm(
  56. // TRANS: Client error displayed when the session token does not match or is not given.
  57. _m('There was a problem with your session token. '
  58. . 'Try again, please.'
  59. )
  60. );
  61. return;
  62. }
  63. if ($this->arg('save')) {
  64. $this->saveDetails();
  65. } else {
  66. // TRANS: Message given submitting a form with an unknown action.
  67. $this->showForm(_m('Unexpected form submission.'));
  68. }
  69. }
  70. function showContent()
  71. {
  72. $cur = common_current_user();
  73. $profile = $cur->getProfile();
  74. $widget = new ExtendedProfileWidget(
  75. $this,
  76. $profile,
  77. ExtendedProfileWidget::EDITABLE
  78. );
  79. $widget->show();
  80. }
  81. function saveDetails()
  82. {
  83. common_debug(var_export($_POST, true));
  84. $user = common_current_user();
  85. try {
  86. $this->saveStandardProfileDetails($user);
  87. $profile = $user->getProfile();
  88. $simpleFieldNames = array('title', 'spouse', 'kids', 'manager');
  89. $dateFieldNames = array('birthday');
  90. foreach ($simpleFieldNames as $name) {
  91. $value = $this->trimmed('extprofile-' . $name);
  92. if (!empty($value)) {
  93. $this->saveField($user, $name, $value);
  94. }
  95. }
  96. foreach ($dateFieldNames as $name) {
  97. $value = $this->trimmed('extprofile-' . $name);
  98. $dateVal = $this->parseDate($name, $value);
  99. $this->saveField(
  100. $user,
  101. $name,
  102. null,
  103. null,
  104. null,
  105. $dateVal
  106. );
  107. }
  108. $this->savePhoneNumbers($user);
  109. $this->saveIms($user);
  110. $this->saveWebsites($user);
  111. $this->saveExperiences($user);
  112. $this->saveEducations($user);
  113. } catch (Exception $e) {
  114. $this->showForm($e->getMessage(), false);
  115. return;
  116. }
  117. // TRANS: Success message after saving extended profile details.
  118. $this->showForm(_m('Details saved.'), true);
  119. }
  120. function parseDate($fieldname, $datestr, $required = false)
  121. {
  122. if (empty($datestr)) {
  123. if ($required) {
  124. $msg = sprintf(
  125. // TRANS: Exception thrown when no date was entered in a required date field.
  126. // TRANS: %s is the field name.
  127. _m('You must supply a date for "%s".'),
  128. $fieldname
  129. );
  130. throw new Exception($msg);
  131. }
  132. } else {
  133. $ts = strtotime($datestr);
  134. if ($ts === false) {
  135. throw new Exception(
  136. sprintf(
  137. // TRANS: Exception thrown on incorrect data input.
  138. // TRANS: %1$s is a field name, %2$s is the incorrect input.
  139. _m('Invalid date entered for "%1$s": %2$s.'),
  140. $fieldname,
  141. $ts
  142. )
  143. );
  144. }
  145. return common_sql_date($ts);
  146. }
  147. return null;
  148. }
  149. function savePhoneNumbers($user) {
  150. $phones = $this->findPhoneNumbers();
  151. $this->removeAll($user, 'phone');
  152. $i = 0;
  153. foreach($phones as $phone) {
  154. if (!empty($phone['value'])) {
  155. ++$i;
  156. $this->saveField(
  157. $user,
  158. 'phone',
  159. $phone['value'],
  160. $phone['rel'],
  161. $i
  162. );
  163. }
  164. }
  165. }
  166. function findPhoneNumbers() {
  167. // Form vals look like this:
  168. // 'extprofile-phone-1' => '11332',
  169. // 'extprofile-phone-1-rel' => 'mobile',
  170. $phones = $this->sliceParams('phone', 2);
  171. $phoneArray = array();
  172. foreach ($phones as $phone) {
  173. list($number, $rel) = array_values($phone);
  174. $phoneArray[] = array(
  175. 'value' => $number,
  176. 'rel' => $rel
  177. );
  178. }
  179. return $phoneArray;
  180. }
  181. function findIms() {
  182. // Form vals look like this:
  183. // 'extprofile-im-0' => 'jed',
  184. // 'extprofile-im-0-rel' => 'yahoo',
  185. $ims = $this->sliceParams('im', 2);
  186. $imArray = array();
  187. foreach ($ims as $im) {
  188. list($id, $rel) = array_values($im);
  189. $imArray[] = array(
  190. 'value' => $id,
  191. 'rel' => $rel
  192. );
  193. }
  194. return $imArray;
  195. }
  196. function saveIms($user) {
  197. $ims = $this->findIms();
  198. $this->removeAll($user, 'im');
  199. $i = 0;
  200. foreach($ims as $im) {
  201. if (!empty($im['value'])) {
  202. ++$i;
  203. $this->saveField(
  204. $user,
  205. 'im',
  206. $im['value'],
  207. $im['rel'],
  208. $i
  209. );
  210. }
  211. }
  212. }
  213. function findWebsites() {
  214. // Form vals look like this:
  215. $sites = $this->sliceParams('website', 2);
  216. $wsArray = array();
  217. foreach ($sites as $site) {
  218. list($id, $rel) = array_values($site);
  219. $wsArray[] = array(
  220. 'value' => $id,
  221. 'rel' => $rel
  222. );
  223. }
  224. return $wsArray;
  225. }
  226. function saveWebsites($user) {
  227. $sites = $this->findWebsites();
  228. $this->removeAll($user, 'website');
  229. $i = 0;
  230. foreach($sites as $site) {
  231. if (!empty($site['value']) && !common_valid_http_url($site['value'])) {
  232. // TRANS: Exception thrown when entering an invalid URL.
  233. // TRANS: %s is the invalid URL.
  234. throw new Exception(sprintf(_m('Invalid URL: %s.'), $site['value']));
  235. }
  236. if (!empty($site['value'])) {
  237. ++$i;
  238. $this->saveField(
  239. $user,
  240. 'website',
  241. $site['value'],
  242. $site['rel'],
  243. $i
  244. );
  245. }
  246. }
  247. }
  248. function findExperiences() {
  249. // Form vals look like this:
  250. // 'extprofile-experience-0' => 'Bozotronix',
  251. // 'extprofile-experience-0-current' => 'true'
  252. // 'extprofile-experience-0-start' => '1/5/10',
  253. // 'extprofile-experience-0-end' => '2/3/11',
  254. $experiences = $this->sliceParams('experience', 4);
  255. $expArray = array();
  256. foreach ($experiences as $exp) {
  257. if (sizeof($experiences) == 4) {
  258. list($company, $current, $end, $start) = array_values($exp);
  259. } else {
  260. $end = null;
  261. list($company, $current, $start) = array_values($exp);
  262. }
  263. if (!empty($company)) {
  264. $expArray[] = array(
  265. 'company' => $company,
  266. 'start' => $this->parseDate('Start', $start, true),
  267. 'end' => ($current == 'false') ? $this->parseDate('End', $end, true) : null,
  268. 'current' => ($current == 'false') ? false : true
  269. );
  270. }
  271. }
  272. return $expArray;
  273. }
  274. function saveExperiences($user) {
  275. common_debug('save experiences');
  276. $experiences = $this->findExperiences();
  277. $this->removeAll($user, 'company');
  278. $this->removeAll($user, 'start');
  279. $this->removeAll($user, 'end'); // also stores 'current'
  280. $i = 0;
  281. foreach($experiences as $experience) {
  282. if (!empty($experience['company'])) {
  283. ++$i;
  284. $this->saveField(
  285. $user,
  286. 'company',
  287. $experience['company'],
  288. null,
  289. $i
  290. );
  291. $this->saveField(
  292. $user,
  293. 'start',
  294. null,
  295. null,
  296. $i,
  297. $experience['start']
  298. );
  299. // Save "current" employer indicator in rel
  300. if ($experience['current']) {
  301. $this->saveField(
  302. $user,
  303. 'end',
  304. null,
  305. 'current', // rel
  306. $i
  307. );
  308. } else {
  309. $this->saveField(
  310. $user,
  311. 'end',
  312. null,
  313. null,
  314. $i,
  315. $experience['end']
  316. );
  317. }
  318. }
  319. }
  320. }
  321. function findEducations() {
  322. // Form vals look like this:
  323. // 'extprofile-education-0-school' => 'Pigdog',
  324. // 'extprofile-education-0-degree' => 'BA',
  325. // 'extprofile-education-0-description' => 'Blar',
  326. // 'extprofile-education-0-start' => '05/22/99',
  327. // 'extprofile-education-0-end' => '05/22/05',
  328. $edus = $this->sliceParams('education', 5);
  329. $eduArray = array();
  330. foreach ($edus as $edu) {
  331. list($school, $degree, $description, $end, $start) = array_values($edu);
  332. if (!empty($school)) {
  333. $eduArray[] = array(
  334. 'school' => $school,
  335. 'degree' => $degree,
  336. 'description' => $description,
  337. 'start' => $this->parseDate('Start', $start, true),
  338. 'end' => $this->parseDate('End', $end, true)
  339. );
  340. }
  341. }
  342. return $eduArray;
  343. }
  344. function saveEducations($user) {
  345. common_debug('save education');
  346. $edus = $this->findEducations();
  347. common_debug(var_export($edus, true));
  348. $this->removeAll($user, 'school');
  349. $this->removeAll($user, 'degree');
  350. $this->removeAll($user, 'degree_descr');
  351. $this->removeAll($user, 'school_start');
  352. $this->removeAll($user, 'school_end');
  353. $i = 0;
  354. foreach($edus as $edu) {
  355. if (!empty($edu['school'])) {
  356. ++$i;
  357. $this->saveField(
  358. $user,
  359. 'school',
  360. $edu['school'],
  361. null,
  362. $i
  363. );
  364. $this->saveField(
  365. $user,
  366. 'degree',
  367. $edu['degree'],
  368. null,
  369. $i
  370. );
  371. $this->saveField(
  372. $user,
  373. 'degree_descr',
  374. $edu['description'],
  375. null,
  376. $i
  377. );
  378. $this->saveField(
  379. $user,
  380. 'school_start',
  381. null,
  382. null,
  383. $i,
  384. $edu['start']
  385. );
  386. $this->saveField(
  387. $user,
  388. 'school_end',
  389. null,
  390. null,
  391. $i,
  392. $edu['end']
  393. );
  394. }
  395. }
  396. }
  397. function arraySplit($array, $pieces)
  398. {
  399. if ($pieces < 2) {
  400. return array($array);
  401. }
  402. $newCount = ceil(count($array) / $pieces);
  403. $a = array_slice($array, 0, $newCount);
  404. $b = $this->arraySplit(array_slice($array, $newCount), $pieces - 1);
  405. return array_merge(array($a), $b);
  406. }
  407. function findMultiParams($type) {
  408. $formVals = array();
  409. $target = $type;
  410. foreach ($_POST as $key => $val) {
  411. if (strrpos('extprofile-' . $key, $target) !== false) {
  412. $formVals[$key] = $val;
  413. }
  414. }
  415. return $formVals;
  416. }
  417. function sliceParams($key, $size) {
  418. $slice = array();
  419. $params = $this->findMultiParams($key);
  420. ksort($params);
  421. $slice = $this->arraySplit($params, sizeof($params) / $size);
  422. return $slice;
  423. }
  424. /**
  425. * Save an extended profile field as a Profile_detail
  426. *
  427. * @param User $user the current user
  428. * @param string $name field name
  429. * @param string $value field value
  430. * @param string $rel field rel (type)
  431. * @param int $index index (fields can have multiple values)
  432. * @param date $date related date
  433. */
  434. function saveField($user, $name, $value, $rel = null, $index = null, $date = null)
  435. {
  436. $profile = $user->getProfile();
  437. $detail = new Profile_detail();
  438. $detail->profile_id = $profile->id;
  439. $detail->field_name = $name;
  440. $detail->value_index = $index;
  441. $result = $detail->find(true);
  442. if (empty($result)) {
  443. $detial->value_index = $index;
  444. $detail->rel = $rel;
  445. $detail->field_value = $value;
  446. $detail->date = $date;
  447. $detail->created = common_sql_now();
  448. $result = $detail->insert();
  449. if (empty($result)) {
  450. common_log_db_error($detail, 'INSERT', __FILE__);
  451. // TRANS: Server error displayed when a field could not be saved in the database.
  452. $this->serverError(_m('Could not save profile details.'));
  453. }
  454. } else {
  455. $orig = clone($detail);
  456. $detail->field_value = $value;
  457. $detail->rel = $rel;
  458. $detail->date = $date;
  459. $result = $detail->update($orig);
  460. if (empty($result)) {
  461. common_log_db_error($detail, 'UPDATE', __FILE__);
  462. // TRANS: Server error displayed when a field could not be saved in the database.
  463. $this->serverError(_m('Could not save profile details.'));
  464. }
  465. }
  466. $detail->free();
  467. }
  468. function removeAll($user, $name)
  469. {
  470. $profile = $user->getProfile();
  471. $detail = new Profile_detail();
  472. $detail->profile_id = $profile->id;
  473. $detail->field_name = $name;
  474. $detail->delete();
  475. $detail->free();
  476. }
  477. /**
  478. * Save fields that should be stored in the main profile object
  479. *
  480. * XXX: There's a lot of dupe code here from ProfileSettingsAction.
  481. * Do not want.
  482. *
  483. * @param User $user the current user
  484. */
  485. function saveStandardProfileDetails($user)
  486. {
  487. $fullname = $this->trimmed('extprofile-fullname');
  488. $location = $this->trimmed('extprofile-location');
  489. $tagstring = $this->trimmed('extprofile-tags');
  490. $bio = $this->trimmed('extprofile-bio');
  491. if ($tagstring) {
  492. $tags = array_map(
  493. 'common_canonical_tag',
  494. preg_split('/[\s,]+/', $tagstring)
  495. );
  496. } else {
  497. $tags = array();
  498. }
  499. foreach ($tags as $tag) {
  500. if (!common_valid_profile_tag($tag)) {
  501. // TRANS: Validation error in form for profile settings.
  502. // TRANS: %s is an invalid tag.
  503. throw new Exception(sprintf(_m('Invalid tag: "%s".'), $tag));
  504. }
  505. }
  506. $profile = $user->getProfile();
  507. $oldTags = $user->getSelfTags();
  508. $newTags = array_diff($tags, $oldTags);
  509. if ($fullname != $profile->fullname
  510. || $location != $profile->location
  511. || !empty($newTags)
  512. || $bio != $profile->bio) {
  513. $orig = clone($profile);
  514. $profile->nickname = $user->nickname;
  515. $profile->fullname = $fullname;
  516. $profile->bio = $bio;
  517. $profile->location = $location;
  518. $loc = Location::fromName($location);
  519. if (empty($loc)) {
  520. $profile->lat = null;
  521. $profile->lon = null;
  522. $profile->location_id = null;
  523. $profile->location_ns = null;
  524. } else {
  525. $profile->lat = $loc->lat;
  526. $profile->lon = $loc->lon;
  527. $profile->location_id = $loc->location_id;
  528. $profile->location_ns = $loc->location_ns;
  529. }
  530. $profile->profileurl = common_profile_url($user->nickname);
  531. $result = $profile->update($orig);
  532. if ($result === false) {
  533. common_log_db_error($profile, 'UPDATE', __FILE__);
  534. // TRANS: Server error thrown when user profile settings could not be saved.
  535. $this->serverError(_m('Could not save profile.'));
  536. }
  537. // Set the user tags
  538. $result = $user->setSelfTags($tags);
  539. if (!$result) {
  540. // TRANS: Server error thrown when user profile settings tags could not be saved.
  541. $this->serverError(_m('Could not save tags.'));
  542. }
  543. Event::handle('EndProfileSaveForm', array($this));
  544. }
  545. }
  546. }