added export companies as csv file function
This commit is contained in:
parent
4b2ab455c9
commit
4ca87255ab
@ -13,6 +13,12 @@
|
||||
* Implements hook_permission().
|
||||
*/
|
||||
function materio_contactops_permission() {
|
||||
$perms = array(
|
||||
'access company export' => array(
|
||||
'title' => t('access company export'),
|
||||
'description' => t('access company export'),
|
||||
)
|
||||
);
|
||||
$perms = array(
|
||||
'view own company related materials' => array(
|
||||
'title' => t('view own company related materials'),
|
||||
@ -26,7 +32,13 @@ function materio_contactops_permission() {
|
||||
function materio_contactops_menu(){
|
||||
$items = array();
|
||||
|
||||
$base = array(
|
||||
$items['admin/content/companies/export'] = array(
|
||||
'title' => t('Export Companies'),
|
||||
'page callback' => 'materio_contactops_export',
|
||||
'page arguments' => array(),
|
||||
'access arguments' => array('access company export'),
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
'file' => 'materio_contactops.pages.inc',
|
||||
);
|
||||
|
||||
$items['user/%user/materials'] = array(
|
||||
@ -41,6 +53,25 @@ function materio_contactops_menu(){
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_menu_local_tasks_alter().
|
||||
*/
|
||||
function materio_contactops_menu_local_tasks_alter(&$data, $router_item, $root_path) {
|
||||
switch($root_path){
|
||||
case 'admin/content/companies' :
|
||||
$item = menu_get_item('admin/content/companies/export');
|
||||
if ($item['access']) {
|
||||
$data['actions']['output'][] = array(
|
||||
'#theme' => 'menu_local_action',
|
||||
'#link' => $item,
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
|
@ -88,3 +88,119 @@ function materio_contactops_materials($current_user){
|
||||
'pager' => theme('pager'),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
function materio_contactops_export(){
|
||||
// get materials taged with this company
|
||||
$query = new EntityFieldQuery;
|
||||
$query
|
||||
->entityCondition('entity_type', 'node')
|
||||
->propertyCondition('status', 1)
|
||||
->entityCondition('bundle', array('company'))
|
||||
->propertyOrderBy('title', 'ASC');
|
||||
|
||||
$result = $query->execute();
|
||||
// dsm($result, '$result');
|
||||
$headers = array("name", "email", "materials");
|
||||
// $k=1;
|
||||
$items = [];
|
||||
if(isset($result['node'])){
|
||||
$companies = $result['node'];
|
||||
|
||||
// At first we need to get field's id.
|
||||
// If you already know field id, you can ommit this step
|
||||
// Get all fields attached to a given node type
|
||||
$company_fields = field_info_instances('node', 'company');
|
||||
// dsm($company_fields, 'fields');
|
||||
$materiau_fields = field_info_instances('node', 'materiau');
|
||||
// dsm($materiau_fields, 'fields');
|
||||
|
||||
// Attach a field of selected id only to get value for it
|
||||
$field_tode_company_id = $company_fields['field_tode_company']['field_id'];
|
||||
field_attach_load('node', $companies, FIELD_LOAD_CURRENT, array('field_id' => $field_tode_company_id));
|
||||
$field_public_email_id = $company_fields['field_public_email']['field_id'];
|
||||
field_attach_load('node', $companies, FIELD_LOAD_CURRENT, array('field_id' => $field_public_email_id));
|
||||
|
||||
$title_field_materio_id = $materiau_fields['title_field']['field_id'];
|
||||
$field_reference_materio_id = $materiau_fields['field_reference_materio']['field_id'];
|
||||
|
||||
// dsm($companies, "companies");
|
||||
|
||||
foreach ($companies as $comp_nid => $company) {
|
||||
// if($k)
|
||||
// dsm($company, "company");
|
||||
|
||||
|
||||
// title
|
||||
$title = db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $comp_nid))->fetchField();
|
||||
// if($k)
|
||||
// dsm($title, "title");
|
||||
$item = array(trim($title));
|
||||
|
||||
// emails
|
||||
$email = $company->field_public_email['und'][0]["email"];
|
||||
$item[] = trim($email);
|
||||
|
||||
// get the company taxo terme (tode)
|
||||
$tid = $company->field_tode_company['und'][0]['tid'];
|
||||
|
||||
// dsm($tid, "tid");
|
||||
// get all materials tagued with this terme
|
||||
$materials_query = new EntityFieldQuery;
|
||||
$materials_query
|
||||
->entityCondition('entity_type', 'node')
|
||||
->propertyCondition('status', 1)
|
||||
->entityCondition('bundle', array('materiau'))
|
||||
->fieldCondition('field_company_fab', 'tid', $tid, '=')
|
||||
->propertyOrderBy('created', 'DESC');
|
||||
$materials_result = $materials_query->execute();
|
||||
// if($k)
|
||||
// dsm($materials_result , 'materials_result');
|
||||
|
||||
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));
|
||||
|
||||
$materials_output = [];
|
||||
// $m=1;
|
||||
foreach ($materials as $mnid => $mn) {
|
||||
// if($k && $m)
|
||||
// dsm($mn, "materiau");
|
||||
|
||||
$name = trim($mn->title_field['fr'][0]['value']);
|
||||
$ref = trim($mn->field_reference_materio['und'][0]['value']);
|
||||
|
||||
// if($k && $m){
|
||||
// dsm($name, "name");
|
||||
// dsm($ref, "ref");
|
||||
// }
|
||||
|
||||
$materials_output[] = $name." (".$ref.")";
|
||||
// $m=0;
|
||||
}
|
||||
// if($k)
|
||||
// dsm($materials_output, 'materials_output');
|
||||
|
||||
$item[] = implode(",", $materials_output);
|
||||
}
|
||||
|
||||
// record the csv line
|
||||
$items[] = $item;
|
||||
// $k=0;
|
||||
}
|
||||
|
||||
// dsm($items, "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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user