materio_contactops.pages.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. function materio_contactops_materials($current_user){
  3. // dsm($current_user, 'current_user');
  4. global $language;
  5. global $user;
  6. // dsm($user, 'user');
  7. // get user company tag
  8. $company_tid = $current_user->field_company['und'][0]['tid'];
  9. // dsm($company_tid,'company_tid');
  10. # retrieve viewmode and then use it to define the query range
  11. $viewmode = isset($user->data['materiosearchapi_viewmode'])
  12. ? $user->data['materiosearchapi_viewmode']
  13. : variable_get('defaultviewmode', 'full');
  14. $limit = variable_get($viewmode.'_limite', '10');
  15. // dsm($limit, "limit");
  16. $offset = pager_find_page() * $limit; //$page*$limit;//
  17. // dsm($offset, "offset");
  18. // get materials taged with this company
  19. $query = new EntityFieldQuery;
  20. $query
  21. ->entityCondition('entity_type', 'node')
  22. ->propertyCondition('status', 1)
  23. ->entityCondition('bundle', array('materiau'))
  24. // ->fieldCondition('field_company_distrib', 'tid', $company_tid, '=')
  25. ->fieldCondition('field_company_fab', 'tid', $company_tid, '=')
  26. ->propertyOrderBy('created', 'DESC')
  27. ->range($offset,$limit);
  28. $result = $query->execute();
  29. // dsm($result, '$result');
  30. foreach ($result['node'] as $nid => $n) {
  31. $items[] = node_load($nid);
  32. }
  33. $count_query = new EntityFieldQuery;
  34. $count_query
  35. ->entityCondition('entity_type', 'node')
  36. ->propertyCondition('status', 1)
  37. ->entityCondition('bundle', array('materiau'))
  38. // ->fieldCondition('field_company_distrib', 'tid', $company_tid, '=')
  39. ->fieldCondition('field_company_fab', 'tid', $company_tid, '=');
  40. // dsm($count, 'count');
  41. $count = $count_query->count()->execute();
  42. $query = new EntityFieldQuery;
  43. $query
  44. ->entityCondition('entity_type', 'node')
  45. ->propertyCondition('status', 1)
  46. ->entityCondition('bundle', array('materiau'))
  47. ->fieldCondition('field_company_distrib', 'tid', $company_tid, '=')
  48. // ->fieldCondition('field_company_fab', 'tid', $company_tid, '=')
  49. ->propertyOrderBy('created', 'DESC')
  50. ->range($offset,$limit);
  51. $result = $query->execute();
  52. // dsm($result, '$result');
  53. foreach ($result['node'] as $nid => $n) {
  54. $items[] = node_load($nid);
  55. }
  56. $count_query = new EntityFieldQuery;
  57. $count_query
  58. ->entityCondition('entity_type', 'node')
  59. ->propertyCondition('status', 1)
  60. ->entityCondition('bundle', array('materiau'))
  61. // ->fieldCondition('field_company_distrib', 'tid', $company_tid, '=')
  62. ->fieldCondition('field_company_fab', 'tid', $company_tid, '=');
  63. // dsm($count, 'count');
  64. $count += $count_query->count()->execute();
  65. pager_default_initialize($count, $limit);
  66. drupal_set_title(t(''));
  67. return theme('materio_contactops_materials', array(
  68. 'items' => $items,
  69. 'view_mode' => $viewmode,
  70. 'count' => $count,
  71. 'pager' => theme('pager'),
  72. ));
  73. }
  74. function materio_companies_export(){
  75. // get materials taged with this company
  76. $query = new EntityFieldQuery;
  77. $query
  78. ->entityCondition('entity_type', 'node')
  79. ->propertyCondition('status', 1)
  80. ->entityCondition('bundle', array('company'))
  81. ->propertyOrderBy('title', 'ASC');
  82. $result = $query->execute();
  83. // dsm($result, '$result');
  84. $headers = array("name", "email", "materials");
  85. // $k=1;
  86. $items = [];
  87. if(isset($result['node'])){
  88. $companies = $result['node'];
  89. // At first we need to get field's id.
  90. // If you already know field id, you can ommit this step
  91. // Get all fields attached to a given node type
  92. $company_fields = field_info_instances('node', 'company');
  93. // dsm($company_fields, 'fields');
  94. $materiau_fields = field_info_instances('node', 'materiau');
  95. // dsm($materiau_fields, 'fields');
  96. // Attach a field of selected id only to get value for it
  97. $field_tode_company_id = $company_fields['field_tode_company']['field_id'];
  98. field_attach_load('node', $companies, FIELD_LOAD_CURRENT, array('field_id' => $field_tode_company_id));
  99. $field_public_email_id = $company_fields['field_public_email']['field_id'];
  100. field_attach_load('node', $companies, FIELD_LOAD_CURRENT, array('field_id' => $field_public_email_id));
  101. $title_field_materio_id = $materiau_fields['title_field']['field_id'];
  102. $field_reference_materio_id = $materiau_fields['field_reference_materio']['field_id'];
  103. // dsm($companies, "companies");
  104. foreach ($companies as $comp_nid => $company) {
  105. // if($k)
  106. // dsm($company, "company");
  107. // title
  108. $title = db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $comp_nid))->fetchField();
  109. // if($k)
  110. // dsm($title, "title");
  111. $item = array(trim($title));
  112. // emails
  113. $email = $company->field_public_email['und'][0]["email"];
  114. $item[] = trim($email);
  115. // get the company taxo terme (tode)
  116. $tid = $company->field_tode_company['und'][0]['tid'];
  117. // dsm($tid, "tid");
  118. // get all materials tagued with this terme
  119. $materials_query = new EntityFieldQuery;
  120. $materials_query
  121. ->entityCondition('entity_type', 'node')
  122. ->propertyCondition('status', 1)
  123. ->entityCondition('bundle', array('materiau'))
  124. ->fieldCondition('field_company_fab', 'tid', $tid, '=')
  125. ->propertyOrderBy('created', 'DESC');
  126. $materials_result = $materials_query->execute();
  127. // if($k)
  128. // dsm($materials_result , 'materials_result');
  129. if(isset($materials_result['node'])){
  130. $materials = $materials_result['node'];
  131. field_attach_load('node', $materials, FIELD_LOAD_CURRENT, array('field_id' => $title_field_materio_id));
  132. field_attach_load('node', $materials, FIELD_LOAD_CURRENT, array('field_id' => $field_reference_materio_id));
  133. $materials_output = [];
  134. // $m=1;
  135. foreach ($materials as $mnid => $mn) {
  136. // if($k && $m)
  137. // dsm($mn, "materiau");
  138. $name = trim($mn->title_field['fr'][0]['value']);
  139. $ref = trim($mn->field_reference_materio['und'][0]['value']);
  140. // if($k && $m){
  141. // dsm($name, "name");
  142. // dsm($ref, "ref");
  143. // }
  144. $materials_output[] = $name." (".$ref.")";
  145. // $m=0;
  146. }
  147. // if($k)
  148. // dsm($materials_output, 'materials_output');
  149. $item[] = implode(",", $materials_output);
  150. }
  151. // record the csv line
  152. $items[] = $item;
  153. // $k=0;
  154. }
  155. // dsm($items, "items");
  156. }
  157. // drupal_set_title("Companies export");
  158. drupal_add_http_header('Content-Type', 'text/csv; utf-8');
  159. drupal_add_http_header('Content-Disposition', 'attachment;filename=materio_companies.csv');
  160. $separator=";";
  161. $rows = implode($separator, $headers) . "\r\n";
  162. foreach ($items as $count => $item_row):
  163. $rows .= implode($separator, $item_row) . "\r\n";
  164. endforeach;
  165. print $rows;
  166. }
  167. function materio_contactops_export(){
  168. $role = user_role_load_by_name('Contact opérationnel');
  169. $uids = db_select('users_roles', 'ur')
  170. ->fields('ur', array('uid'))
  171. ->condition('ur.rid', $role->rid, '=')
  172. ->execute()
  173. ->fetchCol();
  174. // dsm($uids, "uids");
  175. $users = user_load_multiple($uids);
  176. // dsm($users, "users");
  177. $companies = array(); // cache array
  178. $companies_material_cache = array(); // cache array
  179. $items = [];
  180. $headers = ['company', 'mail', 'materials'];
  181. foreach ($users as $uid => $user) {
  182. // $item = [];
  183. // TODO: get company from
  184. $company_tid = $user->field_company['und'][0]['tid'];
  185. if(isset($companies[$company_tid])){
  186. $company = $companies[$company_tid];
  187. }else{
  188. $company = taxonomy_term_load($company_tid);
  189. $companies[$company_tid] = $company;
  190. }
  191. // avoid users withour company
  192. if(!isset($company->name)){
  193. continue;
  194. }
  195. // if(count($items) < 1){
  196. // dsm($user, "user");
  197. // dsm($company, "company");
  198. // }
  199. if(!isset($companies_material_cache[$company->name])){
  200. // get all materials tagued with this company terme
  201. $materials_query = new EntityFieldQuery;
  202. $materials_query
  203. ->entityCondition('entity_type', 'node')
  204. ->propertyCondition('status', 1)
  205. ->entityCondition('bundle', array('materiau'))
  206. ->fieldCondition('field_company_fab', 'tid', $company_tid, '=')
  207. ->propertyOrderBy('created', 'DESC');
  208. $materials_result = $materials_query->execute();
  209. //
  210. $materials_array = [];
  211. //
  212. if(isset($materials_result['node'])){
  213. $materials = $materials_result['node'];
  214. field_attach_load('node', $materials, FIELD_LOAD_CURRENT, array('field_id' => $title_field_materio_id));
  215. field_attach_load('node', $materials, FIELD_LOAD_CURRENT, array('field_id' => $field_reference_materio_id));
  216. foreach ($materials as $mnid => $mn) {
  217. $name = trim($mn->title_field['fr'][0]['value']);
  218. $ref = trim($mn->field_reference_materio['und'][0]['value']);
  219. $materials_array[] = $name." (".$ref.")";
  220. }
  221. $companies_material_cache[$company->name] = implode(",", $materials_array);
  222. }
  223. }
  224. $item = array(
  225. "company" => $company->name,
  226. "mail" => $user->mail,
  227. "materials" => $companies_material_cache[$company->name]
  228. );
  229. $items[] = $item;
  230. }
  231. // Obtient une liste de colonnes
  232. foreach ($items as $key => $row) {
  233. $company_col[$key] = $row['company'];
  234. }
  235. // Trie les données par company croissant
  236. // Ajoute $items en tant que dernier paramètre, pour trier par la clé commune
  237. array_multisort($company_col, SORT_ASC, $items);
  238. // dsm($items);
  239. // drupal_set_title("Companies export");
  240. drupal_add_http_header('Content-Type', 'text/csv; utf-8');
  241. drupal_add_http_header('Content-Disposition', 'attachment;filename=materio_companies.csv');
  242. $separator=";";
  243. $rows = implode($separator, $headers) . "\r\n";
  244. foreach ($items as $count => $item_row):
  245. $rows .= implode($separator, $item_row) . "\r\n";
  246. endforeach;
  247. print $rows;
  248. }