materio_user.module 24 KB

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