materio_user.module 24 KB

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