<?php $plugin = elgg_extract('entity', $vars); $separator = csv_exporter_get_separator(); echo elgg_view_input('text', ['label' => elgg_echo('csv_exporter:settings:separator'), 'name' => 'params[separator]', 'value' => $separator, 'maxlength' => 1]); echo elgg_view_input('text', ['label' => elgg_echo('csv_exporter:settings:retention'), 'help' => elgg_echo('csv_exporter:settings:retention:description'), 'name' => 'params[retention]', 'value' => $plugin->retention]);
/** * Process the export to generate the downloadable file * * @return void */ public function process() { if ($this->isProcessing()) { return; } // lock this entity in order to prevent duplicate processing $this->lockProcessing(); // get content type/subtype $type = $this->getFormData('type'); $subtype = $this->getFormData('subtype'); // get possible exportable values for this content type/subtype $available_values = csv_exporter_get_exportable_values($type, $subtype, true); // get configured export fields $exportable_values = $this->getFormData('exportable_values'); if (empty($exportable_values)) { $this->unlockProcessing(); return; } // prepare for exporting $fo = $this->getFileObject(); $seperator = csv_exporter_get_separator(); // make csv header row $headers = []; foreach ($exportable_values as $export_value) { $headers[] = array_search($export_value, $available_values); } // create the new file with the headers $fh = $fo->open('write'); fputcsv($fh, $headers, $seperator); // append the rest of the data $fh = $fo->open('append'); // set entity options $entity_options = ['type' => $type, 'subtype' => $subtype, 'limit' => false]; // limit users to members of the site if ($type == 'user') { $entity_options['relationship'] = 'member_of_site'; $entity_options['relationship_guid'] = elgg_get_site_entity()->getGUID(); $entity_options['inverse_relationship'] = true; } // add time constraints $this->addTimeContraints($entity_options); // this could take a while set_time_limit(0); $entities = new ElggBatch('elgg_get_entities_from_relationship', $entity_options); /* @var $entity ElggEntity */ foreach ($entities as $entity) { $values = []; // params for hook $params = ['type' => $type, 'subtype' => $subtype, 'entity' => $entity]; foreach ($exportable_values as $export_value) { $params['exportable_value'] = $export_value; $value = elgg_trigger_plugin_hook('export_value', 'csv_exporter', $params); if ($value === null) { $value = $entity->{$export_value}; } if (is_array($value)) { $value = implode(', ', $value); } $values[] = $value; } // write row fputcsv($fh, $values, $seperator); } // cleanup $this->complete(); $this->unlockProcessing(); $fo->close(); }