Browse Source

added the right export script for industriels users (not companies)

Bachir Soussi Chiadmi 6 years ago
parent
commit
be2da7afe0

+ 19 - 2
sites/all/modules/gui/materiobasemod/materio_contactops.module

@@ -32,6 +32,15 @@ function materio_contactops_menu(){
 
   $items['admin/content/companies/export'] = array(
     'title' => t('Export Companies'),
+    'page callback' => 'materio_companies_export',
+    'page arguments' => array(),
+    'access arguments' => array('access company export'),
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'materio_contactops.pages.inc',
+  );
+
+  $items['admin/users/industriels/export'] = array(
+    'title' => t('Export Companies Contacts'),
     'page callback' => 'materio_contactops_export',
     'page arguments' => array(),
     'access arguments' => array('access company export'),
@@ -64,8 +73,16 @@ function materio_contactops_menu_local_tasks_alter(&$data, $router_item, $root_p
           '#link' => $item,
         );
       }
-
-    break;
+      break;
+    case 'admin/users/industriels':
+      $item = menu_get_item('admin/users/industriels/export');
+      if ($item['access']) {
+        $data['actions']['output'][] = array(
+          '#theme' => 'menu_local_action',
+          '#link' => $item,
+        );
+      }
+      break;
   }
 
 }

+ 93 - 1
sites/all/modules/gui/materiobasemod/materio_contactops.pages.inc

@@ -90,7 +90,7 @@ function materio_contactops_materials($current_user){
 }
 
 
-function materio_contactops_export(){
+function materio_companies_export(){
   // get materials taged with this company
   $query = new EntityFieldQuery;
   $query
@@ -204,3 +204,95 @@ function materio_contactops_export(){
   endforeach;
   print $rows;
 }
+
+
+function materio_contactops_export(){
+  $role = user_role_load_by_name('Contact opérationnel');
+  $uids = db_select('users_roles', 'ur')
+      ->fields('ur', array('uid'))
+      ->condition('ur.rid', $role->rid, '=')
+      ->execute()
+      ->fetchCol();
+  // dsm($uids, "uids");
+  $users = user_load_multiple($uids);
+  // dsm($users, "users");
+
+  $companies_material_cache = array();
+  $items = [];
+  $headers = ['company', 'mail', 'materials'];
+  foreach ($users as $uid => $user) {
+    // $item = [];
+    // TODO: get company from
+    $company_tid = $user->field_company['und'][0]['tid'];
+    $company = taxonomy_term_load($company_tid);
+
+    // avoid users withour company
+    if(!isset($company->name)){
+      continue;
+    }
+
+    // if(count($items) < 1){
+    //   dsm($user, "user");
+    //   dsm($company, "company");
+    // }
+
+    if(!isset($companies_material_cache[$company->name])){
+      // get all materials tagued with this company terme
+      $materials_query = new EntityFieldQuery;
+      $materials_query
+        ->entityCondition('entity_type', 'node')
+        ->propertyCondition('status', 1)
+        ->entityCondition('bundle', array('materiau'))
+        ->fieldCondition('field_company_fab', 'tid', $company_tid, '=')
+        ->propertyOrderBy('created', 'DESC');
+      $materials_result = $materials_query->execute();
+      //
+      $materials_array = [];
+      //
+      if(isset($materials_result['node'])){
+        $materials = $materials_result['node'];
+
+        field_attach_load('node', $materials, FIELD_LOAD_CURRENT, array('field_id' => $title_field_materio_id));
+        field_attach_load('node', $materials, FIELD_LOAD_CURRENT, array('field_id' => $field_reference_materio_id));
+
+        foreach ($materials as $mnid => $mn) {
+          $name = trim($mn->title_field['fr'][0]['value']);
+          $ref = trim($mn->field_reference_materio['und'][0]['value']);
+          $materials_array[] = $name." (".$ref.")";
+        }
+
+        $companies_material_cache[$company->name] = implode(",", $materials_array);
+      }
+    }
+
+    $item = array(
+      "company" => $company->name,
+      "mail" => $user->mail,
+      "materials" => $companies_material_cache[$company->name]
+    );
+
+    $items[] = $item;
+  }
+
+  // Obtient une liste de colonnes
+  foreach ($items as $key => $row) {
+      $company_col[$key]  = $row['company'];
+  }
+
+  // Trie les données par company croissant
+  // Ajoute $items en tant que dernier paramètre, pour trier par la clé commune
+  array_multisort($company_col, SORT_ASC, $items);
+
+  // dsm($items);
+
+  // drupal_set_title("Companies export");
+  drupal_add_http_header('Content-Type', 'text/csv; utf-8');
+  drupal_add_http_header('Content-Disposition', 'attachment;filename=materio_companies.csv');
+
+  $separator=";";
+  $rows = implode($separator, $headers) . "\r\n";
+  foreach ($items as $count => $item_row):
+    $rows .= implode($separator, $item_row) . "\r\n";
+  endforeach;
+  print $rows;
+}