profiledetailsettings.php 18 KB

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