profiledetailsettings.php 17 KB

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