materio_user.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. 'access to online payment' => array(
  18. 'title' => t('access to online payment'),
  19. 'description' => t('Access to online payment'),
  20. ),
  21. );
  22. }
  23. function materio_user_menu(){
  24. $items = array();
  25. $base = array(
  26. 'type' => MENU_CALLBACK,
  27. 'file' => 'materio_user.pages.inc',
  28. );
  29. $items['materio_user/registerblock'] = $base+array(
  30. 'title' => 'Materio base user ajax',
  31. 'page callback' => 'materio_user_registerblock',
  32. // 'page arguments' => array(),
  33. 'access callback' => TRUE,
  34. );
  35. $items['materio_user/loginandregisterblock'] = $base+array(
  36. 'title' => 'Materio base user ajax',
  37. 'page callback' => 'materio_user_loginandregisterblock',
  38. // 'page arguments' => array(),
  39. 'access callback' => TRUE,
  40. );
  41. $items['materio_user/register/submit'] = $base+array(
  42. 'title' => 'Materio base user ajax',
  43. 'page callback' => 'materio_user_register_submit',
  44. // 'page arguments' => array(),
  45. 'access callback' => TRUE,
  46. );
  47. $items['materio_user/login/submit'] = $base+array(
  48. 'title' => 'Materio base user ajax',
  49. 'page callback' => 'materio_user_login_submit',
  50. // 'page arguments' => array(),
  51. 'access callback' => TRUE,
  52. );
  53. return $items;
  54. }
  55. /**
  56. * Implements hook_menu_alter().
  57. */
  58. function materio_user_menu_alter(&$items) {
  59. $items['user/%user']['access callback'] = 'user_access';
  60. $items['user/%user']['access arguments'] = array('view own user profile');
  61. }
  62. /**
  63. * Implements hook_block_info().
  64. */
  65. function materio_user_block_info() {
  66. $blocks['user_createaccount'] = array(
  67. 'info' => t('Create an account block'),
  68. 'cache' => DRUPAL_NO_CACHE
  69. );
  70. $blocks['user_register'] = array(
  71. 'info' => t('Register block'),
  72. 'cache' => DRUPAL_NO_CACHE
  73. );
  74. $blocks['old_database_link'] = array(
  75. 'info' => t('Old data base link'),
  76. 'cache' => DRUPAL_NO_CACHE
  77. );
  78. $blocks['front_link'] = array(
  79. 'info' => t('Front page link'),
  80. 'cache' => DRUPAL_NO_CACHE
  81. );
  82. return $blocks;
  83. }
  84. /**
  85. * Implements hook_block_view().
  86. */
  87. function materio_user_block_view($delta = '') {
  88. global $user, $language;
  89. $block = array();
  90. switch ($delta) {
  91. case 'user_createaccount':
  92. if(isset($user->roles[1])){
  93. $block['subject'] = '';
  94. if (drupal_is_front_page()) {
  95. $block['content'] .= '<h3>'. t('Create your materiO\' account') . '</h3>';
  96. $block['content'] .= l(t('Join us'), 'node/11187', array("attributes"=>array("class"=>array("join"))));
  97. }else{
  98. $block['content'] .= '<h3>'. t('Create your materiO\' account') . '</h3>';
  99. $block['content'] .= drupal_render(drupal_get_form('user_register_form'));
  100. }
  101. }
  102. break;
  103. case 'user_register':
  104. if(isset($user->roles[1])){
  105. $block['subject'] = '';//drupal_is_front_page() ? t('Your projects will born from here') : t('Create your materiO\' account');
  106. $block['content'] = '<h3>'. t('Login') . '</h3>';
  107. $ulog_form = drupal_get_form('user_login');
  108. $block['content'] .= drupal_render($ulog_form);
  109. if (drupal_is_front_page()) {
  110. $block['content'] .= '<h3>'. t('<span>or </span>create your materiO\' account') . '</h3>';
  111. $block['content'] .= l(t('Join us'), 'node/11187', array("attributes"=>array("class"=>array("join"))));
  112. }else{
  113. $block['content'] .= '<h3>'. t('<span>or </span>create your materiO\' account') . '</h3>';
  114. $block['content'] .= drupal_render(drupal_get_form('user_register_form'));
  115. }
  116. // $block['content'] .= l(t('Pricing'), 'node/11187', array('attributes' => array('class' => 'pricing'),));
  117. }
  118. break;
  119. case 'old_database_link':
  120. if( (isset($user->roles[6]) || isset($user->roles[8])) && $user->created < strtotime('01-12-2012') ){
  121. // dsm($user, 'user');
  122. $block['subject'] = '';
  123. $path = 'http://base.materio.com';
  124. if($language->language == 'fr')
  125. $path .= '/index_fr.html';
  126. $block['content'] = l(t('Old database'), $path);
  127. }
  128. break;
  129. case 'front_link':
  130. $block['subject'] = '';
  131. $block['content'] = l('<i class="icon-home"></i><span class="text">'.t('home').'</span>', '<front>', array('html'=>true));
  132. break;
  133. }
  134. return $block;
  135. }
  136. /**
  137. * Implements hook_form_alter().
  138. */
  139. function materio_user_form_alter(&$form, &$form_state, $form_id) {
  140. // dsm($form_id);
  141. if( $form_id == "user_register_form" && !user_access('administer users') ){
  142. // dsm($form);
  143. $form['account']['pass']['#type'] = 'password';
  144. $form['account']['pass']['#title'] = t('Password');
  145. $form['actions']['#type'] = "container";
  146. $form['actions']['submit']['#value'] = t('Join');
  147. // $form['termsofservices'] = array(
  148. // '#type' => 'checkbox',
  149. // '#title' => t('I accept') .' '. l(t('the materiO terms of services'), 'node/11183'),
  150. // '#required' => true,
  151. // );
  152. // $form['#submit'][] = "materio_user_user_register_form_submit";
  153. }
  154. if($form_id == "user_login" ){
  155. // dsm($form);
  156. $form['actions']['#type'] = "container";
  157. // $form['actions']['submit']['#value'] = t('Join');
  158. // if( $_GET['q'] == 'node/11187' ){
  159. // $form['#submit'][] = "materio_user_user_login_form_submit";
  160. // }
  161. }
  162. # https://drupal.org/comment/6293810#comment-6293810
  163. if( $form_id == "webform_client_form_11186" ){
  164. // dsm($form, '$form');
  165. materio_user_webform_client_11186_form_alter($form, $form_state, $form_id);
  166. }
  167. if( $form_id == "uc_cart_checkout_form" ){
  168. materio_user_uc_cart_checkout_form_alter($form, $form_state, $form_id);
  169. }
  170. // if( $form_id == "user_profile_form" ){
  171. // //dsm($form, '$form');
  172. // $form['account']['pass']['#type'] = "new_password_confirm";
  173. // }
  174. }
  175. // function materio_user_user_register_form_submit($form, &$form_state){
  176. // dsm($form, 'form');
  177. // dsm($form_state, 'form_state');
  178. // dsm($_REQUEST, '$_REQUEST');
  179. // dsm($_GET, '$_GET');
  180. // unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
  181. // $form_state['redirect'] = array(
  182. // 'node/11187',
  183. // array(
  184. // 'fragment' => $_GET['q'] == 'node/11187' ? 'content-bottom' : '', // if we register from the membership page, then go directly to the form
  185. // )
  186. // );
  187. // $frontlink = l('continue with basic', '<front>');
  188. // // drupal_get_messages('status');
  189. // 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)));
  190. // }
  191. // function materio_user_user_login_form_submit($form, &$form_state){
  192. // unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
  193. // $form_state['redirect'] = array(
  194. // 'node/11187',
  195. // array(
  196. // 'fragment' => 'content-bottom', // if we login from the membership page, then go directly to the form
  197. // )
  198. // );
  199. // }
  200. /**
  201. * subscription webform (nid : 11186)
  202. */
  203. # prefill the webform form with user profil values
  204. # add validate and submit functions
  205. function materio_user_webform_client_11186_form_alter(&$form, &$form_state, $form_id){
  206. // dsm($form_id, 'form_id');
  207. // dsm($form, "form");
  208. // dsm($form_state, "form_state");
  209. $form['#validate'][] = "materio_user_webform_client_11186_validate";
  210. $form['#submit'][] = "materio_user_webform_client_11186_submit";
  211. global $user;
  212. // dsm($user, 'user');
  213. $member_profile = profile2_load_by_user($user, 'adherent');
  214. // dsm($member_profile, "member_profile");
  215. $columnright = &$form['submitted']['column_right'];
  216. $columnright['me']['firstname']['#default_value'] = $member_profile->field_first_name['und'][0]['value'];
  217. $columnright['me']['name']['#default_value'] = $member_profile->field_name['und'][0]['value'];
  218. $columnright['company']['administrative_e_mail']['#default_value'] = $member_profile->field_administrative_email['und'][0]['value'];
  219. $columnright['company']['s']['company']['#default_value'] = $member_profile->field_organization['und'][0]['value'];
  220. $columnright['company']['s']['activity_sector']['#default_value'] = $member_profile->field_activity_sector['und'][0]['value'];
  221. $columnright['company']['d']['vat_number_intra_ce']['#default_value'] = $member_profile->field_vat_number_intra_ce['und'][0]['value'];
  222. $columnright['company']['d']['website']['#default_value'] = $member_profile->field_user_website['und'][0]['url'];
  223. if( isset($member_profile->field_adresse['und'][0]) ){
  224. $address = $member_profile->field_adresse['und'][0];
  225. $formaddress = &$columnright['company']['location']['#address'];
  226. $formaddress['country'] = $address['country'];
  227. $formaddress['thoroughfare'] = $address['thoroughfare'];
  228. $formaddress['premise'] = $address['premise'];
  229. $formaddress['postal_code'] = $address['postal_code'];
  230. $formaddress['locality'] = $address['locality'];
  231. }
  232. }
  233. function materio_user_webform_client_11186_validate($form, &$form_state){
  234. // dsm($form, 'form');
  235. // dsm($form_state, 'form_state');
  236. # test if collaborators are filled in case of membership option is 3
  237. $values = $form_state['values']['submitted'];
  238. if ($values['column_left']['membership_options'] == 3) {
  239. foreach ($values['column_right']['collaborators'] as $collab_key => $coll) {
  240. foreach ($coll as $field_key => $field_value) {
  241. $form_field = $form['submitted']['column_right']['collaborators'][$collab_key][$field_key];
  242. if($field_value == ''){
  243. $collab_label = $form['submitted']['column_right']['collaborators'][$collab_key]['#title'];
  244. $field_label = $form_field['#title'];
  245. $human_field_name = $collab_label. " : " .$field_label;
  246. form_error($form_field, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  247. }
  248. # validate the emails
  249. # see https://api.drupal.org/api/drupal/modules!user!user.module/function/user_account_form_validate/7
  250. if($form_field['#type'] == 'webform_email'){
  251. // dsm($form_field, 'form_field');
  252. $u = db_select('users')
  253. ->fields('users', array('uid'))
  254. // ->condition('uid', $account->uid, '<>')
  255. ->condition('mail', db_like($field_value), 'LIKE')
  256. ->range(0, 1)
  257. ->execute()->fetchField();
  258. if ((bool) $u ) {
  259. form_error($form_field, t('The e-mail address %email is already taken.', array('%email' => $field_value)));
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. #retreive from the web form info, record them into user profil
  267. function materio_user_webform_client_11186_submit(&$form, &$form_state){
  268. // dsm($form, 'form');
  269. // dsm($form_state, 'form_state');
  270. $node = &$form['#node'];
  271. // dsm($node, '$node');
  272. $values = $form_state['values']['submitted_tree'];
  273. // dsm($values, '$values');
  274. $flat_values = array();
  275. materio_user_flatenize_form_values($values, $flat_values);
  276. // dsm($flat_values, 'flat_values');
  277. # records values in member profile
  278. materio_user_record_member_profile($flat_values);
  279. # redirect membership form to checkout with good option selected
  280. if(user_access('access to online payment')){
  281. // forcer le vidage du panier
  282. uc_cart_empty(uc_cart_get_id());
  283. // fill the cart and redirect to checkout
  284. // cf : http://www.ubercart.org/node/1427
  285. $subscription_level = $flat_values['membership_options'];
  286. $form_state['redirect'] = array(
  287. 'cart/add/e-p11849_q1_a1o'.$subscription_level.'-membershipform',
  288. array(
  289. 'query' => array(
  290. 'destination' => 'cart/checkout',
  291. ),
  292. // 'fragment' => 'baz',
  293. ),
  294. );
  295. // TODO effacer le message de soumissions du formulaire -> pas moyen de overwriter le message de webform depuis ici, dommage
  296. // $node->webform['confirmation'] = '';
  297. # create collaborators users account incase of membership option is 3
  298. # NO wait for checkout for that
  299. //materio_user_create_collaborators_users();
  300. // enregistrer le type d'adonnement chez les user
  301. }
  302. }
  303. function materio_user_record_member_profile($values) {
  304. // dsm($values, 'values');
  305. global $user;
  306. // dsm($user, 'user');
  307. $member_profile = profile2_load_by_user($user, 'adherent');
  308. if (empty($member_profile)) {
  309. $member_profile = profile2_create(array("type" => 'adherent', "uid" => $user->uid));
  310. }
  311. // dsm($member_profile, '$member_profile');
  312. $correspondances = array(
  313. "firstname"=>"field_first_name",
  314. "name"=>"field_name",
  315. "company"=>"field_organization",
  316. "activity_sector"=>"field_activity_sector",
  317. "website"=>"field_user_website",
  318. "administrative_e_mail"=>"field_administrative_email",
  319. "vat_number_intra_ce"=>"field_vat_number_intra_ce",
  320. "location"=>"field_adresse",
  321. );
  322. foreach ($correspondances as $form_field => $field_name) {
  323. switch($form_field){
  324. case "website":
  325. $value = array("url" => $values[$form_field]);
  326. break;
  327. case "administrative_e_mail":
  328. $value = array("email" => $values[$form_field]);
  329. break;
  330. case "location":
  331. $value = unserialize($values[$form_field]);
  332. // dsm($value, 'location value');
  333. break;
  334. default:
  335. $value = array("value" => $values[$form_field]);
  336. break;
  337. }
  338. $member_profile->{$field_name} = array(
  339. "und"=>array( 0 => $value,)
  340. );
  341. }
  342. // save the profile2 new contents
  343. profile2_save($member_profile);
  344. // Indicate success to the user.
  345. // drupal_set_message(t('Your member profile has been created.'));
  346. }
  347. function materio_user_flatenize_form_values($ar_src, &$flat_ar){
  348. foreach ($ar_src as $key => $value) {
  349. if(is_array($value)){
  350. materio_user_flatenize_form_values($value, $flat_ar);
  351. }else{
  352. $flat_ar[$key] = $value;
  353. }
  354. }
  355. }
  356. /**
  357. * Implements hook_form_alter().
  358. */
  359. function materio_user_uc_cart_checkout_form_alter(&$form, &$form_state, $form_id) {
  360. // dsm($form_id, 'form_id');
  361. // dsm($form, "form");
  362. // dsm($form_state, "form_state");
  363. // Load an order from the session, if available.
  364. if (isset($_SESSION['cart_order'])) {
  365. $order = uc_order_load($_SESSION['cart_order']);
  366. if ($order) {
  367. // dsm($order, 'order');
  368. global $user;
  369. // dsm($user, 'user');
  370. $member_profile = profile2_load_by_user($user, 'adherent');
  371. // dsm($member_profile, "member_profile");
  372. $location = (object) $member_profile->field_adresse['und'][0];
  373. // dsm($location, "location");
  374. $phone_field = field_get_items('profile2', $member_profile, 'field_private_phone');
  375. $phone = $phone_field ? field_view_value('profile2', $member_profile, 'field_private_phone', $phone_field[0]) : "";
  376. $correspondances = array(
  377. "billing_first_name" => $member_profile->field_first_name['und'][0]['value'],
  378. "billing_last_name" => $member_profile->field_name['und'][0]['value'],
  379. "billing_phone" => $phone,
  380. "billing_company" => $member_profile->field_organization['und'][0]['value'],
  381. "billing_street1" => $location->thoroughfare,
  382. "billing_street2" => $location->premise,
  383. "billing_city" => $location->locality,
  384. // "billing_zone" => ,
  385. "billing_postal_code" => $location->postal_code,
  386. "billing_country" => $location->country,
  387. );
  388. foreach ($correspondances as $order_field => $value) {
  389. $order->{$order_field} = $value;
  390. }
  391. $form['panes']['billing']['address']['#default_value'] = $order;
  392. }
  393. }
  394. }
  395. /**
  396. * Implements hook_uc_order();
  397. */
  398. function materio_user_order($op, $order, $arg2) {
  399. // dsm($op, "matyerio_user_uc_order | op");
  400. // dsm($order, "order");
  401. // dsm($arg2, "arg2");
  402. switch ($op) {
  403. case 'save':
  404. // Do something to save payment info!
  405. break;
  406. }
  407. }
  408. # not used yet
  409. function materio_user_create_collaborators_users(){
  410. /*
  411. if ($values['membership_options'] == 3) {
  412. $i = 1;
  413. foreach ($values['collaborators'] as $collab_key => $coll) {
  414. // foreach ($coll as $field_key => $field_value) {
  415. // if($field_value == ''){
  416. // $human_field_name = $form['submitted']['collaborators'][$coll_key]['#title'] . " : " . $form['submitted']['collaborators'][$coll_key][$field_key]['#title'];
  417. // $field_name = 'submitted][collaborators]['.$coll_key.']['.$field_key;
  418. // // dsm($field_name, 'field_name');
  419. // form_set_error($field_name, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  420. // }
  421. // }
  422. $userinfo = array(
  423. 'mail' => $coll['e_mail_collab_'.$i],
  424. 'name' => user_password(),
  425. 'pass' => user_password(), // Generate password
  426. // 'init' => $data['components']['username']['value'],
  427. 'status' => 0,
  428. 'access' => REQUEST_TIME,
  429. 'memo' => 'from webform',
  430. );
  431. $account = drupal_anonymous_user();
  432. $account->is_new = TRUE;
  433. user_save($account, $userinfo);
  434. // module_invoke_all('user_insert', $edit, $account);
  435. $i++;
  436. }
  437. }
  438. */
  439. }
  440. /**
  441. * Implements hook_help().
  442. */
  443. function materio_user_help($path, $arg) {
  444. // dsm($path, 'path');
  445. // dsm($arg, 'arg');
  446. switch ($path) {
  447. case 'node/%':
  448. if($arg[1] == 11187 ){
  449. global $user;
  450. // dsm($user, 'user');
  451. if(isset($user->roles[1])){ // anonyme
  452. $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());
  453. $content = '<p>' . $message . '</p>';
  454. return $content;
  455. }else if(isset($user->roles[2])){ // authentificated user (not utilisateur)
  456. $content = '<h2>' . t('Welcome, you just joined materiO\' !') . '</h2>';
  457. $frontlink = l(t('continue with basic'), '<front>');
  458. $message = t('Now you can choose the membership that\'s right for you, or !link', array('!link'=>$frontlink));
  459. $content .= '<p>' . $message . '</p>';
  460. return $content;
  461. }
  462. }
  463. }
  464. }
  465. /**
  466. * Implements hook_block_view_alter().
  467. */
  468. function materio_user_block_view_alter(&$data, $block) {
  469. if( $block->module == "user" && $block->delta == 'login' && isset($data['subject']) ){
  470. // dsm($block, 'block');
  471. // dsm($data, 'data');
  472. $data['subject'] = '<i class="icon-user"></i>' . '<span class="login">' . $data['subject'] . '</span>';
  473. }
  474. }
  475. /**
  476. * Define constants
  477. */
  478. define('MATERIO_USER_CONFIRMED_USER_ROLE', 'Utilisateur'); // add role name here
  479. /**
  480. * Implement hook_user
  481. */
  482. function _materio_user_user_update(&$edit, $account, $category){
  483. // dsm($edit, 'edit');
  484. // dsm($account, 'account');
  485. // dsm($category, 'category');
  486. // This is only fired when a user confirms their email address, logintoboggan style
  487. if (isset($account->logintoboggan_email_validated) && $account->logintoboggan_email_validated == TRUE) {
  488. $confirmed_rid = materio_user_get_role_by_name(MATERIO_USER_CONFIRMED_USER_ROLE);
  489. $roles = $account->roles + array($confirmed_rid => MATERIO_USER_CONFIRMED_USER_ROLE);
  490. // we have to do this to stop an infinite loop, and also to allow lower weighted modules to possibly do something here
  491. $user = $account;
  492. unset($user->logintoboggan_email_validated);
  493. user_save($user, array('roles' => $roles));
  494. drupal_go_to('user/'.$user->uid.'/edit');
  495. }
  496. }
  497. /**
  498. * Returns a role ID based on role name
  499. *
  500. * @param $name
  501. * name of role to return
  502. * @return
  503. * (int) Role ID
  504. */
  505. function materio_user_get_role_by_name($name) {
  506. return array_search($name, user_roles());
  507. }