array(
'title' => t('view own user profile'),
'description' => t('view own user profile'),
),
'access to online payment' => array(
'title' => t('access to online payment'),
'description' => t('Access to online payment'),
),
'access to user export' => array(
'title' => t('access to user export'),
'description' => t('Access to user export'),
),
);
$simplenews_cats = simplenews_category_list();
// dsm($simplenews_cats);
foreach ($simplenews_cats as $tid => $name) {
$perms["subscribe to simplenews category $tid"] = array(
'title' => t("subscribe to simplenews category $name"),
// 'description' => t('Access to online payment'),
);
}
return $perms;
}
function materio_user_menu(){
$items = array();
$base = array(
'type' => MENU_CALLBACK,
'file' => 'materio_user.pages.inc',
);
$items['materio_user/registerblock'] = $base+array(
'title' => 'Materio base user ajax',
'page callback' => 'materio_user_registerblock',
// 'page arguments' => array(),
'access callback' => TRUE,
);
$items['materio_user/loginandregisterblock'] = $base+array(
'title' => 'Materio base user ajax',
'page callback' => 'materio_user_loginandregisterblock',
// 'page arguments' => array(),
'access callback' => TRUE,
);
$items['materio_user/register/submit'] = $base+array(
'title' => 'Materio base user ajax',
'page callback' => 'materio_user_register_submit',
// 'page arguments' => array(),
'access callback' => TRUE,
);
$items['materio_user/login/submit'] = $base+array(
'title' => 'Materio base user ajax',
'page callback' => 'materio_user_login_submit',
// 'page arguments' => array(),
'access callback' => TRUE,
);
$items['admin/materio_user/export'] = $base+array(
'title' => 'Materio base user export',
'page callback' => 'materio_user_export',
// 'page arguments' => array(),
'access arguments' => array('access to user export'),
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function materio_user_menu_alter(&$items) {
$items['user/%user']['access callback'] = 'user_access';
$items['user/%user']['access arguments'] = array('view own user profile');
}
/**
* Implements hook_block_info().
*/
function materio_user_block_info() {
$blocks['user_createaccount'] = array(
'info' => t('Create an account block'),
'cache' => DRUPAL_NO_CACHE
);
$blocks['user_register'] = array(
'info' => t('Register block'),
'cache' => DRUPAL_NO_CACHE
);
$blocks['old_database_link'] = array(
'info' => t('Old data base link'),
'cache' => DRUPAL_NO_CACHE
);
$blocks['front_link'] = array(
'info' => t('Front page link'),
'cache' => DRUPAL_NO_CACHE
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function materio_user_block_view($delta = '') {
global $user, $language;
$block = array();
switch ($delta) {
case 'user_createaccount':
if(isset($user->roles[1])){
$block['subject'] = '';
if (drupal_is_front_page()) {
$block['content'] .= '
'. t('Create your materiO\' account') . '
';
$block['content'] .= l(t('Join us'), 'node/11187', array("attributes"=>array("class"=>array("join"))));
}else{
$block['content'] .= ''. t('Create your materiO\' account') . '
';
$block['content'] .= drupal_render(drupal_get_form('user_register_form'));
}
}
break;
case 'user_register':
if(isset($user->roles[1])){
$block['subject'] = '';//drupal_is_front_page() ? t('Your projects will born from here') : t('Create your materiO\' account');
$block['content'] = ''. t('Login') . '
';
$ulog_form = drupal_get_form('user_login');
$block['content'] .= drupal_render($ulog_form);
if (drupal_is_front_page()) {
$block['content'] .= ''. t('or create your materiO\' account') . '
';
$block['content'] .= l(t('Join us'), 'node/11187', array("attributes"=>array("class"=>array("join"))));
}else{
$block['content'] .= ''. t('or create your materiO\' account') . '
';
$block['content'] .= drupal_render(drupal_get_form('user_register_form'));
}
// $block['content'] .= l(t('Pricing'), 'node/11187', array('attributes' => array('class' => 'pricing'),));
}
break;
case 'old_database_link':
if( (isset($user->roles[6]) || isset($user->roles[8])) && $user->created < strtotime('01-12-2012') ){
// dsm($user, 'user');
$block['subject'] = '';
$path = 'http://base.materio.com';
if($language->language == 'fr')
$path .= '/index_fr.html';
$block['content'] = l(t('Old database'), $path);
}
break;
case 'front_link':
$block['subject'] = '';
$block['content'] = l(''.t('home').'', '', array('html'=>true));
break;
}
return $block;
}
/**
* Implements hook_module_implements_alter().
*/
function materio_user_module_implements_alter(&$implementations, $hook){
if ($hook == 'form_alter') {
// It is to ensure that hook_form_alter of this module is called
// after all other hooks
$group = $implementations['materio_user'];
if (isset($group)) {
unset($implementations['materio_user']);
// Then add it back, so it will be at the end of hook array
$implementations['materio_user'] = $group;
}
}
}
/**
* Implements hook_form_alter().
*/
function materio_user_form_alter(&$form, &$form_state, $form_id) {
// dsm($form_id);
if( $form_id == "user_register_form" && !user_access('administer users') ){
// dsm($form);
$form['account']['pass']['#type'] = 'password';
$form['account']['pass']['#title'] = t('Password');
$form['actions']['#type'] = "container";
$form['actions']['submit']['#value'] = t('Join');
}
if($form_id == "user_login" ){
// dsm($form);
$form['actions']['#type'] = "container";
}
# https://drupal.org/comment/6293810#comment-6293810
if( $form_id == "webform_client_form_11186" ){
// dsm($form, '$form');
materio_user_webform_client_11186_form_alter($form, $form_state, $form_id);
}
if( $form_id == "uc_cart_checkout_form" ){
materio_user_uc_cart_checkout_form_alter($form, $form_state, $form_id);
}
// remove newsletters category if user doesn't have access to them
if( $form_id == "user_profile_form" ){
// dsm($form, '$form');
$options = array();
foreach ($form['subscriptions']['newsletters']['#options'] as $tid => $name) {
if(user_access("subscribe to simplenews category $tid")){
$options[$tid] = $name;
}
}
$form['subscriptions']['newsletters']['#options'] = $options;
}
// remove newsletters category if user doesn't have access to them
if ( $form_id == "simplenews_subscriptions_multi_block_form"){
// dsm($form);
$options = array();
foreach ($form['newsletters']['#options'] as $tid => $name) {
if(user_access("subscribe to simplenews category $tid")){
$options[$tid] = $name;
}
}
$form['newsletters']['#options'] = $options;
}
// if ( $form_id == "user_profile_form"){
// dsm($form);
// }
}
// function materio_user_user_register_form_submit($form, &$form_state){
// dsm($form, 'form');
// dsm($form_state, 'form_state');
// dsm($_REQUEST, '$_REQUEST');
// dsm($_GET, '$_GET');
// unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
// $form_state['redirect'] = array(
// 'node/11187',
// array(
// 'fragment' => $_GET['q'] == 'node/11187' ? 'content-bottom' : '', // if we register from the membership page, then go directly to the form
// )
// );
// $frontlink = l('continue with basic', '');
// // drupal_get_messages('status');
// 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)));
// }
// function materio_user_user_login_form_submit($form, &$form_state){
// unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
// $form_state['redirect'] = array(
// 'node/11187',
// array(
// 'fragment' => 'content-bottom', // if we login from the membership page, then go directly to the form
// )
// );
// }
/*
_ _ _ _
___ _ _| |_ ___ ___ ___|_|___| |_|_|___ ___
|_ -| | | . |_ -| _| _| | . | _| | . | |
|___|___|___|___|___|_| |_| _|_| |_|___|_|_|
|_|
//(nid : 11186)
*/
# prefill the webform form with user profil values
# add validate and submit functions
function materio_user_webform_client_11186_form_alter(&$form, &$form_state, $form_id){
// dsm($form_id, 'form_id');
// dsm($form, "form");
// dsm($form_state, "form_state");
$form['#validate'][] = "materio_user_webform_client_11186_validate";
$form['#submit'][] = "materio_user_webform_client_11186_submit";
# dont fill default values if form is been rebuilded (by address module)
if($form_state["rebuild"])
return;
global $user;
// dsm($user, 'user');
$member_profile = profile2_load_by_user($user, 'adherent');
// dsm($member_profile, "member_profile");
$columnright = &$form['submitted']['column_right'];
$columnright['me']['firstname']['#default_value'] = $member_profile->field_first_name['und'][0]['value'];
$columnright['me']['name']['#default_value'] = $member_profile->field_name['und'][0]['value'];
$columnright['company']['administrative_e_mail']['#default_value'] = $member_profile->field_administrative_email['und'][0]['value'];
$columnright['company']['s']['company']['#default_value'] = $member_profile->field_organization['und'][0]['value'];
$columnright['company']['s']['activity_sector']['#default_value'] = $member_profile->field_activity_sector['und'][0]['value'];
$columnright['company']['d']['vat_number_intra_ce']['#default_value'] = $member_profile->field_vat_number_intra_ce['und'][0]['value'];
$columnright['company']['d']['website']['#default_value'] = $member_profile->field_user_website['und'][0]['url'];
if( isset($member_profile->field_adresse['und'][0]) ){
$address = $member_profile->field_adresse['und'][0];
$formaddress = &$columnright['company']['location']['#address'];
$formaddress['country'] = $address['country'];
$formaddress['thoroughfare'] = $address['thoroughfare'];
$formaddress['premise'] = $address['premise'];
$formaddress['postal_code'] = $address['postal_code'];
$formaddress['locality'] = $address['locality'];
}
$columnright['company']['phone_number']['#default_value'] = $member_profile->field_private_phone['und'][0]["number"];
}
function materio_user_webform_client_11186_validate($form, &$form_state){
// dsm($form, 'form');
// dsm($form_state, 'form_state');
# test if collaborators are filled in case of membership option is 3
$values = $form_state['values']['submitted'];
if ($values['column_left']['membership_options'] == 3) {
foreach ($values['column_right']['collaborators'] as $collab_key => $coll) {
foreach ($coll as $field_key => $field_value) {
$form_field = $form['submitted']['column_right']['collaborators'][$collab_key][$field_key];
if($field_value == ''){
$collab_label = $form['submitted']['column_right']['collaborators'][$collab_key]['#title'];
$field_label = $form_field['#title'];
$human_field_name = $collab_label. " : " .$field_label;
form_error($form_field, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
}
# validate the emails
# see https://api.drupal.org/api/drupal/modules!user!user.module/function/user_account_form_validate/7
if($form_field['#type'] == 'webform_email'){
// dsm($form_field, 'form_field');
$u = db_select('users')
->fields('users', array('uid'))
// ->condition('uid', $account->uid, '<>')
->condition('mail', db_like($field_value), 'LIKE')
->range(0, 1)
->execute()->fetchField();
if ((bool) $u ) {
form_error($form_field, t('The e-mail address %email is already taken.', array('%email' => $field_value)));
}
}
}
}
}
}
#retreive from the web form info, record them into user profil
function materio_user_webform_client_11186_submit(&$form, &$form_state){
// dsm($form, 'form');
// dsm($form_state, 'form_state');
$node = &$form['#node'];
// dsm($node, '$node');
$values = $form_state['values']['submitted_tree'];
// dsm($values, '$values');
$flat_values = array();
materio_user_flatenize_form_values($values, $flat_values);
// dsm($flat_values, 'flat_values');
# records values in member profile
materio_user_record_member_profile($flat_values);
# redirect membership form to checkout with good option selected
if(user_access('access to online payment')){
// forcer le vidage du panier
uc_cart_empty(uc_cart_get_id());
// fill the cart and redirect to checkout
// cf : http://www.ubercart.org/node/1427
$subscription_level = $flat_values['membership_options'];
$form_state['redirect'] = array(
'cart/add/e-p11849_q1_a1o'.$subscription_level.'-membershipform',
array(
'query' => array(
'destination' => 'cart/checkout',
),
// 'fragment' => 'baz',
),
);
// TODO effacer le message de soumissions du formulaire -> pas moyen de overwriter le message de webform depuis ici, dommage
// $node->webform['confirmation'] = '';
# create collaborators users account incase of membership option is 3
# NO wait for checkout for that
//materio_user_create_collaborators_users();
// enregistrer le type d'adonnement chez les user
}
}
function materio_user_record_member_profile($values) {
// dsm($values, 'values');
global $user;
// dsm($user, 'user');
$member_profile = profile2_load_by_user($user, 'adherent');
if (empty($member_profile)) {
$member_profile = profile2_create(array("type" => 'adherent', "uid" => $user->uid));
}
// dsm($member_profile, '$member_profile');
$correspondances = array(
"firstname"=>"field_first_name",
"name"=>"field_name",
"company"=>"field_organization",
"activity_sector"=>"field_activity_sector",
"website"=>"field_user_website",
"administrative_e_mail"=>"field_administrative_email",
"vat_number_intra_ce"=>"field_vat_number_intra_ce",
"location"=>"field_adresse",
"phone_number"=>"field_private_phone",
);
foreach ($correspondances as $form_field => $field_name) {
switch($form_field){
case "website":
$value = array("url" => $values[$form_field]);
break;
case "administrative_e_mail":
$value = array("email" => $values[$form_field]);
break;
case "location":
$value = unserialize($values[$form_field]);
// dsm($value, 'location value');
break;
case "phone_number":
$pn = $values[$form_field];
if(module_exists('cck_phone')){
preg_match('/^(\+[0-9]+)\s([0-9]+)$/', $pn, $matches);
// dsm($matches, "matches");
$ccs = cck_phone_countrycodes();
foreach ($ccs as $c => $vs) {
if($vs['code'] == $matches[1]){
$pn = "0".$matches[2];
$cc = $c;
break;
}
}
$value = array(
"number"=>$pn,
"country_codes"=>$cc,
"extension"=>"",
);
}else{
$value = array(
"number"=>$pn,
"country_codes"=>"",
"extension"=>"",
);
}
break;
default:
$value = array("value" => $values[$form_field]);
break;
}
$member_profile->{$field_name} = array(
"und"=>array( 0 => $value,)
);
}
// save the profile2 new contents
profile2_save($member_profile);
// Indicate success to the user.
// drupal_set_message(t('Your member profile has been created.'));
}
function materio_user_flatenize_form_values($ar_src, &$flat_ar){
foreach ($ar_src as $key => $value) {
if(is_array($value)){
materio_user_flatenize_form_values($value, $flat_ar);
}else{
$flat_ar[$key] = $value;
}
}
}
/**
* Implements hook_form_alter().
*/
function materio_user_uc_cart_checkout_form_alter(&$form, &$form_state, $form_id) {
// dsm($form_id, 'form_id');
// dsm($form, "form");
// dsm($form_state, "form_state");
// Load an order from the session, if available.
if (isset($_SESSION['cart_order'])) {
$order = uc_order_load($_SESSION['cart_order']);
if ($order) {
// dsm($order, 'order');
global $user;
// dsm($user, 'user');
$member_profile = profile2_load_by_user($user, 'adherent');
// dsm($member_profile, "member_profile");
$location = (object) $member_profile->field_adresse['und'][0];
# convert country code to UC country ID (pfff)
$countries = db_query("SELECT country_id, country_iso_code_2 FROM {uc_countries} WHERE version > :version", array(':version' => 0))->fetchAllKeyed();
foreach ($countries as $country_id => $country_code) {
if($location->country == $country_code){
$location->uc_country_id = $country_id;
break;
}
}
// dsm($location, "location");
// $phone_field = field_get_items('profile2', $member_profile, 'field_private_phone');
// $phone = $phone_field ? field_view_value('profile2', $member_profile, 'field_private_phone', $phone_field[0]) : "";
// dsm($phone, 'phone');
$phone_field = $member_profile->field_private_phone['und'][0];
if(module_exists('cck_phone')){
$cc = cck_phone_countrycodes($phone_field['country_codes']);
$phone = $cc['code'] .' '. $phone_field['number'];
}
$correspondances = array(
"billing_first_name" => $member_profile->field_first_name['und'][0]['value'],
"billing_last_name" => $member_profile->field_name['und'][0]['value'],
"billing_phone" => $phone,
"billing_company" => $member_profile->field_organization['und'][0]['value'],
"billing_street1" => $location->thoroughfare,
"billing_street2" => $location->premise,
"billing_city" => $location->locality,
// "billing_zone" => ,
"billing_postal_code" => $location->postal_code,
"billing_country" => $location->uc_country_id,
);
foreach ($correspondances as $order_field => $value) {
$order->{$order_field} = $value;
}
$form['panes']['billing']['address']['#default_value'] = $order;
}
}
}
/**
* Implements hook_uc_order();
*/
function materio_user_order($op, $order, $arg2) {
// dsm($op, "matyerio_user_uc_order | op");
// dsm($order, "order");
// dsm($arg2, "arg2");
switch ($op) {
case 'save':
// Do something to save payment info!
break;
}
}
# not used yet
function materio_user_create_collaborators_users(){
/*
if ($values['membership_options'] == 3) {
$i = 1;
foreach ($values['collaborators'] as $collab_key => $coll) {
// foreach ($coll as $field_key => $field_value) {
// if($field_value == ''){
// $human_field_name = $form['submitted']['collaborators'][$coll_key]['#title'] . " : " . $form['submitted']['collaborators'][$coll_key][$field_key]['#title'];
// $field_name = 'submitted][collaborators]['.$coll_key.']['.$field_key;
// // dsm($field_name, 'field_name');
// form_set_error($field_name, t('You must provide a value for the !name field.', array('!name'=>$human_field_name)));
// }
// }
$userinfo = array(
'mail' => $coll['e_mail_collab_'.$i],
'name' => user_password(),
'pass' => user_password(), // Generate password
// 'init' => $data['components']['username']['value'],
'status' => 0,
'access' => REQUEST_TIME,
'memo' => 'from webform',
);
$account = drupal_anonymous_user();
$account->is_new = TRUE;
user_save($account, $userinfo);
// module_invoke_all('user_insert', $edit, $account);
$i++;
}
}
*/
}
/**
* Implements hook_help().
*/
function materio_user_help($path, $arg) {
// dsm($path, 'path');
// dsm($arg, 'arg');
switch ($path) {
case 'node/%':
if($arg[1] == 11187 ){
global $user;
// dsm($user, 'user');
if(isset($user->roles[1])){ // anonyme
$message = t('Please create first a free account and join materio or log in with your existing account,
then we\'ll provide you our membership form.', array());
$content = '' . $message . '
';
return $content;
}else if(isset($user->roles[2])){ // authentificated user (not utilisateur)
$content = '' . t('Welcome, you just joined materiO\' !') . '
';
$frontlink = l(t('continue with basic'), '');
$message = t('Now you can choose the membership that\'s right for you, or !link', array('!link'=>$frontlink));
$content .= '' . $message . '
';
return $content;
}
}
}
}
/**
* Implements hook_block_view_alter().
*/
function materio_user_block_view_alter(&$data, $block) {
if( $block->module == "user" && $block->delta == 'login' && isset($data['subject']) ){
// dsm($block, 'block');
// dsm($data, 'data');
$data['subject'] = '' . '' . $data['subject'] . '';
}
}
/**
* Define constants
*/
define('MATERIO_USER_CONFIRMED_USER_ROLE', 'Utilisateur'); // add role name here
/**
* Implement hook_user
*/
function _materio_user_user_update(&$edit, $account, $category){
// dsm($edit, 'edit');
// dsm($account, 'account');
// dsm($category, 'category');
// This is only fired when a user confirms their email address, logintoboggan style
if (isset($account->logintoboggan_email_validated) && $account->logintoboggan_email_validated == TRUE) {
$confirmed_rid = materio_user_get_role_by_name(MATERIO_USER_CONFIRMED_USER_ROLE);
$roles = $account->roles + array($confirmed_rid => MATERIO_USER_CONFIRMED_USER_ROLE);
// we have to do this to stop an infinite loop, and also to allow lower weighted modules to possibly do something here
$user = $account;
unset($user->logintoboggan_email_validated);
user_save($user, array('roles' => $roles));
drupal_go_to('user/'.$user->uid.'/edit');
}
}
/**
* Returns a role ID based on role name
*
* @param $name
* name of role to return
* @return
* (int) Role ID
*/
function materio_user_get_role_by_name($name) {
return array_search($name, user_roles());
}
/**
* Catch rejected email by mandrill, and remove them from simplenews lists
*
* @param array $result
* Associative array containing the send result, including the status.
*/
function materio_user_mandrill_mailsend_result($result) {
$status = $result['status'];
if ( in_array($status, array('rejected', 'invalid', 'error')) ) {
$email = $result['email'];
// remove email from pool
module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
simplenews_delete_spool(array('mail'=>$email));
watchdog("materio_user", "@mail rejected by mandrill, removed from simplenews spool", array("@mail"=>$email));
if ( in_array($status, array('invalid')) ) {
// unsubscribe user if mail does not exists
$subscriber = simplenews_subscriber_load_by_mail($email);
if ($subscriber) {
simplenews_subscriber_delete($subscriber);
}
// disable user
$user = user_load_by_mail($email);
// $user->status = 0;
user_save($user, array('status'=>0));
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");
}
}
}