materio_user.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. if (drupal_is_front_page()) {
  51. $block['content'] .= '<h3>'. t('<span>or </span>create your materiO\' account') . '</h3>';
  52. $block['content'] .= l(t('Join us'), 'node/11187', array("attributes"=>array("class"=>array("join"))));
  53. }else{
  54. $block['content'] .= '<h3>'. t('<span>or </span>create your materiO\' account') . '</h3>';
  55. $block['content'] .= drupal_render(drupal_get_form('user_register_form'));
  56. }
  57. // $block['content'] .= l(t('Pricing'), 'node/11187', array('attributes' => array('class' => 'pricing'),));
  58. }
  59. break;
  60. case 'old_database_link':
  61. if( (isset($user->roles[6]) || isset($user->roles[8])) && $user->created < strtotime('01-12-2012') ){
  62. // dsm($user, 'user');
  63. $block['subject'] = '';
  64. $path = 'http://base.materio.com';
  65. if($language->language == 'fr')
  66. $path .= '/index_fr.html';
  67. $block['content'] = l(t('Old database'), $path);
  68. }
  69. break;
  70. case 'front_link':
  71. $block['subject'] = '';
  72. $block['content'] = l('<i class="icon-home"></i><span class="text">'.t('home').'</span>', '<front>', array('html'=>true));
  73. break;
  74. }
  75. return $block;
  76. }
  77. /**
  78. * Implements hook_form_alter().
  79. */
  80. function materio_user_form_alter(&$form, &$form_state, $form_id) {
  81. // dsm($form_id);
  82. if( $form_id == "user_register_form" && !user_access('administer users') ){
  83. // dsm($form);
  84. $form['account']['pass']['#type'] = 'password';
  85. $form['account']['pass']['#title'] = t('Password');
  86. $form['actions']['#type'] = "container";
  87. $form['actions']['submit']['#value'] = t('Join');
  88. // $form['termsofservices'] = array(
  89. // '#type' => 'checkbox',
  90. // '#title' => t('I accept') .' '. l(t('the materiO terms of services'), 'node/11183'),
  91. // '#required' => true,
  92. // );
  93. $form['#submit'][] = "materio_user_user_register_form_submit";
  94. }
  95. if($form_id == "user_login" ){
  96. // dsm($form);
  97. $form['actions']['#type'] = "container";
  98. // $form['actions']['submit']['#value'] = t('Join');
  99. if( $_GET['q'] == 'node/11187' ){
  100. $form['#submit'][] = "materio_user_user_login_form_submit";
  101. }
  102. }
  103. if( $form_id == "webform_client_form_11186" ){
  104. // dsm($form, '$form');
  105. $form['#validate'][] = "materio_user_webform_client_111186_validate";
  106. // $form['#submit'][] = "materio_user_webform_client_111186_submit";
  107. }
  108. // if( $form_id == "user_profile_form" ){
  109. // //dsm($form, '$form');
  110. // $form['account']['pass']['#type'] = "new_password_confirm";
  111. // }
  112. }
  113. // function materio_user_element_info() {
  114. // return array(
  115. // "new_password_confirm" => array(
  116. // '#input' => TRUE,
  117. // '#process' => array('materio_user_process_new_password_confirm'),
  118. // '#theme_wrappers' => array('form_element'),
  119. // ),
  120. // );
  121. // }
  122. /**
  123. * Expand a password_confirm field into two text boxes.
  124. */
  125. // function materio_user_process_new_password_confirm($element) {
  126. // $element['pass1'] = array(
  127. // '#type' => 'password',
  128. // '#title' => t('New password'),
  129. // '#value' => empty($element['#value']) ? NULL : $element['#value']['pass1'],
  130. // '#required' => $element['#required'],
  131. // '#attributes' => array('class' => array('password-field')),
  132. // );
  133. // $element['pass2'] = array(
  134. // '#type' => 'password',
  135. // '#title' => t('Confirm password'),
  136. // '#value' => empty($element['#value']) ? NULL : $element['#value']['pass2'],
  137. // '#required' => $element['#required'],
  138. // '#attributes' => array('class' => array('password-confirm')),
  139. // );
  140. // $element['#element_validate'] = array('password_confirm_validate');
  141. // $element['#tree'] = TRUE;
  142. // if (isset($element['#size'])) {
  143. // $element['pass1']['#size'] = $element['pass2']['#size'] = $element['#size'];
  144. // }
  145. // return $element;
  146. // }
  147. function materio_user_user_register_form_submit($form, &$form_state){
  148. // dsm($form, 'form');
  149. // dsm($form_state, 'form_state');
  150. // dsm($_REQUEST, '$_REQUEST');
  151. // dsm($_GET, '$_GET');
  152. unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
  153. $form_state['redirect'] = array(
  154. 'node/11187',
  155. array(
  156. 'fragment' => $_GET['q'] == 'node/11187' ? 'content-bottom' : '', // if we register from the membership page, then go directly to the form
  157. )
  158. );
  159. // $frontlink = l('continue with basic', '<front>');
  160. // // drupal_get_messages('status');
  161. // 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)));
  162. }
  163. function materio_user_user_login_form_submit($form, &$form_state){
  164. unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
  165. $form_state['redirect'] = array(
  166. 'node/11187',
  167. array(
  168. 'fragment' => 'content-bottom', // if we login from the membership page, then go directly to the form
  169. )
  170. );
  171. }
  172. function materio_user_webform_client_111186_validate($form, &$form_state){
  173. // dsm($form, 'form');
  174. // dsm($form_state, 'form_state');
  175. $values = $form_state['values']['submitted'];
  176. if ($values['membership_options'] == 3) {
  177. foreach ($values['collaborators'] as $coll_key => $coll) {
  178. foreach ($coll as $field_key => $field_value) {
  179. if($field_value == ''){
  180. $human_field_name = $form['submitted']['collaborators'][$coll_key]['#title'] . " : " . $form['submitted']['collaborators'][$coll_key][$field_key]['#title'];
  181. $field_name = 'submitted][collaborators]['.$coll_key.']['.$field_key;
  182. // dsm($field_name, 'field_name');
  183. // TODO : tester si les email ne sont pas deja inscrit sur le site
  184. form_set_error($field_name, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  185. }
  186. }
  187. }
  188. }
  189. }
  190. // function materio_user_webform_client_111186_submit($form, &$form_state){
  191. // dsm($form, 'form');
  192. // dsm($form_state, 'form_state');
  193. // $values = $form_state['values']['submitted_tree'];
  194. // if ($values['membership_options'] == 3) {
  195. // $i = 1;
  196. // foreach ($values['collaborators'] as $coll_key => $coll) {
  197. // // foreach ($coll as $field_key => $field_value) {
  198. // // if($field_value == ''){
  199. // // $human_field_name = $form['submitted']['collaborators'][$coll_key]['#title'] . " : " . $form['submitted']['collaborators'][$coll_key][$field_key]['#title'];
  200. // // $field_name = 'submitted][collaborators]['.$coll_key.']['.$field_key;
  201. // // // dsm($field_name, 'field_name');
  202. // // form_set_error($field_name, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  203. // // }
  204. // // }
  205. // $userinfo = array(
  206. // 'mail' => $coll['e_mail_collab_'.$i],
  207. // 'name' => user_password(),
  208. // 'pass' => user_password(), // Generate password
  209. // // 'init' => $data['components']['username']['value'],
  210. // 'status' => 0,
  211. // 'access' => REQUEST_TIME,
  212. // 'memo' => 'from webform',
  213. // );
  214. // $account = drupal_anonymous_user();
  215. // $account->is_new = TRUE;
  216. // user_save($account, $userinfo);
  217. // // module_invoke_all('user_insert', $edit, $account);
  218. // $i++;
  219. // }
  220. // }
  221. // enregistrer le type d'adonnement chez les user
  222. // }
  223. /**
  224. * Implements hook_help().
  225. */
  226. function materio_user_help($path, $arg) {
  227. // dsm($path, 'path');
  228. // dsm($arg, 'arg');
  229. switch ($path) {
  230. case 'node/%':
  231. if($arg[1] == 11187 ){
  232. global $user;
  233. // dsm($user, 'user');
  234. if(isset($user->roles[1])){ // anonyme
  235. $message = t('Please create first a free account and join materio or log in with your existing account,<br />then we\'ll provide you our membership form.', array());
  236. $content = '<p>' . $message . '</p>';
  237. return $content;
  238. }else if(isset($user->roles[2])){ // authentificated user (not utilisateur)
  239. $content = '<h2>' . t('Welcome, you just joined materiO\' !') . '</h2>';
  240. $frontlink = l(t('continue with basic'), '<front>');
  241. $message = t('Now you can choose the membership that\'s right for you, or !link', array('!link'=>$frontlink));
  242. $content .= '<p>' . $message . '</p>';
  243. return $content;
  244. }
  245. }
  246. }
  247. }
  248. /**
  249. * Implements hook_block_view_alter().
  250. */
  251. function materio_user_block_view_alter(&$data, $block) {
  252. if( $block->module == "user" && $block->delta == 'login' && isset($data['subject']) ){
  253. // dsm($block, 'block');
  254. // dsm($data, 'data');
  255. $data['subject'] = '<i class="icon-user"></i>' . '<span class="login">' . $data['subject'] . '</span>';
  256. }
  257. }
  258. /**
  259. * Define constants
  260. */
  261. define('MATERIO_USER_CONFIRMED_USER_ROLE', 'Utilisateur'); // add role name here
  262. /**
  263. * Implement hook_user
  264. */
  265. function _materio_user_user_update(&$edit, $account, $category){
  266. // dsm($edit, 'edit');
  267. // dsm($account, 'account');
  268. // dsm($category, 'category');
  269. // This is only fired when a user confirms their email address, logintoboggan style
  270. if (isset($account->logintoboggan_email_validated) && $account->logintoboggan_email_validated == TRUE) {
  271. $confirmed_rid = materio_user_get_role_by_name(MATERIO_USER_CONFIRMED_USER_ROLE);
  272. $roles = $account->roles + array($confirmed_rid => MATERIO_USER_CONFIRMED_USER_ROLE);
  273. // we have to do this to stop an infinite loop, and also to allow lower weighted modules to possibly do something here
  274. $user = $account;
  275. unset($user->logintoboggan_email_validated);
  276. user_save($user, array('roles' => $roles));
  277. drupal_go_to('user/'.$user->uid.'/edit');
  278. }
  279. }
  280. /**
  281. * Returns a role ID based on role name
  282. *
  283. * @param $name
  284. * name of role to return
  285. * @return
  286. * (int) Role ID
  287. */
  288. function materio_user_get_role_by_name($name) {
  289. return array_search($name, user_roles());
  290. }