extendedprofile.php 13 KB

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