Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.56 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Implements hook_menu().
  5. */
  6. function alumni_users_menu() {
  7. $items = array();
  8.  
  9. $items['alumni-list'] = array(
  10. 'title' => 'Alumni list',
  11. 'type' => MENU_CALLBACK,
  12. 'page callback' => 'drupal_get_form',
  13. 'page arguments' => array('alumni_users_list_form'),
  14. 'access arguments' => array('access alumni listing page'),
  15. );
  16.  
  17. $items['alumni/%/edit'] = array(
  18. 'title' => 'Edit alumni user',
  19. 'type' => MENU_CALLBACK,
  20. 'page callback' => 'drupal_get_form',
  21. 'page arguments' => array('alumni_users_profile_edit', 1),
  22. 'access callback' => 'alumni_users_profile_edit_access',
  23. );
  24.  
  25. $items['alumni/test'] = array(
  26. 'title' => 'Edit alumni user',
  27. 'type' => MENU_CALLBACK,
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('alumni_user_get_pass_field'),
  30. // 'access callback' => 'alumni_users_profile_edit_access',
  31. );
  32.  
  33. return $items;
  34. }
  35.  
  36. /**
  37. * Implements hook_menu_alter().
  38. */
  39. function alumni_users_menu_alter(&$items) {
  40. // View user profile page.
  41. $items['user/%']['page callback'] = 'alumni_users_profile_view';
  42. $items['user/%']['page arguments'] = array(1);
  43. $items['user/%']['access callback'] = 'alumni_users_profile_edit_access';
  44.  
  45. // Edit user profile.
  46. $items['user/%/edit']['page callback'] = 'drupal_get_form';
  47. $items['user/%/edit']['page arguments'] = array('alumni_users_profile_edit', 1);
  48. $items['user/%/edit']['access callback'] = TRUE;
  49. }
  50.  
  51. /**
  52. * Implements hook_permission().
  53. */
  54. function alumni_users_permission() {
  55.  
  56. return array(
  57. 'access alumni listing page' => array(
  58. 'title' => t('Alumni listing page'),
  59. 'description' => t('Access the alumni listing page'),
  60. ),
  61. 'access to alumni profile' => array(
  62. 'title' => t('View alumni profile page'),
  63. 'description' => t('View alumni profile page'),
  64. ),
  65. 'administer alumni profile page' => array(
  66. 'title' => t('Administer alumni profile page'),
  67. 'description' => t('Administer alumni profile page'),
  68. ),
  69. );
  70. }
  71.  
  72. /**
  73. * Implement hook_theme().
  74. */
  75. function alumni_users_theme($existing, $type, $theme, $path) {
  76.  
  77. $items['alumni_users_display'] = array(
  78. 'template' => 'alumni-user-display',
  79. 'path' => drupal_get_path('module', 'alumni_users') . '/tpl',
  80. 'variables' => array(
  81. 'nickname' => NULL,
  82. 'fullname' => NULL,
  83. 'veteran' => NULL,
  84. 'branch_of_service' => NULL,
  85. 'business_name' => NULL,
  86. 'website' => NULL,
  87. 'curriculums' => NULL,
  88. 'msglink' => NULL,
  89. 'awards_credentials' => NULL,
  90. 'alumniorfellow' => NULL,
  91. ),
  92. );
  93.  
  94. $items['alumni_users_profile_edit'] = array(
  95. 'render element' => 'form',
  96. 'template' => 'alumni-user-profile-edit',
  97. 'path' => drupal_get_path('module', 'alumni_users') . '/tpl',
  98. );
  99.  
  100. $items['alumni_users_profile_view'] = array(
  101. 'template' => 'alumni-user-profile-view',
  102. 'path' => drupal_get_path('module', 'alumni_users') . '/tpl',
  103. 'variables' => array('alumni_user' => NULL),
  104. );
  105.  
  106. return $items;
  107. }
  108.  
  109. /**
  110. * Access callback for user account editing.
  111. */
  112. function alumni_users_profile_edit_access() {
  113. global $user;
  114.  
  115. $current_user = $user->uid && user_access('change own username');
  116. $alumni_profile_admin = user_access('administer alumni profile page');
  117.  
  118. return ($current_user || user_access('administer users') || $alumni_profile_admin);
  119. }
  120.  
  121. /**
  122. * Alumni list form.
  123. */
  124. function alumni_users_list_form($form, &$form_state) {
  125.  
  126. $form['alumni_search'] = array(
  127. '#type' => 'textfield',
  128. '#title' => t('Alumni name'),
  129. '#size' => 60,
  130. '#maxlength' => 128,
  131. );
  132.  
  133. $form['alumni_serch_submit'] = array(
  134. '#type' => 'submit',
  135. '#value' => t('Find alumni')
  136. );
  137.  
  138. $form['alumni_serch_reset'] = array(
  139. '#type' => 'submit',
  140. '#value' => t('Reset')
  141. );
  142.  
  143. $completed_curriculum = db_select('contentenrollment', 'ce')->extend('PagerDefault');
  144. $completed_curriculum->innerJoin('curriculum', 'c', 'c.CurriculumID = ce.contentID');
  145. $completed_curriculum->innerJoin('users', 'u', 'u.uid = ce.userID');
  146. $completed_curriculum->fields('ce', array('userID'));
  147. $completed_curriculum->isNotNull('ce.CompletionDate')
  148. ->condition('u.uid', 1, '!=');
  149.  
  150. //add condition if need search user
  151. if (isset($form_state['values']['alumni_search']) && ($form_state['values']['alumni_search'])) {
  152.  
  153. $variable_search = db_or();
  154. $completed_curriculum->leftJoin('field_data_field_user_last_name', 'uln', 'uln.entity_id = ce.userID');
  155. $completed_curriculum->leftJoin('field_data_field_user_first_name', 'ufn', 'ufn.entity_id = ce.userID');
  156. $variable_search->condition('u.name', '%' . db_like($form_state['values']['alumni_search']) . '%', 'LIKE');
  157. $variable_search->condition('uln.field_user_last_name_value', '%' . db_like($form_state['values']['alumni_search']) . '%', 'LIKE');
  158. $variable_search->condition('ufn.field_user_first_name_value', '%' . db_like($form_state['values']['alumni_search']) . '%', 'LIKE');
  159. $completed_curriculum->condition($variable_search);
  160.  
  161. } else {
  162.  
  163. $completed_curriculum->distinct()
  164. ->orderBy('ce.userID')
  165. ->limit(5);
  166. }
  167.  
  168. $user_ids = $completed_curriculum->execute()->fetchCol();
  169. $alumni_list = user_load_multiple($user_ids);
  170. $alumni_displays = alumni_users_create_alumni_list($alumni_list);
  171.  
  172. $form['alumni_table'] = array(
  173. '#markup' => theme('item_list', array('items' => $alumni_displays, 'title' => '', 'type' => 'ul', 'attributes' => array())),
  174. );
  175.  
  176. if (!isset($form_state['values']['alumni_search']) || (!$form_state['values']['alumni_search'])) {
  177. $form['pager'] = array('#markup' => theme('pager'));
  178. }
  179.  
  180. return $form;
  181. }
  182.  
  183.  
  184. /**
  185. * Alumni list form validate.
  186. */
  187. function alumni_users_list_form_validate($form, &$form_state) {
  188.  
  189. $form_state['values']['alumni_search'] = trim($form_state['values']['alumni_search']);
  190. if (($form_state['values']['alumni_search']) && (strlen($form_state['values']['alumni_search']) < 3)) {
  191. form_set_error('alumni_search', t('3 characters - minimum for search'));
  192. }
  193.  
  194. }
  195.  
  196. /*
  197. * Alumni list form submit.
  198. */
  199. function alumni_users_list_form_submit($form, &$form_state) {
  200.  
  201. if ($form_state['clicked_button']['#id'] === 'edit-alumni-serch-reset') {
  202. unset($form_state['values']['alumni_search']);
  203. $form_state['input']['alumni_search'] = '';
  204. }
  205.  
  206. $form_state['rebuild'] = TRUE;
  207. }
  208.  
  209. /*
  210. * Implements hook_FORM_ID_form_alter().
  211. */
  212. function alumni_users_form_alter(&$form, &$form_state, $form_id) {
  213. global $user;
  214.  
  215. if($form_id == 'user_profile_form') {
  216. // hide alumni editing field group.
  217. if (!in_array('alumni', $user->roles) && !in_array('administrator', $user->roles)) {
  218. field_group_hide_field_groups($form, array('group_alumni_profile'));
  219. }
  220.  
  221. $form['field_branch_of_service_upd']['#states'] = array(
  222. 'visible' => array(':input[name="field_veteran[und]"]' => array('value' => '0'))
  223. );
  224. }
  225.  
  226. // Add form '#states'.
  227. if($form_id == 'alumni_user_profile_edit') {
  228. $form['field_branch_of_service_upd']['field_branch_of_service_upd']['#states'] = array(
  229. 'visible' => array(':input[name="field_veteran[und]"]' => array('value' => '0'))
  230. );
  231.  
  232. $form['field_branch_of_service_upd_hide']['#states'] = array(
  233. 'visible' => array(':input[name="field_veteran[und]"]' => array('value' => '0'))
  234. );
  235. }
  236. }
  237.  
  238. /*
  239. * Implements hook_user_load().
  240. */
  241. function alumni_users_user_load($users) {
  242. // add field_virtual_curriculum
  243. foreach ($users as $user_id => $user_obj) {
  244. $curriclum_list = db_select('contentenrollment', 'ce');
  245. $curriclum_list->leftJoin('curriculum', 'c', 'c.CurriculumID = ce.contentID');
  246.  
  247. $sql = $curriclum_list->fields('c', array('CurriculumID', 'Title'))
  248. ->condition('ce.userID', $user_obj->uid, '=')
  249. ->isNotNull('ce.CompletionDate')
  250. ->execute()
  251. ->fetchAllKeyed();
  252.  
  253. $user_obj->field_virtual_curriculum = $sql;
  254. }
  255. }
  256.  
  257. /**
  258. * Create alumni table array.
  259. */
  260. function alumni_users_create_alumni_list($users) {
  261.  
  262. $alumni_displays = array();
  263. foreach ($users as $current_alumni) {
  264.  
  265. $alumni_wrapper = entity_metadata_wrapper('user', $current_alumni);
  266. $name = $alumni_wrapper->name->value();
  267. $full_name = $alumni_wrapper->field_user_first_name->value() . ' ' . $alumni_wrapper->field_user_last_name->value();
  268. $veteran = $alumni_wrapper->field_veteran->value() ? 'No' : 'Yes';
  269. $branch_of_service = $alumni_wrapper->field_branch_of_service_upd->value();
  270. $business_name = $alumni_wrapper->field_business_name2->value();
  271. $website = $alumni_wrapper->field_website->value();
  272. $msglink = l(t('Send this user a private message'), 'messages/new/' . $alumni_wrapper->uid->value(), array('query' => array('destination' => 'user/' . $alumni_wrapper->uid->value())));
  273. $awards_credentials = $alumni_wrapper->field_awards_and_credentials->value();
  274.  
  275. //create curriculum list
  276. $curriculum_list = array();
  277. foreach ($current_alumni->field_virtual_curriculum as $curriculum_id => $current_curriculum) {
  278. $curriculum_sting = 'ID: ' . $curriculum_id . '; Title: ' . $current_curriculum;
  279. $curriculum_list[] = array('data' => $curriculum_sting);
  280. }
  281. $curriculums = theme('item_list', array('items' => $curriculum_list, 'title' => '', 'type' => 'ul', 'attributes' => array()));
  282.  
  283. $alumniorfellow = ($curriculum_list) ? 'Alumni' : 'Fellow';
  284.  
  285. $alumni_displays[] = theme('alumni_users_display', array(
  286. 'nickname' => $name,
  287. 'fullname' => $full_name,
  288. 'veteran' => $veteran,
  289. 'branch_service' => $branch_of_service,
  290. 'business_name' => $business_name,
  291. 'website' => $website,
  292. 'curriculums' => $curriculums,
  293. 'awards_credentials' => $awards_credentials,
  294. 'msglink' => $msglink,
  295. 'alumniorfellow' => $alumniorfellow,
  296. ));
  297. }
  298.  
  299. return $alumni_displays;
  300. }
  301.  
  302. /**
  303. * Get standart password user field.
  304. */
  305. function alumni_user_get_pass_field() {
  306. global $user;
  307.  
  308. module_load_include('inc', 'user', 'user.pages');
  309. $form = drupal_get_form('user_profile_form', $user);
  310.  
  311.  
  312. // $request_new = l(t('Request new password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));
  313. // $current_pass_description = t('Enter your current password to change the %pass. !request_new.', array('%pass' => t('Password'), '!request_new' => $request_new));
  314. // $form['account']['current_pass']['#description'] = $current_pass_description;
  315. //dsm($form);
  316. unset(
  317. $form['account']['name'],
  318. $form['account']['mail'],
  319. $form['account']['status'],
  320. $form['account']['roles'],
  321. $form['locale'],
  322. $form['l10n_client'],
  323. $form['picture'],
  324. $form['overlay_control'],
  325. $form['contact'],
  326. $form['timezone'],
  327. $form['ckeditor'],
  328. $form['metatags'],
  329. $form['redirect']
  330. );
  331.  
  332. // foreach ($form as $key => $val) {
  333. // if (
  334. // $key == '#user' ||
  335. // $key == '#validate' ||
  336. // $key == 'account' ||
  337. // $key == 'signature_settings' ||
  338. // $key == '#entity' ||
  339. // $key == '#pre_render' ||
  340. // $key == '#entity_type' ||
  341. // $key == '#bundle'
  342. // ) continue;
  343. //
  344. // unset($form[$key]);
  345. //}
  346. //dsm($form);
  347. return $form;
  348. }
  349. /**
  350. * Alumni profile user fields list.
  351. */
  352. function alumni_users_prifile_fields() {
  353. $form_fields = array(
  354. 'field_user_first_name' => array('title' => ''),
  355. 'field_user_last_name' => array('title' => ''),
  356. 'field_veteran' => array('title' => ''),
  357. 'field_branch_of_service_upd' => array('title' => ''),
  358. 'field_business_name2' => array('title' => ''),
  359. 'field_website' => array('title' => ''),
  360. 'field_home_number' => array('title' => ''),
  361. 'field_confirm_email' => array('title' => t('Email')),
  362. 'field_biography_and_descr' => array('title' => ''),
  363. 'field_awards_and_credentials' => array('title' => ''),
  364. 'field_related_links_' => array('title' => ''),
  365. );
  366.  
  367. return $form_fields;
  368. }
  369.  
  370. /**
  371. * Load form fields.
  372. */
  373. function alumni_users_load_field($field_name, $form, $form_state, $user, $title = '') {
  374. if (!$field_name) return FALSE;
  375.  
  376. $entity_type = 'user';
  377. $bundle_name = 'user';
  378. $field = field_info_field($field_name);
  379. $items = field_get_items($entity_type, $user, $field_name);
  380. $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  381. $form_field = field_default_form($entity_type, $user, $field, $instance, LANGUAGE_NONE, $items, $form, $form_state);
  382.  
  383. // Change default field title.
  384. if($title) {
  385. $value_key = isset($form_field[$field_name]['und'][0]['value']['#title']) ? 'value' : 'email';
  386. $form_field[$field_name]['und'][0][$value_key]['#title'] = $title;
  387. }
  388.  
  389. return $form_field;
  390. }
  391.  
  392. /**
  393. * Load user data.
  394. */
  395. function alumni_users_load_data($uid) {
  396. $data = array();
  397.  
  398. $user = user_load($uid);
  399. // Load fields list.
  400. $fields = array_keys(alumni_users_prifile_fields());
  401. // Load user entity.
  402. $wpr = entity_metadata_wrapper('user', $user);
  403.  
  404. foreach ($fields as $field) {
  405. // Get value from textarea field.
  406. if ($field == 'field_related_links_') {
  407. $data[$field] = $wpr->{$field}->value()['value'];
  408. continue;
  409. }
  410.  
  411. $data[$field] = $wpr->{$field}->value();
  412. }
  413.  
  414. return $data;
  415. }
  416.  
  417. /**
  418. * Load form fields visability settings.
  419. */
  420. function alumni_users_field_settings($uid) {
  421. $fields_settings = db_select('alumni_settings', 'als')
  422. ->fields('als', array('hide_settings'))
  423. ->condition('als.uid', $uid)
  424. ->execute()->fetchField();
  425.  
  426. return $fields_settings ? unserialize($fields_settings) : array();
  427. }
  428.  
  429. /**
  430. * View user profile.
  431. */
  432. function alumni_users_profile_view($uid) {
  433. if(!$uid) { return FALSE; }
  434. if(!is_numeric($uid)) { $uid = user_load_multiple(array(), array('name' => $uid))->uid; }
  435.  
  436. if(user_access('administer alumni profile page') || user_access('administer users')) {
  437. $data = alumni_users_load_data($uid);
  438. }
  439. else {
  440. // Hiding fields selected by the user.
  441. $fields_settings = alumni_users_field_settings($uid);
  442. $data = alumni_users_load_data($uid);
  443.  
  444. if(!empty($fields_settings) && !empty($data)) {
  445. foreach ($data as $field => &$val) {
  446. if(isset($fields_settings[$field]) && $fields_settings[$field] == 1) {
  447. $data[$field] = '';
  448. }
  449. }
  450. }
  451. }
  452.  
  453. $no_data = t('No information about the user');
  454. return isset($data) ? theme('alumni_users_profile_view', array('alumni_user' => $data)) : $no_data;
  455. }
  456.  
  457. /**
  458. * Edit user profile form.
  459. */
  460. function alumni_users_profile_edit($form, &$form_state, $uid) {
  461. $user = is_numeric($uid) ? user_load($uid) : user_load($GLOBALS['user']->uid);
  462. ///alumni_user_get_pass_field();
  463. // Alumni user profile fields.
  464. $form_fields = alumni_users_prifile_fields();
  465.  
  466. // Load fields visability settings.
  467. $fields_settings = alumni_users_field_settings($user->uid);
  468.  
  469. // Pass fields.
  470. // $pass = alumni_user_get_pass_field();
  471.  
  472. $form = array();
  473. $form['#tree'] = TRUE;
  474. $form['#parents'] = array();
  475. $form['#user'] = $user;
  476. $form['#user_category'] = 'register';
  477. $form['mail'] = array(
  478. '#type' => 'textfield',
  479. '#default_value' => $user->mail,
  480. );
  481.  
  482. user_account_form($form, $form_state);
  483.  
  484. // Dublicate element to parent array.
  485. $form['mail'] = $form['account']['mail'];
  486. $form['pass'] = $form['account']['pass'];
  487. //$form[''] = $form['account']['mail'];
  488. hide($form['mail']);
  489. hide($form['pass']);
  490.  
  491. // hide($form['account']['name']);
  492. hide($form['account']['mail']);
  493. hide($form['account']['roles']);
  494. hide($form['account']['status']);
  495. hide($form['picture']);
  496. $form1 = current($form['account']);
  497.  
  498.  
  499. $form['current_pass_required_values'] = $form['account']['current_pass_required_values'];
  500. // Load fields from user profile to this form.
  501. foreach ($form_fields as $field => $field_val) {
  502. $form[$field] = alumni_users_load_field($field, $form, $form_state, $user, $field_val['title']);
  503. $form[$field . '_hide'] = array(
  504. '#type' => 'checkboxes',
  505. '#title' => '',
  506. '#options' => array(1 => ''),
  507. '#default_value' => (isset($fields_settings[$field]) && $fields_settings[$field] == 1) ? array(1) : array(),
  508. );
  509. }
  510.  
  511. // array_splice($form, 3, 0, $pass);
  512. // $form += $pass;
  513. $form['submit'] = array(
  514. '#type' => 'submit',
  515. '#value' => 'Submit',
  516. );
  517.  
  518. return $form;
  519. }
  520.  
  521. /**
  522. * Edit user profile form submit.
  523. *
  524. *
  525. */
  526. function alumni_users_profile_edit_submit($form, &$form_state, $uid) {
  527. dsm($uid);
  528. $user = is_numeric($uid) ? user_load($uid) : user_load($GLOBALS['user']->uid);
  529.  
  530. // $form_mail = $form_state['values']['field_confirm_email']['field_confirm_email']['und'][0]['email'];
  531. // $user->mail = $form_mail;
  532.  
  533. // Change user password.
  534. if(isset($form_state['values']['account']['pass']) && !empty($form_state['values']['account']['pass'])) {
  535.  
  536. $pass = $form_state['values']['account']['pass'];
  537. // $edit['pass'] = 'New password';
  538. user_save($user, array('pass' => $pass));
  539. // require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
  540. // $user->pass = user_hash_password(trim($pass));
  541.  
  542. // // Recreate sessions.
  543. // drupal_session_destroy_uid($user->uid);
  544. // if ($user->uid == $GLOBALS['user']->uid) {
  545. // drupal_session_regenerate();
  546. // }
  547. }
  548.  
  549. dsm($user);
  550.  
  551. // Alumni user profile fields.
  552. $form_fields = array_keys(alumni_users_prifile_fields());
  553.  
  554. // Added form values to user object and getting visability settings.
  555. $visability = array();
  556. foreach ($form_fields as $field) {
  557. if (!isset($form_state['values'][$field][$field]['und'][0])) continue;
  558. $value_key = key($form_state['values'][$field][$field]['und'][0]);
  559. // Add new field values to user profile.
  560. $user->{$field}['und'][0][$value_key] = $form_state['values'][$field][$field]['und'][0][$value_key];
  561. // Creating profile fields visability settings.
  562. $visability[$field] = $form_state['values'][$field . '_hide'][1];
  563. }
  564. dsm('finish');
  565. dsm($user);
  566.  
  567. // Update user profile information.
  568. user_save($user, array());
  569.  
  570. // Saving fields visibility settings.
  571. db_merge('alumni_settings')
  572. ->key(array('uid' => $user->uid))
  573. ->fields(array('hide_settings' => serialize($visability)))
  574. ->execute();
  575.  
  576. drupal_set_message(t('The changes have been saved.'));
  577. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement