materio_user.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. $values = $form_state['input']['submitted']; // code updated with webform module
  328. // dsm($values, '$values');
  329. $flat_values = array();
  330. materio_user_flatenize_form_values($values, $flat_values);
  331. // dsm($flat_values, 'flat_values');
  332. # records values in member profile
  333. materio_user_record_member_profile($flat_values);
  334. # redirect membership form to checkout with good option selected
  335. if(user_access('access to online payment')){
  336. // forcer le vidage du panier
  337. uc_cart_empty(uc_cart_get_id());
  338. // fill the cart and redirect to checkout
  339. // cf : http://www.ubercart.org/node/1427
  340. $subscription_level = $flat_values['membership_options'];
  341. $form_state['redirect'] = array(
  342. 'cart/add/e-p11849_q1_a1o'.$subscription_level.'-membershipform',
  343. array(
  344. 'query' => array(
  345. 'destination' => 'cart/checkout',
  346. ),
  347. // 'fragment' => 'baz',
  348. ),
  349. );
  350. // TODO effacer le message de soumissions du formulaire -> pas moyen de overwriter le message de webform depuis ici, dommage
  351. // $node->webform['confirmation'] = '';
  352. # create collaborators users account incase of membership option is 3
  353. # NO wait for checkout for that
  354. //materio_user_create_collaborators_users();
  355. // enregistrer le type d'adonnement chez les user
  356. }
  357. }
  358. function materio_user_record_member_profile($values) {
  359. // dsm($values, 'values');
  360. global $user;
  361. // dsm($user, 'user');
  362. $member_profile = profile2_load_by_user($user, 'adherent');
  363. if (empty($member_profile)) {
  364. $member_profile = profile2_create(array("type" => 'adherent', "uid" => $user->uid));
  365. }
  366. // dsm($member_profile, '$member_profile');
  367. // transform location values into field_address compatible
  368. $values['location'] = array(
  369. 'country' => $values['country'],
  370. 'thoroughfare' => $values['thoroughfare'],
  371. 'premise' => $values['premise'],
  372. 'postal_code' => $values['postal_code'],
  373. 'locality' => $values['locality'],
  374. 'administrative_area' => $values['administrative_area'],
  375. );
  376. $correspondances = array(
  377. "firstname"=>"field_first_name",
  378. "name"=>"field_name",
  379. "company"=>"field_organization",
  380. "activity_sector"=>"field_activity_sector",
  381. "website"=>"field_user_website",
  382. "administrative_e_mail"=>"field_administrative_email",
  383. "vat_number_intra_ce"=>"field_vat_number_intra_ce",
  384. "location"=>"field_adresse",
  385. "phone_number"=>"field_private_phone",
  386. );
  387. foreach ($correspondances as $form_field => $field_name) {
  388. switch($form_field){
  389. case "website":
  390. $value = array("url" => $values[$form_field]);
  391. break;
  392. case "administrative_e_mail":
  393. $value = array("email" => $values[$form_field]);
  394. break;
  395. case "location":
  396. // $value = unserialize($values[$form_field]);
  397. $value = $values[$form_field];
  398. // dsm($value, 'location value');
  399. break;
  400. case "phone_number":
  401. $pn = $values[$form_field];
  402. if(module_exists('cck_phone')){
  403. preg_match('/^(\+[0-9]+)\s([0-9]+)$/', $pn, $matches);
  404. // dsm($matches, "matches");
  405. $ccs = cck_phone_countrycodes();
  406. foreach ($ccs as $c => $vs) {
  407. if($vs['code'] == $matches[1]){
  408. $pn = "0".$matches[2];
  409. $cc = $c;
  410. break;
  411. }
  412. }
  413. $value = array(
  414. "number"=>$pn,
  415. "country_codes"=>$cc,
  416. "extension"=>"",
  417. );
  418. }else{
  419. $value = array(
  420. "number"=>$pn,
  421. "country_codes"=>"",
  422. "extension"=>"",
  423. );
  424. }
  425. break;
  426. default:
  427. $value = array("value" => $values[$form_field]);
  428. break;
  429. }
  430. $member_profile->{$field_name} = array(
  431. "und"=>array( 0 => $value,)
  432. );
  433. }
  434. // save the profile2 new contents
  435. profile2_save($member_profile);
  436. // Indicate success to the user.
  437. // drupal_set_message(t('Your member profile has been created.'));
  438. }
  439. function materio_user_flatenize_form_values($ar_src, &$flat_ar){
  440. foreach ($ar_src as $key => $value) {
  441. if(is_array($value)){
  442. materio_user_flatenize_form_values($value, $flat_ar);
  443. }else{
  444. $flat_ar[$key] = $value;
  445. }
  446. }
  447. }
  448. function materio_user_uc_cart_checkout_start($order) {
  449. // dsm($order);
  450. $user = user_load($order->uid);
  451. // dsm($user, 'user');
  452. $member_profile = profile2_load_by_user($user, 'adherent');
  453. // dsm($member_profile, "member_profile");
  454. $location = (object) $member_profile->field_adresse['und'][0];
  455. # convert country code to UC country ID (pfff)
  456. $countries = db_query("SELECT country_id, country_iso_code_2 FROM {uc_countries} WHERE version > :version", array(':version' => 0))->fetchAllKeyed();
  457. foreach ($countries as $country_id => $country_code) {
  458. if($location->country == $country_code){
  459. $location->uc_country_id = $country_id;
  460. break;
  461. }
  462. }
  463. $correspondances = array(
  464. "billing_country" => $location->uc_country_id,
  465. );
  466. // dsm($correspondances);
  467. foreach ($correspondances as $order_field => $value) {
  468. $order->{$order_field} = $value;
  469. }
  470. }
  471. /**
  472. * Implements hook_form_alter().
  473. */
  474. function materio_user_uc_cart_checkout_form_alter(&$form, &$form_state, $form_id) {
  475. // dsm($form_id, 'form_id');
  476. // dsm($form, "form");
  477. // dsm($form_state, "form_state");
  478. // Load an order from the session, if available.
  479. if (isset($_SESSION['cart_order'])) {
  480. $order = uc_order_load($_SESSION['cart_order']);
  481. if ($order) {
  482. // dsm($order, 'order');
  483. global $user;
  484. // dsm($user, 'user');
  485. $member_profile = profile2_load_by_user($user, 'adherent');
  486. // dsm($member_profile, "member_profile");
  487. $location = (object) $member_profile->field_adresse['und'][0];
  488. # convert country code to UC country ID (pfff)
  489. $countries = db_query("SELECT country_id, country_iso_code_2 FROM {uc_countries} WHERE version > :version", array(':version' => 0))->fetchAllKeyed();
  490. foreach ($countries as $country_id => $country_code) {
  491. if($location->country == $country_code){
  492. $location->uc_country_id = $country_id;
  493. break;
  494. }
  495. }
  496. // dsm($location, "location");
  497. // $phone_field = field_get_items('profile2', $member_profile, 'field_private_phone');
  498. // $phone = $phone_field ? field_view_value('profile2', $member_profile, 'field_private_phone', $phone_field[0]) : "";
  499. // dsm($phone, 'phone');
  500. $phone_field = $member_profile->field_private_phone['und'][0];
  501. if(module_exists('cck_phone')){
  502. $cc = cck_phone_countrycodes($phone_field['country_codes']);
  503. $phone = $cc['code'] .' '. $phone_field['number'];
  504. }
  505. $correspondances = array(
  506. "billing_first_name" => $member_profile->field_first_name['und'][0]['value'],
  507. "billing_last_name" => $member_profile->field_name['und'][0]['value'],
  508. "billing_phone" => $phone,
  509. "billing_company" => $member_profile->field_organization['und'][0]['value'],
  510. "billing_street1" => $location->thoroughfare,
  511. "billing_street2" => $location->premise,
  512. "billing_city" => $location->locality,
  513. "billing_zone" => $location->adminstrative_area,
  514. "billing_postal_code" => $location->postal_code,
  515. "billing_country" => $location->uc_country_id,
  516. );
  517. // dsm($correspondances);
  518. foreach ($correspondances as $order_field => $value) {
  519. $order->{$order_field} = $value;
  520. }
  521. $form['panes']['billing']['address']['#default_value'] = $order;
  522. }
  523. }
  524. }
  525. /**
  526. * Implements hook_uc_order();
  527. */
  528. function materio_user_order($op, $order, $arg2) {
  529. // dsm($op, "matyerio_user_uc_order | op");
  530. // dsm($order, "order");
  531. // dsm($arg2, "arg2");
  532. switch ($op) {
  533. case 'save':
  534. // Do something to save payment info!
  535. break;
  536. }
  537. }
  538. # not used yet
  539. function materio_user_create_collaborators_users(){
  540. /*
  541. if ($values['membership_options'] == 3) {
  542. $i = 1;
  543. foreach ($values['collaborators'] as $collab_key => $coll) {
  544. // foreach ($coll as $field_key => $field_value) {
  545. // if($field_value == ''){
  546. // $human_field_name = $form['submitted']['collaborators'][$coll_key]['#title'] . " : " . $form['submitted']['collaborators'][$coll_key][$field_key]['#title'];
  547. // $field_name = 'submitted][collaborators]['.$coll_key.']['.$field_key;
  548. // // dsm($field_name, 'field_name');
  549. // form_set_error($field_name, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
  550. // }
  551. // }
  552. $userinfo = array(
  553. 'mail' => $coll['e_mail_collab_'.$i],
  554. 'name' => user_password(),
  555. 'pass' => user_password(), // Generate password
  556. // 'init' => $data['components']['username']['value'],
  557. 'status' => 0,
  558. 'access' => REQUEST_TIME,
  559. 'memo' => 'from webform',
  560. );
  561. $account = drupal_anonymous_user();
  562. $account->is_new = TRUE;
  563. user_save($account, $userinfo);
  564. // module_invoke_all('user_insert', $edit, $account);
  565. $i++;
  566. }
  567. }
  568. */
  569. }
  570. /**
  571. * Implements hook_help().
  572. */
  573. function materio_user_help($path, $arg) {
  574. // dsm($path, 'path');
  575. // dsm($arg, 'arg');
  576. switch ($path) {
  577. case 'node/%':
  578. if($arg[1] == 11187 ){
  579. global $user;
  580. // dsm($user, 'user');
  581. if(isset($user->roles[1])){ // anonyme
  582. $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());
  583. $content = '<p>' . $message . '</p>';
  584. return $content;
  585. }else if(isset($user->roles[2])){ // authentificated user (not utilisateur)
  586. $content = '<h2>' . t('Welcome, you just joined materiO\' !') . '</h2>';
  587. $frontlink = l(t('continue with basic'), '<front>');
  588. $message = t('Now you can choose the membership that\'s right for you, or !link', array('!link'=>$frontlink));
  589. $content .= '<p>' . $message . '</p>';
  590. return $content;
  591. }
  592. }
  593. }
  594. }
  595. /**
  596. * Implements hook_block_view_alter().
  597. */
  598. function materio_user_block_view_alter(&$data, $block) {
  599. if( $block->module == "user" && $block->delta == 'login' && isset($data['subject']) ){
  600. // dsm($block, 'block');
  601. // dsm($data, 'data');
  602. $data['subject'] = '<i class="icon-user"></i>' . '<span class="login">' . $data['subject'] . '</span>';
  603. }
  604. }
  605. /**
  606. * Define constants
  607. */
  608. define('MATERIO_USER_CONFIRMED_USER_ROLE', 'Utilisateur'); // add role name here
  609. /**
  610. * Implement hook_user
  611. */
  612. function _materio_user_user_update(&$edit, $account, $category){
  613. // dsm($edit, 'edit');
  614. // dsm($account, 'account');
  615. // dsm($category, 'category');
  616. // This is only fired when a user confirms their email address, logintoboggan style
  617. if (isset($account->logintoboggan_email_validated) && $account->logintoboggan_email_validated == TRUE) {
  618. $confirmed_rid = materio_user_get_role_by_name(MATERIO_USER_CONFIRMED_USER_ROLE);
  619. $roles = $account->roles + array($confirmed_rid => MATERIO_USER_CONFIRMED_USER_ROLE);
  620. // we have to do this to stop an infinite loop, and also to allow lower weighted modules to possibly do something here
  621. $user = $account;
  622. unset($user->logintoboggan_email_validated);
  623. user_save($user, array('roles' => $roles));
  624. drupal_go_to('user/'.$user->uid.'/edit');
  625. }
  626. }
  627. /**
  628. * Returns a role ID based on role name
  629. *
  630. * @param $name
  631. * name of role to return
  632. * @return
  633. * (int) Role ID
  634. */
  635. function materio_user_get_role_by_name($name) {
  636. return array_search($name, user_roles());
  637. }
  638. /**
  639. * Catch rejected email by mandrill, and remove them from simplenews lists
  640. *
  641. * @param array $result
  642. * Associative array containing the send result, including the status.
  643. */
  644. function materio_user_mandrill_mailsend_result($result) {
  645. $status = $result['status'];
  646. if ( in_array($status, array('rejected', 'invalid', 'error')) ) {
  647. $email = $result['email'];
  648. // remove email from pool
  649. module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
  650. simplenews_delete_spool(array('mail'=>$email));
  651. watchdog("materio_user", "@mail rejected by mandrill, removed from simplenews spool", array("@mail"=>$email));
  652. if ( in_array($status, array('invalid')) ) {
  653. // unsubscribe user if mail does not exists
  654. $subscriber = simplenews_subscriber_load_by_mail($email);
  655. if ($subscriber) {
  656. simplenews_subscriber_delete($subscriber);
  657. }
  658. // disable user
  659. $user = user_load_by_mail($email);
  660. // $user->status = 0;
  661. user_save($user, array('status'=>0));
  662. 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");
  663. }
  664. }
  665. }