<?php

function materio_user_registerblock(){
  $return = array();
  $block = block_load('materio_user','user_createaccount');
  $return['block'] = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
  drupal_json_output($return);
}

function materio_user_loginandregisterblock(){
  $return = array();
  $block = block_load('materio_user','user_register');
  $return['block'] = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
  drupal_json_output($return);
}

function materio_user_register_submit(){
  $return = array();
  $return['POST'] = $_POST;

  $form_state = array( "values"=>$_POST);
  drupal_form_submit($_POST['form_id'], $form_state);

  // $return['form_state'] = $form_state;

  $return['errors'] = form_get_errors();
  if($return['errors']){
    unset ($_SESSION['messages']['error']);
  }else{
    $messages = drupal_get_messages('status');
    // $return['messages'] = $messages;
    // drupal_set_message(t("Congratulations, you juste created your free materiO' account, welcome !"), 'status');
    foreach ($messages['status'] as $msg) {
      drupal_set_message($msg, 'status');
    }
  }
  // after registration user is automaticly logged in, thank's to login tobogan module

  drupal_json_output($return);
}

function materio_user_login_submit(){
  $return = array();
  $return['POST'] = $_POST;

  $form_state = array("values"=>$_POST);
  drupal_form_submit($_POST['form_id'], $form_state);

  $return['errors'] = form_get_errors();
  if($return['errors'])
    unset ($_SESSION['messages']['error']);

  // if user-login form succed we retreive the user uid on $form_state, then we can effectively loggin the user
  if($uid = $form_state['uid'])
    user_login_submit(array(), $form_state);

  drupal_json_output($return);
}

function materio_user_export(){
  $ret = "";

  // MEMBRE et etudiants
  $query = db_select('users', 'u');
  $query->join('users_roles', 'ur', 'u.uid=ur.uid');
  $query
    ->fields('u', array('mail', 'uid'))
    ->condition('u.status', '1')
    ->condition('ur.rid', array(6, 8, 10), "IN");
  $results = $query->execute()->fetchall();

  $vals = array();
  foreach ($results as $rec) {
    $vals[] = $rec->mail;
  }

  $num_rows = count($vals);
  $ret .= "<h1>Liste de tous les user adherents et étudiants ($num_rows)</h1>";
  $ret .= '<textarea cols="40" rows="30">'.implode($vals, '&#13;&#10;').'</textarea>';

  // MEMBRE sans les aaf
  $query = db_select('users', 'u');
  $query->join('users_roles', 'ur', 'u.uid=ur.uid');
  // $query->join('field_data_field_memo', 'mem', 'u.uid=mem.entity_id');
  $query
    ->fields('u', array('mail', 'uid'))
    ->condition('u.status', '1')
    ->condition('ur.rid', array(6, 8, 10), "IN");
    // ->condition('mem.field_memo_value', "%AAF%", "LIKE");
  $results = $query->execute()->fetchall();

  $vals = array();
  foreach ($results as $rec) {
    $aaf_query = db_select('field_data_field_memo', 'mem');
    $aaf_query
      ->fields('mem', array("field_memo_value"))
      ->isNotNull('field_memo_value')
      ->condition('mem.entity_id', $rec->uid);
      $aaf_results = $aaf_query->execute()->fetchall();

    $aaf = false;
    foreach ($aaf_results as $memo) {
      if(strpos($memo->field_memo_value,'AAF') !== false){
        $aaf = true;
        break;
      }
    }
    if(!$aaf)
      $vals[] = $rec->mail;
  }

  $num_rows = count($vals);
  $ret .= "<h1>Liste des user adhérents et étudiants (sans les aaf) ($num_rows)</h1>";
  $ret .= '<textarea cols="40" rows="30">'.implode($vals, '&#13;&#10;').'</textarea>';


  // MEMBRE AAF
  $query = db_select('users', 'u');
  $query->join('users_roles', 'ur', 'u.uid=ur.uid');
  // $query->join('field_data_field_memo', 'mem', 'u.uid=mem.entity_id');
  $query
    ->fields('u', array('mail', 'uid'))
    ->condition('u.status', '1')
    ->condition('ur.rid', array(6, 8, 10), "IN");
    // ->condition('mem.field_memo_value', "%AAF%", "LIKE");
  $results = $query->execute()->fetchall();

  $vals = array();
  foreach ($results as $rec) {
    $aaf_query = db_select('field_data_field_memo', 'mem');
    $aaf_query
      ->fields('mem', array("field_memo_value"))
      ->isNotNull('field_memo_value')
      ->condition('mem.entity_id', $rec->uid);
      $aaf_results = $aaf_query->execute()->fetchall();

    $aaf = false;
    foreach ($aaf_results as $memo) {
      if(strpos($memo->field_memo_value,'AAF') !== false){
        $aaf = true;
        break;
      }
    }
    if($aaf)
      $vals[] = $rec->mail;
  }

  $num_rows = count($vals);
  $ret .= "<h1>Liste des user adhérents AAF ($num_rows)</h1>";
  $ret .= '<textarea cols="40" rows="30">'.implode($vals, '&#13;&#10;').'</textarea>';


  // inscrit daily non membre
  $query = db_select('users', 'u');
  $query->join('users_roles', 'ur', 'u.uid=ur.uid');
  $query->join('simplenews_subscriber', 'ssr', 'u.uid=ssr.uid');
  $query->join('simplenews_subscription', 'ssn', 'ssr.snid=ssn.snid');
  $query
    ->fields('u', array('mail'))
    ->condition('u.status', '1')
    ->condition('ur.rid', array(3, 4, 6, 8, 10), "NOT IN")
    ->condition('ssn.tid', '6374')
    ->condition('ssn.status', '1');

  $results = $query->execute()->fetchall();

  $vals = array();
  foreach ($results as $rec) {
    $vals[] = $rec->mail;
  }

  $num_rows = count($vals);
  $ret .= "<h1>Liste des user non membre inscrits au daily ($num_rows)</h1>";
  $ret .= '<textarea cols="40" rows="30">'.implode($vals, '&#13;&#10;').'</textarea>';

  return $ret;
}



