materio_user.module 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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="fi-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. # dont fill default values if form is been rebuilded (by address module)
  212. if($form_state["rebuild"])
  213. return;
  214. global $user;
  215. // dsm($user, 'user');
  216. $member_profile = profile2_load_by_user($user, 'adherent');
  217. // dsm($member_profile, "member_profile");
  218. $columnright = &$form['submitted']['column_right'];
  219. $columnright['me']['firstname']['#default_value'] = $member_profile->field_first_name['und'][0]['value'];
  220. $columnright['me']['name']['#default_value'] = $member_profile->field_name['und'][0]['value'];
  221. $columnright['company']['administrative_e_mail']['#default_value'] = $member_profile->field_administrative_email['und'][0]['value'];
  222. $columnright['company']['s']['company']['#default_value'] = $member_profile->field_organization['und'][0]['value'];
  223. $columnright['company']['s']['activity_sector']['#default_value'] = $member_profile->field_activity_sector['und'][0]['value'];
  224. $columnright['company']['d']['vat_number_intra_ce']['#default_value'] = $member_profile->field_vat_number_intra_ce['und'][0]['value'];
  225. $columnright['company']['d']['website']['#default_value'] = $member_profile->field_user_website['und'][0]['url'];
  226. if( isset($member_profile->field_adresse['und'][0]) ){
  227. $address = $member_profile->field_adresse['und'][0];
  228. $formaddress = &$columnright['company']['location']['#address'];
  229. $formaddress['country'] = $address['country'];
  230. $formaddress['thoroughfare'] = $address['thoroughfare'];
  231. $formaddress['premise'] = $address['premise'];
  232. $formaddress['postal_code'] = $address['postal_code'];
  233. $formaddress['locality'] = $address['locality'];
  234. }
  235. $columnright['company']['phone_number']['#default_value'] = $member_profile->field_private_phone['und'][0]["number"];
  236. }
  237. function materio_user_webform_client_11186_validate($form, &$form_state){
  238. // dsm($form, 'form');
  239. // dsm($form_state, 'form_state');
  240. # test if collaborators are filled in case of membership option is 3
  241. $values = $form_state['values']['submitted'];
  242. if ($values['column_left']['membership_options'] == 3) {
  243. foreach ($values['column_right']['collaborators'] as $collab_key => $coll) {
  244. foreach ($coll as $field_key => $field_value) {
  245. $form_field = $form['submitted']['column_right']['collaborators'][$collab_key][$field_key];
  246. if($field_value == ''){
  247. $collab_label = $form['submitted']['column_right']['collaborators'][$collab_key]['#title'];
  248. $field_label = $form_field['#title'];
  249. $human_field_name = $collab_label. " : " .$field_label;
  250. form_error($form_field, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  251. }
  252. # validate the emails
  253. # see https://api.drupal.org/api/drupal/modules!user!user.module/function/user_account_form_validate/7
  254. if($form_field['#type'] == 'webform_email'){
  255. // dsm($form_field, 'form_field');
  256. $u = db_select('users')
  257. ->fields('users', array('uid'))
  258. // ->condition('uid', $account->uid, '<>')
  259. ->condition('mail', db_like($field_value), 'LIKE')
  260. ->range(0, 1)
  261. ->execute()->fetchField();
  262. if ((bool) $u ) {
  263. form_error($form_field, t('The e-mail address %email is already taken.', array('%email' => $field_value)));
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }
  270. #retreive from the web form info, record them into user profil
  271. function materio_user_webform_client_11186_submit(&$form, &$form_state){
  272. // dsm($form, 'form');
  273. // dsm($form_state, 'form_state');
  274. $node = &$form['#node'];
  275. // dsm($node, '$node');
  276. $values = $form_state['values']['submitted_tree'];
  277. // dsm($values, '$values');
  278. $flat_values = array();
  279. materio_user_flatenize_form_values($values, $flat_values);
  280. // dsm($flat_values, 'flat_values');
  281. # records values in member profile
  282. materio_user_record_member_profile($flat_values);
  283. # redirect membership form to checkout with good option selected
  284. if(user_access('access to online payment')){
  285. // forcer le vidage du panier
  286. uc_cart_empty(uc_cart_get_id());
  287. // fill the cart and redirect to checkout
  288. // cf : http://www.ubercart.org/node/1427
  289. $subscription_level = $flat_values['membership_options'];
  290. $form_state['redirect'] = array(
  291. 'cart/add/e-p11849_q1_a1o'.$subscription_level.'-membershipform',
  292. array(
  293. 'query' => array(
  294. 'destination' => 'cart/checkout',
  295. ),
  296. // 'fragment' => 'baz',
  297. ),
  298. );
  299. // TODO effacer le message de soumissions du formulaire -> pas moyen de overwriter le message de webform depuis ici, dommage
  300. // $node->webform['confirmation'] = '';
  301. # create collaborators users account incase of membership option is 3
  302. # NO wait for checkout for that
  303. //materio_user_create_collaborators_users();
  304. // enregistrer le type d'adonnement chez les user
  305. }
  306. }
  307. function materio_user_record_member_profile($values) {
  308. // dsm($values, 'values');
  309. global $user;
  310. // dsm($user, 'user');
  311. $member_profile = profile2_load_by_user($user, 'adherent');
  312. if (empty($member_profile)) {
  313. $member_profile = profile2_create(array("type" => 'adherent', "uid" => $user->uid));
  314. }
  315. // dsm($member_profile, '$member_profile');
  316. $correspondances = array(
  317. "firstname"=>"field_first_name",
  318. "name"=>"field_name",
  319. "company"=>"field_organization",
  320. "activity_sector"=>"field_activity_sector",
  321. "website"=>"field_user_website",
  322. "administrative_e_mail"=>"field_administrative_email",
  323. "vat_number_intra_ce"=>"field_vat_number_intra_ce",
  324. "location"=>"field_adresse",
  325. "phone_number"=>"field_private_phone",
  326. );
  327. foreach ($correspondances as $form_field => $field_name) {
  328. switch($form_field){
  329. case "website":
  330. $value = array("url" => $values[$form_field]);
  331. break;
  332. case "administrative_e_mail":
  333. $value = array("email" => $values[$form_field]);
  334. break;
  335. case "location":
  336. $value = unserialize($values[$form_field]);
  337. // dsm($value, 'location value');
  338. break;
  339. case "phone_number":
  340. $pn = $values[$form_field];
  341. if(module_exists('cck_phone')){
  342. preg_match('/^(\+[0-9]+)\s([0-9]+)$/', $pn, $matches);
  343. // dsm($matches, "matches");
  344. $ccs = cck_phone_countrycodes();
  345. foreach ($ccs as $c => $vs) {
  346. if($vs['code'] == $matches[1]){
  347. $pn = "0".$matches[2];
  348. $cc = $c;
  349. break;
  350. }
  351. }
  352. $value = array(
  353. "number"=>$pn,
  354. "country_codes"=>$cc,
  355. "extension"=>"",
  356. );
  357. }else{
  358. $value = array(
  359. "number"=>$pn,
  360. "country_codes"=>"",
  361. "extension"=>"",
  362. );
  363. }
  364. break;
  365. default:
  366. $value = array("value" => $values[$form_field]);
  367. break;
  368. }
  369. $member_profile->{$field_name} = array(
  370. "und"=>array( 0 => $value,)
  371. );
  372. }
  373. // save the profile2 new contents
  374. profile2_save($member_profile);
  375. // Indicate success to the user.
  376. // drupal_set_message(t('Your member profile has been created.'));
  377. }
  378. function materio_user_flatenize_form_values($ar_src, &$flat_ar){
  379. foreach ($ar_src as $key => $value) {
  380. if(is_array($value)){
  381. materio_user_flatenize_form_values($value, $flat_ar);
  382. }else{
  383. $flat_ar[$key] = $value;
  384. }
  385. }
  386. }
  387. /**
  388. * Implements hook_form_alter().
  389. */
  390. function materio_user_uc_cart_checkout_form_alter(&$form, &$form_state, $form_id) {
  391. // dsm($form_id, 'form_id');
  392. // dsm($form, "form");
  393. // dsm($form_state, "form_state");
  394. // Load an order from the session, if available.
  395. if (isset($_SESSION['cart_order'])) {
  396. $order = uc_order_load($_SESSION['cart_order']);
  397. if ($order) {
  398. // dsm($order, 'order');
  399. global $user;
  400. // dsm($user, 'user');
  401. $member_profile = profile2_load_by_user($user, 'adherent');
  402. // dsm($member_profile, "member_profile");
  403. $location = (object) $member_profile->field_adresse['und'][0];
  404. # convert country code to UC country ID (pfff)
  405. $countries = db_query("SELECT country_id, country_iso_code_2 FROM {uc_countries} WHERE version > :version", array(':version' => 0))->fetchAllKeyed();
  406. foreach ($countries as $country_id => $country_code) {
  407. if($location->country == $country_code){
  408. $location->uc_country_id = $country_id;
  409. break;
  410. }
  411. }
  412. // dsm($location, "location");
  413. // $phone_field = field_get_items('profile2', $member_profile, 'field_private_phone');
  414. // $phone = $phone_field ? field_view_value('profile2', $member_profile, 'field_private_phone', $phone_field[0]) : "";
  415. // dsm($phone, 'phone');
  416. $phone_field = $member_profile->field_private_phone['und'][0];
  417. if(module_exists('cck_phone')){
  418. $cc = cck_phone_countrycodes($phone_field['country_codes']);
  419. $phone = $cc['code'] .' '. $phone_field['number'];
  420. }
  421. $correspondances = array(
  422. "billing_first_name" => $member_profile->field_first_name['und'][0]['value'],
  423. "billing_last_name" => $member_profile->field_name['und'][0]['value'],
  424. "billing_phone" => $phone,
  425. "billing_company" => $member_profile->field_organization['und'][0]['value'],
  426. "billing_street1" => $location->thoroughfare,
  427. "billing_street2" => $location->premise,
  428. "billing_city" => $location->locality,
  429. // "billing_zone" => ,
  430. "billing_postal_code" => $location->postal_code,
  431. "billing_country" => $location->uc_country_id,
  432. );
  433. foreach ($correspondances as $order_field => $value) {
  434. $order->{$order_field} = $value;
  435. }
  436. $form['panes']['billing']['address']['#default_value'] = $order;
  437. }
  438. }
  439. }
  440. /**
  441. * Implements hook_uc_order();
  442. */
  443. function materio_user_order($op, $order, $arg2) {
  444. // dsm($op, "matyerio_user_uc_order | op");
  445. // dsm($order, "order");
  446. // dsm($arg2, "arg2");
  447. switch ($op) {
  448. case 'save':
  449. // Do something to save payment info!
  450. break;
  451. }
  452. }
  453. # not used yet
  454. function materio_user_create_collaborators_users(){
  455. /*
  456. if ($values['membership_options'] == 3) {
  457. $i = 1;
  458. foreach ($values['collaborators'] as $collab_key => $coll) {
  459. // foreach ($coll as $field_key => $field_value) {
  460. // if($field_value == ''){
  461. // $human_field_name = $form['submitted']['collaborators'][$coll_key]['#title'] . " : " . $form['submitted']['collaborators'][$coll_key][$field_key]['#title'];
  462. // $field_name = 'submitted][collaborators]['.$coll_key.']['.$field_key;
  463. // // dsm($field_name, 'field_name');
  464. // form_set_error($field_name, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  465. // }
  466. // }
  467. $userinfo = array(
  468. 'mail' => $coll['e_mail_collab_'.$i],
  469. 'name' => user_password(),
  470. 'pass' => user_password(), // Generate password
  471. // 'init' => $data['components']['username']['value'],
  472. 'status' => 0,
  473. 'access' => REQUEST_TIME,
  474. 'memo' => 'from webform',
  475. );
  476. $account = drupal_anonymous_user();
  477. $account->is_new = TRUE;
  478. user_save($account, $userinfo);
  479. // module_invoke_all('user_insert', $edit, $account);
  480. $i++;
  481. }
  482. }
  483. */
  484. }
  485. /**
  486. * Implements hook_help().
  487. */
  488. function materio_user_help($path, $arg) {
  489. // dsm($path, 'path');
  490. // dsm($arg, 'arg');
  491. switch ($path) {
  492. case 'node/%':
  493. if($arg[1] == 11187 ){
  494. global $user;
  495. // dsm($user, 'user');
  496. if(isset($user->roles[1])){ // anonyme
  497. $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());
  498. $content = '<p>' . $message . '</p>';
  499. return $content;
  500. }else if(isset($user->roles[2])){ // authentificated user (not utilisateur)
  501. $content = '<h2>' . t('Welcome, you just joined materiO\' !') . '</h2>';
  502. $frontlink = l(t('continue with basic'), '<front>');
  503. $message = t('Now you can choose the membership that\'s right for you, or !link', array('!link'=>$frontlink));
  504. $content .= '<p>' . $message . '</p>';
  505. return $content;
  506. }
  507. }
  508. }
  509. }
  510. /**
  511. * Implements hook_block_view_alter().
  512. */
  513. function materio_user_block_view_alter(&$data, $block) {
  514. if( $block->module == "user" && $block->delta == 'login' && isset($data['subject']) ){
  515. // dsm($block, 'block');
  516. // dsm($data, 'data');
  517. $data['subject'] = '<i class="icon-user"></i>' . '<span class="login">' . $data['subject'] . '</span>';
  518. }
  519. }
  520. /**
  521. * Define constants
  522. */
  523. define('MATERIO_USER_CONFIRMED_USER_ROLE', 'Utilisateur'); // add role name here
  524. /**
  525. * Implement hook_user
  526. */
  527. function _materio_user_user_update(&$edit, $account, $category){
  528. // dsm($edit, 'edit');
  529. // dsm($account, 'account');
  530. // dsm($category, 'category');
  531. // This is only fired when a user confirms their email address, logintoboggan style
  532. if (isset($account->logintoboggan_email_validated) && $account->logintoboggan_email_validated == TRUE) {
  533. $confirmed_rid = materio_user_get_role_by_name(MATERIO_USER_CONFIRMED_USER_ROLE);
  534. $roles = $account->roles + array($confirmed_rid => MATERIO_USER_CONFIRMED_USER_ROLE);
  535. // we have to do this to stop an infinite loop, and also to allow lower weighted modules to possibly do something here
  536. $user = $account;
  537. unset($user->logintoboggan_email_validated);
  538. user_save($user, array('roles' => $roles));
  539. drupal_go_to('user/'.$user->uid.'/edit');
  540. }
  541. }
  542. /**
  543. * Returns a role ID based on role name
  544. *
  545. * @param $name
  546. * name of role to return
  547. * @return
  548. * (int) Role ID
  549. */
  550. function materio_user_get_role_by_name($name) {
  551. return array_search($name, user_roles());
  552. }
  553. /**
  554. * Catch rejected email by mandrill, and remove them from simplenews lists
  555. *
  556. * @param array $result
  557. * Associative array containing the send result, including the status.
  558. */
  559. function materio_user_mandrill_mailsend_result($result) {
  560. $status = $result['status'];
  561. if ( in_array($status, array('rejected', 'invalid', 'error')) ) {
  562. $email = $result['email'];
  563. // remove email from pool
  564. module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
  565. simplenews_delete_spool(array('mail'=>$email));
  566. watchdog("materio_user", "@mail rejected by mandrill, removed from simplenews spool", array("@mail"=>$email));
  567. if ( in_array($status, array('invalid')) ) {
  568. // unsubscribe user if mail does not exists
  569. $subscriber = simplenews_subscriber_load_by_mail($email);
  570. if ($subscriber) {
  571. simplenews_subscriber_delete($subscriber);
  572. }
  573. // disable user
  574. $user = user_load_by_mail($email);
  575. // $user->status = 0;
  576. user_save($user, array('status'=>0));
  577. 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");
  578. }
  579. }
  580. }