extendedprofile.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 to represent extended profile data
  24. */
  25. class ExtendedProfile
  26. {
  27. protected $fields;
  28. /**
  29. * Constructor
  30. *
  31. * @param Profile $profile
  32. * @throws NoSuchUserException
  33. */
  34. public function __construct(Profile $profile)
  35. {
  36. $this->profile = $profile;
  37. $this->user = $profile->getUser();
  38. $this->fields = $this->loadFields();
  39. $this->sections = $this->getSections();
  40. //common_debug(var_export($this->sections, true));
  41. //common_debug(var_export($this->fields, true));
  42. }
  43. /**
  44. * Load extended profile fields
  45. *
  46. * @return array $fields the list of fields
  47. * @throws Exception
  48. */
  49. public function loadFields()
  50. {
  51. $detail = new Profile_detail();
  52. $detail->profile_id = $this->profile->getID();
  53. $detail->find();
  54. $fields = [];
  55. while ($detail->fetch()) {
  56. $fields[$detail->field_name][] = clone($detail);
  57. }
  58. return $fields;
  59. }
  60. /**
  61. * Get a the self-tags associated with this profile
  62. *
  63. * @return string the concatenated string of tags
  64. */
  65. public function getTags()
  66. {
  67. return implode(' ', Profile_tag::getSelfTagsArray($this->profile));
  68. }
  69. /**
  70. * Return a simple string value. Checks for fields that should
  71. * be stored in the regular profile and returns values from it
  72. * if appropriate.
  73. *
  74. * @param string $name name of the detail field to get the
  75. * value from
  76. *
  77. * @return string|null the value, null if none
  78. */
  79. public function getTextValue($name): ?string
  80. {
  81. $key = strtolower($name);
  82. $profileFields = ['fullname', 'location', 'bio'];
  83. if (in_array($key, $profileFields)) {
  84. return $this->profile->$name;
  85. } elseif (array_key_exists($key, $this->fields)) {
  86. return $this->fields[$key][0]->field_value;
  87. } else {
  88. return null;
  89. }
  90. }
  91. public function getDateValue($name)
  92. {
  93. $key = strtolower($name);
  94. if (array_key_exists($key, $this->fields)) {
  95. return $this->fields[$key][0]->date;
  96. } else {
  97. return null;
  98. }
  99. }
  100. // XXX: getPhones, getIms, and getWebsites pretty much do the same thing,
  101. // so refactor.
  102. public function getPhones()
  103. {
  104. $phones = (isset($this->fields['phone'])) ? $this->fields['phone'] : null;
  105. $pArrays = [];
  106. if (empty($phones)) {
  107. $pArrays[] = array(
  108. // TRANS: Field label for extended profile properties.
  109. 'label' => _m('Phone'),
  110. 'index' => 0,
  111. 'type' => 'phone',
  112. 'vcard' => 'tel',
  113. 'rel' => 'office',
  114. 'value' => null
  115. );
  116. } else {
  117. for ($i = 0; $i < sizeof($phones); $i++) {
  118. $pa = array(
  119. // TRANS: Field label for extended profile properties.
  120. 'label' => _m('Phone'),
  121. 'type' => 'phone',
  122. 'index' => intval($phones[$i]->value_index),
  123. 'rel' => $phones[$i]->rel,
  124. 'value' => $phones[$i]->field_value,
  125. 'vcard' => 'tel'
  126. );
  127. $pArrays[] = $pa;
  128. }
  129. }
  130. return $pArrays;
  131. }
  132. public function getIms()
  133. {
  134. $ims = (isset($this->fields['im'])) ? $this->fields['im'] : null;
  135. $iArrays = [];
  136. if (empty($ims)) {
  137. $iArrays[] = array(
  138. // TRANS: Field label for extended profile properties (Instant Messaging).
  139. 'label' => _m('IM'),
  140. 'type' => 'im'
  141. );
  142. } else {
  143. for ($i = 0; $i < sizeof($ims); $i++) {
  144. $ia = array(
  145. // TRANS: Field label for extended profile properties (Instant Messaging).
  146. 'label' => _m('IM'),
  147. 'type' => 'im',
  148. 'index' => intval($ims[$i]->value_index),
  149. 'rel' => $ims[$i]->rel,
  150. 'value' => $ims[$i]->field_value,
  151. );
  152. $iArrays[] = $ia;
  153. }
  154. }
  155. return $iArrays;
  156. }
  157. public function getWebsites()
  158. {
  159. $sites = (isset($this->fields['website'])) ? $this->fields['website'] : null;
  160. $wArrays = [];
  161. if (empty($sites)) {
  162. $wArrays[] = array(
  163. // TRANS: Field label for extended profile properties.
  164. 'label' => _m('Website'),
  165. 'type' => 'website'
  166. );
  167. } else {
  168. for ($i = 0; $i < sizeof($sites); $i++) {
  169. $wa = array(
  170. // TRANS: Field label for extended profile properties.
  171. 'label' => _m('Website'),
  172. 'type' => 'website',
  173. 'index' => intval($sites[$i]->value_index),
  174. 'rel' => $sites[$i]->rel,
  175. 'value' => $sites[$i]->field_value,
  176. );
  177. $wArrays[] = $wa;
  178. }
  179. }
  180. return $wArrays;
  181. }
  182. public function getExperiences()
  183. {
  184. $companies = (isset($this->fields['company'])) ? $this->fields['company'] : null;
  185. $start = (isset($this->fields['start'])) ? $this->fields['start'] : null;
  186. $end = (isset($this->fields['end'])) ? $this->fields['end'] : null;
  187. $eArrays = [];
  188. if (empty($companies)) {
  189. $eArrays[] = array(
  190. // TRANS: Field label for extended profile properties.
  191. 'label' => _m('Employer'),
  192. 'type' => 'experience',
  193. 'company' => null,
  194. 'start' => null,
  195. 'end' => null,
  196. 'current' => false,
  197. 'index' => 0
  198. );
  199. } else {
  200. for ($i = 0; $i < sizeof($companies); $i++) {
  201. $ea = array(
  202. // TRANS: Field label for extended profile properties.
  203. 'label' => _m('Employer'),
  204. 'type' => 'experience',
  205. 'company' => $companies[$i]->field_value,
  206. 'index' => intval($companies[$i]->value_index),
  207. 'current' => $end[$i]->rel,
  208. 'start' => $start[$i]->date,
  209. 'end' => $end[$i]->date
  210. );
  211. $eArrays[] = $ea;
  212. }
  213. }
  214. return $eArrays;
  215. }
  216. public function getEducation()
  217. {
  218. $schools = (isset($this->fields['school'])) ? $this->fields['school'] : null;
  219. $degrees = (isset($this->fields['degree'])) ? $this->fields['degree'] : null;
  220. $descs = (isset($this->fields['degree_descr'])) ? $this->fields['degree_descr'] : null;
  221. $start = (isset($this->fields['school_start'])) ? $this->fields['school_start'] : null;
  222. $end = (isset($this->fields['school_end'])) ? $this->fields['school_end'] : null;
  223. $iArrays = [];
  224. if (empty($schools)) {
  225. $iArrays[] = array(
  226. 'type' => 'education',
  227. // TRANS: Field label for extended profile properties.
  228. 'label' => _m('Institution'),
  229. 'school' => null,
  230. 'degree' => null,
  231. 'description' => null,
  232. 'start' => null,
  233. 'end' => null,
  234. 'index' => 0
  235. );
  236. } else {
  237. for ($i = 0; $i < sizeof($schools); $i++) {
  238. $ia = array(
  239. 'type' => 'education',
  240. // TRANS: Field label for extended profile properties.
  241. 'label' => _m('Institution'),
  242. 'school' => $schools[$i]->field_value,
  243. 'degree' => isset($degrees[$i]->field_value) ? $degrees[$i]->field_value : null,
  244. 'description' => isset($descs[$i]->field_value) ? $descs[$i]->field_value : null,
  245. 'index' => intval($schools[$i]->value_index),
  246. 'start' => $start[$i]->date,
  247. 'end' => $end[$i]->date
  248. );
  249. $iArrays[] = $ia;
  250. }
  251. }
  252. return $iArrays;
  253. }
  254. /**
  255. * Return all the sections of the extended profile
  256. *
  257. * @return array the big list of sections and fields
  258. * @throws Exception
  259. */
  260. public function getSections()
  261. {
  262. $display_extra = false;
  263. $gsefields = GNUsocialProfileExtensionField::allFields();
  264. $extra_fields = [];
  265. gnusocial_profile_merge($this->profile);
  266. foreach ($gsefields as $field) {
  267. $display_extra = true;
  268. $field_key = $field->systemname;
  269. switch ($field->type) {
  270. case 'text':
  271. $extra_fields[$field_key]['type'] = 'custom-textarea';
  272. break;
  273. case 'str':
  274. default:
  275. $extra_fields[$field_key]['type'] = 'custom-text';
  276. break;
  277. }
  278. $extra_fields[$field_key]['label'] = $field->title;
  279. $extra_fields[$field_key]['value'] = $this->profile->$field_key;
  280. }
  281. $sections = [
  282. 'basic' => [
  283. // TRANS: Field label for extended profile properties.
  284. 'label' => _m('Personal'),
  285. 'fields' => [
  286. 'fullname' => [
  287. // TRANS: Field label for extended profile properties.
  288. 'label' => _m('Full name'),
  289. 'profile' => 'fullname',
  290. 'vcard' => 'fn',
  291. ],
  292. 'title' => [
  293. // TRANS: Field label for extended profile properties.
  294. 'label' => _m('Title'),
  295. 'vcard' => 'title',
  296. ],
  297. 'manager' => [
  298. // TRANS: Field label for extended profile properties.
  299. 'label' => _m('Manager'),
  300. 'type' => 'person',
  301. 'vcard' => 'x-manager',
  302. ],
  303. 'location' => [
  304. // TRANS: Field label for extended profile properties.
  305. 'label' => _m('Location'),
  306. 'profile' => 'location'
  307. ],
  308. 'bio' => [
  309. // TRANS: Field label for extended profile properties.
  310. 'label' => _m('Bio'),
  311. 'type' => 'textarea',
  312. 'profile' => 'bio',
  313. ],
  314. 'tags' => [
  315. // TRANS: Field label for extended profile properties.
  316. 'label' => _m('Tags'),
  317. 'type' => 'tags',
  318. 'profile' => 'tags',
  319. ],
  320. ],
  321. ],
  322. 'contact' => [
  323. // TRANS: Field label for extended profile properties.
  324. 'label' => _m('Contact'),
  325. 'fields' => [
  326. 'phone' => $this->getPhones(),
  327. 'im' => $this->getIms(),
  328. 'website' => $this->getWebsites()
  329. ],
  330. ],
  331. 'personal' => [
  332. // TRANS: Field label for extended profile properties.
  333. 'label' => _m('Personal'),
  334. 'fields' => [
  335. 'birthday' => [
  336. // TRANS: Field label for extended profile properties.
  337. 'label' => _m('Birthday'),
  338. 'type' => 'date',
  339. 'vcard' => 'bday',
  340. ],
  341. 'spouse' => [
  342. // TRANS: Field label for extended profile properties.
  343. 'label' => _m('Spouse\'s name'),
  344. 'vcard' => 'x-spouse',
  345. ],
  346. 'kids' => [
  347. // TRANS: Field label for extended profile properties.
  348. 'label' => _m('Kids\' names')
  349. ],
  350. ],
  351. ],
  352. 'experience' => [
  353. // TRANS: Field label for extended profile properties.
  354. 'label' => _m('Work experience'),
  355. 'fields' => [
  356. 'experience' => $this->getExperiences()
  357. ],
  358. ],
  359. 'education' => [
  360. // TRANS: Field label for extended profile properties.
  361. 'label' => _m('Education'),
  362. 'fields' => [
  363. 'education' => $this->getEducation()
  364. ],
  365. ],
  366. ];
  367. if ($display_extra) {
  368. $sections['extra'] = [
  369. 'label' => _m('Additional information'),
  370. 'fields' => $extra_fields,
  371. ];
  372. }
  373. return $sections;
  374. }
  375. }