materio_user.module 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * Implements hook_permission().
  4. */
  5. function materio_user_permission() {
  6. return array(
  7. 'view own user profile' => array(
  8. 'title' => t('view own user profile'),
  9. 'description' => t('view own user profile'),
  10. ),
  11. );
  12. }
  13. /**
  14. * Implements hook_menu_alter().
  15. */
  16. function materio_user_menu_alter(&$items) {
  17. $items['user/%user']['access callback'] = 'user_access';
  18. $items['user/%user']['access arguments'] = array('view own user profile');
  19. }
  20. /**
  21. * Implements hook_block_info().
  22. */
  23. function materio_user_block_info() {
  24. $blocks['user_register'] = array(
  25. 'info' => t('Register block'),
  26. 'cache' => DRUPAL_NO_CACHE
  27. );
  28. $blocks['old_database_link'] = array(
  29. 'info' => t('Old data base link'),
  30. 'cache' => DRUPAL_NO_CACHE
  31. );
  32. $blocks['front_link'] = array(
  33. 'info' => t('Front page link'),
  34. 'cache' => DRUPAL_NO_CACHE
  35. );
  36. return $blocks;
  37. }
  38. /**
  39. * Implements hook_block_view().
  40. */
  41. function materio_user_block_view($delta = '') {
  42. global $user, $language;
  43. $block = array();
  44. switch ($delta) {
  45. case 'user_register':
  46. if(isset($user->roles[1])){
  47. $block['subject'] = '';//drupal_is_front_page() ? t('Your projects will born from here') : t('Create your materiO\' account');
  48. $block['content'] = '<h3>'. t('Login') . '</h3>';
  49. $block['content'] .= drupal_render(drupal_get_form('user_login'));
  50. $block['content'] .= '<h3>'. t('or create your materiO\' account') . '</h3>';
  51. $block['content'] .= drupal_render(drupal_get_form('user_register_form'));
  52. // $block['content'] .= l(t('Pricing'), 'node/11187', array('attributes' => array('class' => 'pricing'),));
  53. }
  54. break;
  55. case 'old_database_link':
  56. if( (isset($user->roles[6]) || isset($user->roles[8])) && $user->created < strtotime('01-12-2012') ){
  57. // dsm($user, 'user');
  58. $block['subject'] = '';
  59. $path = 'http://base.materio.com';
  60. if($language->language == 'fr')
  61. $path .= '/index_fr.html';
  62. $block['content'] = l(t('Old database'), $path);
  63. }
  64. break;
  65. case 'front_link':
  66. $block['subject'] = '';
  67. $block['content'] = l('<i class="icon-home"></i><span class="text">'.t('home').'</span>', '<front>', array('html'=>true));
  68. break;
  69. }
  70. return $block;
  71. }
  72. /**
  73. * Implements hook_form_alter().
  74. */
  75. function materio_user_form_alter(&$form, &$form_state, $form_id) {
  76. // dsm($form_id);
  77. if( $form_id == "user_register_form" && !user_access('administer users') ){
  78. // dsm($form);
  79. $form['account']['pass']['#type'] = 'password';
  80. $form['account']['pass']['#title'] = t('Password');
  81. $form['actions']['#type'] = "container";
  82. $form['actions']['submit']['#value'] = t('Join');
  83. // $form['termsofservices'] = array(
  84. // '#type' => 'checkbox',
  85. // '#title' => t('I accept') .' '. l(t('the materiO terms of services'), 'node/11183'),
  86. // '#required' => true,
  87. // );
  88. $form['#submit'][] = "materio_user_user_register_form_submit";
  89. }
  90. if($form_id == "user_login" ){
  91. // dsm($form);
  92. $form['actions']['#type'] = "container";
  93. // $form['actions']['submit']['#value'] = t('Join');
  94. if( $_GET['q'] == 'node/11187' ){
  95. $form['#submit'][] = "materio_user_user_login_form_submit";
  96. }
  97. }
  98. if( $form_id == "webform_client_form_11186" ){
  99. // dsm($form, '$form');
  100. $form['#validate'][] = "materio_user_webform_client_111186_validate";
  101. }
  102. }
  103. function materio_user_user_register_form_submit($form, &$form_state){
  104. // dsm($form, 'form');
  105. // dsm($form_state, 'form_state');
  106. // dsm($_REQUEST, '$_REQUEST');
  107. // dsm($_GET, '$_GET');
  108. unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
  109. $form_state['redirect'] = array(
  110. 'node/11187',
  111. array(
  112. 'fragment' => $_GET['q'] == 'node/11187' ? 'content-bottom' : '', // if we register from the membership page, then go directly to the form
  113. )
  114. );
  115. // $frontlink = l('continue with basic', '<front>');
  116. // // drupal_get_messages('status');
  117. // drupal_set_message(t('Welcome, you just join materiO\'! now you can choose the membership that\'s right for you, or !link', array('!link'=>$frontlink)));
  118. }
  119. function materio_user_user_login_form_submit($form, &$form_state){
  120. unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
  121. $form_state['redirect'] = array(
  122. 'node/11187',
  123. array(
  124. 'fragment' => 'content-bottom', // if we login from the membership page, then go directly to the form
  125. )
  126. );
  127. }
  128. function materio_user_webform_client_111186_validate($form, &$form_state){
  129. // dsm($form, 'form');
  130. // dsm($form_state, 'form_state');
  131. $values = $form_state['values']['submitted'];
  132. if ($values['membership_options'] == 3) {
  133. foreach ($values['collaborators'] as $coll_key => $coll) {
  134. foreach ($coll as $field_key => $field_value) {
  135. if($field_value == ''){
  136. $human_field_name = $form['submitted']['collaborators'][$coll_key]['#title'] . " : " . $form['submitted']['collaborators'][$coll_key][$field_key]['#title'];
  137. $field_name = 'submitted][collaborators]['.$coll_key.']['.$field_key;
  138. // dsm($field_name, 'field_name');
  139. form_set_error($field_name, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  140. }
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * Implements hook_help().
  147. */
  148. function materio_user_help($path, $arg) {
  149. // dsm($path, 'path');
  150. // dsm($arg, 'arg');
  151. switch ($path) {
  152. case 'node/%':
  153. if($arg[1] == 11187 ){
  154. global $user;
  155. // dsm($user, 'user');
  156. if(isset($user->roles[1])){ // anonyme
  157. $message = t('Please create an account and join materio, then we\'ll provide you the membership form.');
  158. $content = '<p>' . $message . '</p>';
  159. return $content;
  160. }else if(isset($user->roles[2])){ // authentificated user (not utilisateur)
  161. $content = '<h2>' . t('Welcome, you just joined materiO\' !') . '</h2>';
  162. $frontlink = l(t('continue with basic'), '<front>');
  163. $message = t('Now you can choose the membership that\'s right for you, or !link', array('!link'=>$frontlink));
  164. $content .= '<p>' . $message . '</p>';
  165. return $content;
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. * Implements hook_block_view_alter().
  172. */
  173. function materio_user_block_view_alter(&$data, $block) {
  174. if( $block->module == "user" && $block->delta == 'login' && isset($data['subject']) ){
  175. // dsm($block, 'block');
  176. // dsm($data, 'data');
  177. $data['subject'] = '<i class="icon-user"></i>' . $data['subject'];
  178. }
  179. }
  180. /**
  181. * Define constants
  182. */
  183. define('MATERIO_USER_CONFIRMED_USER_ROLE', 'Utilisateur'); // add role name here
  184. /**
  185. * Implement hook_user
  186. */
  187. function __materio_user_user_update(&$edit, $account, $category){
  188. // This is only fired when a user confirms their email address, logintoboggan style
  189. if (isset($account->logintoboggan_email_validated) && $account->logintoboggan_email_validated == TRUE) {
  190. $confirmed_rid = materio_user_get_role_by_name(MATERIO_USER_CONFIRMED_USER_ROLE);
  191. $roles = $account->roles + array($confirmed_rid => MATERIO_USER_CONFIRMED_USER_ROLE);
  192. // we have to do this to stop an infinite loop, and also to allow lower weighted modules to possibly do something here
  193. $user = $account;
  194. unset($user->logintoboggan_email_validated);
  195. user_save($user, array('roles' => $roles));
  196. drupal_go_to('user/'.$user->uid.'/edit');
  197. }
  198. }
  199. /**
  200. * Returns a role ID based on role name
  201. *
  202. * @param $name
  203. * name of role to return
  204. * @return
  205. * (int) Role ID
  206. */
  207. function materio_user_get_role_by_name($name) {
  208. return array_search($name, user_roles());
  209. }