materio_user.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * Implements hook_init().
  4. */
  5. function materio_user_init() {
  6. drupal_add_js(drupal_get_path('module', 'materio_user').'/js/materio_user.min.js');
  7. }
  8. /**
  9. * Implements hook_permission().
  10. */
  11. function materio_user_permission() {
  12. return array(
  13. 'view own user profile' => array(
  14. 'title' => t('view own user profile'),
  15. 'description' => t('view own user profile'),
  16. ),
  17. );
  18. }
  19. function materio_user_menu(){
  20. $items = array();
  21. $base = array(
  22. 'type' => MENU_CALLBACK,
  23. 'file' => 'materio_user.pages.inc',
  24. );
  25. $items['materio_user/registerblock'] = $base+array(
  26. 'title' => 'Materio base user ajax',
  27. 'page callback' => 'materio_user_registerblock',
  28. // 'page arguments' => array(),
  29. 'access callback' => TRUE,
  30. );
  31. $items['materio_user/loginandregisterblock'] = $base+array(
  32. 'title' => 'Materio base user ajax',
  33. 'page callback' => 'materio_user_loginandregisterblock',
  34. // 'page arguments' => array(),
  35. 'access callback' => TRUE,
  36. );
  37. $items['materio_user/register/submit'] = $base+array(
  38. 'title' => 'Materio base user ajax',
  39. 'page callback' => 'materio_user_register_submit',
  40. // 'page arguments' => array(),
  41. 'access callback' => TRUE,
  42. );
  43. $items['materio_user/login/submit'] = $base+array(
  44. 'title' => 'Materio base user ajax',
  45. 'page callback' => 'materio_user_login_submit',
  46. // 'page arguments' => array(),
  47. 'access callback' => TRUE,
  48. );
  49. return $items;
  50. }
  51. /**
  52. * Implements hook_menu_alter().
  53. */
  54. function materio_user_menu_alter(&$items) {
  55. $items['user/%user']['access callback'] = 'user_access';
  56. $items['user/%user']['access arguments'] = array('view own user profile');
  57. }
  58. /**
  59. * Implements hook_block_info().
  60. */
  61. function materio_user_block_info() {
  62. $blocks['user_createaccount'] = array(
  63. 'info' => t('Create an account block'),
  64. 'cache' => DRUPAL_NO_CACHE
  65. );
  66. $blocks['user_register'] = array(
  67. 'info' => t('Register block'),
  68. 'cache' => DRUPAL_NO_CACHE
  69. );
  70. $blocks['old_database_link'] = array(
  71. 'info' => t('Old data base link'),
  72. 'cache' => DRUPAL_NO_CACHE
  73. );
  74. $blocks['front_link'] = array(
  75. 'info' => t('Front page link'),
  76. 'cache' => DRUPAL_NO_CACHE
  77. );
  78. return $blocks;
  79. }
  80. /**
  81. * Implements hook_block_view().
  82. */
  83. function materio_user_block_view($delta = '') {
  84. global $user, $language;
  85. $block = array();
  86. switch ($delta) {
  87. case 'user_createaccount':
  88. if(isset($user->roles[1])){
  89. $block['subject'] = '';
  90. if (drupal_is_front_page()) {
  91. $block['content'] .= '<h3>'. t('Create your materiO\' account') . '</h3>';
  92. $block['content'] .= l(t('Join us'), 'node/11187', array("attributes"=>array("class"=>array("join"))));
  93. }else{
  94. $block['content'] .= '<h3>'. t('Create your materiO\' account') . '</h3>';
  95. $block['content'] .= drupal_render(drupal_get_form('user_register_form'));
  96. }
  97. }
  98. break;
  99. case 'user_register':
  100. if(isset($user->roles[1])){
  101. $block['subject'] = '';//drupal_is_front_page() ? t('Your projects will born from here') : t('Create your materiO\' account');
  102. $block['content'] = '<h3>'. t('Login') . '</h3>';
  103. $ulog_form = drupal_get_form('user_login');
  104. $block['content'] .= drupal_render($ulog_form);
  105. if (drupal_is_front_page()) {
  106. $block['content'] .= '<h3>'. t('<span>or </span>create your materiO\' account') . '</h3>';
  107. $block['content'] .= l(t('Join us'), 'node/11187', array("attributes"=>array("class"=>array("join"))));
  108. }else{
  109. $block['content'] .= '<h3>'. t('<span>or </span>create your materiO\' account') . '</h3>';
  110. $block['content'] .= drupal_render(drupal_get_form('user_register_form'));
  111. }
  112. // $block['content'] .= l(t('Pricing'), 'node/11187', array('attributes' => array('class' => 'pricing'),));
  113. }
  114. break;
  115. case 'old_database_link':
  116. if( (isset($user->roles[6]) || isset($user->roles[8])) && $user->created < strtotime('01-12-2012') ){
  117. // dsm($user, 'user');
  118. $block['subject'] = '';
  119. $path = 'http://base.materio.com';
  120. if($language->language == 'fr')
  121. $path .= '/index_fr.html';
  122. $block['content'] = l(t('Old database'), $path);
  123. }
  124. break;
  125. case 'front_link':
  126. $block['subject'] = '';
  127. $block['content'] = l('<i class="icon-home"></i><span class="text">'.t('home').'</span>', '<front>', array('html'=>true));
  128. break;
  129. }
  130. return $block;
  131. }
  132. /**
  133. * Implements hook_form_alter().
  134. */
  135. function materio_user_form_alter(&$form, &$form_state, $form_id) {
  136. // dsm($form_id);
  137. if( $form_id == "user_register_form" && !user_access('administer users') ){
  138. // dsm($form);
  139. $form['account']['pass']['#type'] = 'password';
  140. $form['account']['pass']['#title'] = t('Password');
  141. $form['actions']['#type'] = "container";
  142. $form['actions']['submit']['#value'] = t('Join');
  143. // $form['termsofservices'] = array(
  144. // '#type' => 'checkbox',
  145. // '#title' => t('I accept') .' '. l(t('the materiO terms of services'), 'node/11183'),
  146. // '#required' => true,
  147. // );
  148. // $form['#submit'][] = "materio_user_user_register_form_submit";
  149. }
  150. if($form_id == "user_login" ){
  151. // dsm($form);
  152. $form['actions']['#type'] = "container";
  153. // $form['actions']['submit']['#value'] = t('Join');
  154. // if( $_GET['q'] == 'node/11187' ){
  155. // $form['#submit'][] = "materio_user_user_login_form_submit";
  156. // }
  157. }
  158. # https://drupal.org/comment/6293810#comment-6293810
  159. if( $form_id == "webform_client_form_11186" ){
  160. // dsm($form, '$form');
  161. $form['#validate'][] = "materio_user_webform_client_11186_validate";
  162. $form['#submit'][] = "materio_user_webform_client_11186_submit";
  163. }
  164. // if( $form_id == "user_profile_form" ){
  165. // //dsm($form, '$form');
  166. // $form['account']['pass']['#type'] = "new_password_confirm";
  167. // }
  168. }
  169. // function materio_user_user_register_form_submit($form, &$form_state){
  170. // dsm($form, 'form');
  171. // dsm($form_state, 'form_state');
  172. // dsm($_REQUEST, '$_REQUEST');
  173. // dsm($_GET, '$_GET');
  174. // unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
  175. // $form_state['redirect'] = array(
  176. // 'node/11187',
  177. // array(
  178. // 'fragment' => $_GET['q'] == 'node/11187' ? 'content-bottom' : '', // if we register from the membership page, then go directly to the form
  179. // )
  180. // );
  181. // $frontlink = l('continue with basic', '<front>');
  182. // // drupal_get_messages('status');
  183. // 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)));
  184. // }
  185. // function materio_user_user_login_form_submit($form, &$form_state){
  186. // unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
  187. // $form_state['redirect'] = array(
  188. // 'node/11187',
  189. // array(
  190. // 'fragment' => 'content-bottom', // if we login from the membership page, then go directly to the form
  191. // )
  192. // );
  193. // }
  194. function materio_user_webform_client_11186_validate($form, &$form_state){
  195. // dsm($form, 'form');
  196. // dsm($form_state, 'form_state');
  197. # test if collaborators are filled in case of membership option is 3
  198. $values = $form_state['values']['submitted'];
  199. if ($values['column_left']['membership_options'] == 3) {
  200. foreach ($values['collaborators'] as $coll_key => $coll) {
  201. foreach ($coll as $field_key => $field_value) {
  202. if($field_value == ''){
  203. $human_field_name = $form['submitted']['collaborators'][$coll_key]['#title'] . " : " . $form['submitted']['collaborators'][$coll_key][$field_key]['#title'];
  204. $field_name = 'submitted][collaborators]['.$coll_key.']['.$field_key;
  205. // dsm($field_name, 'field_name');
  206. // TODO : tester si les email ne sont pas deja inscrit sur le site
  207. form_set_error($field_name, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  208. }
  209. }
  210. }
  211. }
  212. }
  213. function materio_user_webform_client_11186_submit($form, &$form_state){
  214. // dsm($form, 'form');
  215. // dsm($form_state, 'form_state');
  216. $values = $form_state['values']['submitted_tree'];
  217. # redirect memebership form to checkout with good option selected
  218. $subscription_level = 3;//$values['membership_options'];
  219. // to redirect to 'node/123?foo=bar#baz':
  220. // http://www.ubercart.org/node/1427
  221. $form_state['redirect'] = array(
  222. 'cart/add/e-p11786_q1_a1o'.$subscription_level.'-membershipform',
  223. array(
  224. 'query' => array(
  225. 'destination' => 'cart/checkout',
  226. ),
  227. // 'fragment' => 'baz',
  228. ),
  229. );
  230. # create collaborators users account incase of membership option is 3
  231. /*
  232. if ($values['membership_options'] == 3) {
  233. $i = 1;
  234. foreach ($values['collaborators'] as $coll_key => $coll) {
  235. // foreach ($coll as $field_key => $field_value) {
  236. // if($field_value == ''){
  237. // $human_field_name = $form['submitted']['collaborators'][$coll_key]['#title'] . " : " . $form['submitted']['collaborators'][$coll_key][$field_key]['#title'];
  238. // $field_name = 'submitted][collaborators]['.$coll_key.']['.$field_key;
  239. // // dsm($field_name, 'field_name');
  240. // form_set_error($field_name, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  241. // }
  242. // }
  243. $userinfo = array(
  244. 'mail' => $coll['e_mail_collab_'.$i],
  245. 'name' => user_password(),
  246. 'pass' => user_password(), // Generate password
  247. // 'init' => $data['components']['username']['value'],
  248. 'status' => 0,
  249. 'access' => REQUEST_TIME,
  250. 'memo' => 'from webform',
  251. );
  252. $account = drupal_anonymous_user();
  253. $account->is_new = TRUE;
  254. user_save($account, $userinfo);
  255. // module_invoke_all('user_insert', $edit, $account);
  256. $i++;
  257. }
  258. }
  259. */
  260. // enregistrer le type d'adonnement chez les user
  261. }
  262. /**
  263. * Implements hook_help().
  264. */
  265. function materio_user_help($path, $arg) {
  266. // dsm($path, 'path');
  267. // dsm($arg, 'arg');
  268. switch ($path) {
  269. case 'node/%':
  270. if($arg[1] == 11187 ){
  271. global $user;
  272. // dsm($user, 'user');
  273. if(isset($user->roles[1])){ // anonyme
  274. $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());
  275. $content = '<p>' . $message . '</p>';
  276. return $content;
  277. }else if(isset($user->roles[2])){ // authentificated user (not utilisateur)
  278. $content = '<h2>' . t('Welcome, you just joined materiO\' !') . '</h2>';
  279. $frontlink = l(t('continue with basic'), '<front>');
  280. $message = t('Now you can choose the membership that\'s right for you, or !link', array('!link'=>$frontlink));
  281. $content .= '<p>' . $message . '</p>';
  282. return $content;
  283. }
  284. }
  285. }
  286. }
  287. /**
  288. * Implements hook_block_view_alter().
  289. */
  290. function materio_user_block_view_alter(&$data, $block) {
  291. if( $block->module == "user" && $block->delta == 'login' && isset($data['subject']) ){
  292. // dsm($block, 'block');
  293. // dsm($data, 'data');
  294. $data['subject'] = '<i class="icon-user"></i>' . '<span class="login">' . $data['subject'] . '</span>';
  295. }
  296. }
  297. /**
  298. * Define constants
  299. */
  300. define('MATERIO_USER_CONFIRMED_USER_ROLE', 'Utilisateur'); // add role name here
  301. /**
  302. * Implement hook_user
  303. */
  304. function _materio_user_user_update(&$edit, $account, $category){
  305. // dsm($edit, 'edit');
  306. // dsm($account, 'account');
  307. // dsm($category, 'category');
  308. // This is only fired when a user confirms their email address, logintoboggan style
  309. if (isset($account->logintoboggan_email_validated) && $account->logintoboggan_email_validated == TRUE) {
  310. $confirmed_rid = materio_user_get_role_by_name(MATERIO_USER_CONFIRMED_USER_ROLE);
  311. $roles = $account->roles + array($confirmed_rid => MATERIO_USER_CONFIRMED_USER_ROLE);
  312. // we have to do this to stop an infinite loop, and also to allow lower weighted modules to possibly do something here
  313. $user = $account;
  314. unset($user->logintoboggan_email_validated);
  315. user_save($user, array('roles' => $roles));
  316. drupal_go_to('user/'.$user->uid.'/edit');
  317. }
  318. }
  319. /**
  320. * Returns a role ID based on role name
  321. *
  322. * @param $name
  323. * name of role to return
  324. * @return
  325. * (int) Role ID
  326. */
  327. function materio_user_get_role_by_name($name) {
  328. return array_search($name, user_roles());
  329. }