Advertisement
Guest User

Untitled

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