Skip to content

Commit

Permalink
add exporter package tainacan
Browse files Browse the repository at this point in the history
  • Loading branch information
vnmedeiros committed Jul 17, 2019
1 parent b96d36e commit a7f548a
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/exporter/class-tainacan-export-handler.php
Expand Up @@ -20,14 +20,23 @@ public function init() {
'class_name' => '\Tainacan\Exporter\CSV'
]);

$this->register_exporter([
'name' => 'Vocabulary CSV',
'description' => __('Allows you to export a taxonomy to a CSV file', 'tainacan'),
'slug' => 'vocabularycsv',
'class_name' => '\Tainacan\Exporter\Term_Exporter',
'manual_mapping' => false,
'manual_collection' => false
]);
$this->register_exporter([
'name' => 'Vocabulary CSV',
'description' => __('Allows you to export a taxonomy to a CSV file', 'tainacan'),
'slug' => 'vocabularycsv',
'class_name' => '\Tainacan\Exporter\Term_Exporter',
'manual_mapping' => false,
'manual_collection' => false
]);

$this->register_exporter([
'name' => 'Package Tainacan',
'description' => __('Allows you to export all data in one package', 'tainacan'),
'slug' => 'packagetainacan',
'class_name' => '\Tainacan\Exporter\Package_Exporter',
'manual_mapping' => false,
'manual_collection' => false
]);

do_action('tainacan_register_exporters');

Expand Down
123 changes: 123 additions & 0 deletions src/exporter/class-tainacan-package-exporter.php
@@ -0,0 +1,123 @@
<?php

/**
* @author: Vinicius Nunes.
* Term Exporter
*
* Class to export package tainacan
*
* the planning:
*
* Export:
* structures
* repositories
* taxonomies (terms)
* metadada
* filters
* collection
* metadada
* filters
* self collection
* item
*
*/


namespace Tainacan\Exporter;
use Tainacan;
use Tainacan\Entities;
use Tainacan\Repositories;

class Package_Exporter extends Exporter {

protected $steps = [
[
'name' => 'Taxonomies',
'progress_label' => 'Exporting taxonomies/terms',
'callback' => 'exporting_taxonomies'
]
];

public function __construct($attributes = array()){
parent::__construct($attributes);
$this->set_default_options([]);
}

/**
* When exporter is finished, gets the final output
*/
public function get_output() {
return "link to Package;";
}

public function options_form() {
ob_start();
?>
<div>
</div>
<?php
return ob_get_clean();
}

public function process_item($index, $collection_definition) {
return true;
}

/**
*
*/
public function exporting_taxonomies() {

$taxonomyRepository = Repositories\Taxonomies::get_instance();
$termRepository = Repositories\Terms::get_instance();
$taxonomies = $taxonomyRepository->fetch();
if($taxonomies->have_posts()) {
while ($taxonomies->have_posts()) {
$taxonomies->the_post();
$taxonomy = new Entities\Taxonomy($taxonomies->post);
$this->getTermsRecursively( $termRepository, $taxonomy );
}
wp_reset_postdata();
}
return true;
}

/**
* @param $termRepository Repositories\Terms the terms repository
* @param $taxonomy Entities\Taxonomy the taxonomy to fetch the terms
* @param $parent int the id of term father
* @param $level int the level to create the csv line
*
* @return string
*/
public function getTermsRecursively( $termRepository, $taxonomy, $parent = 0, $level = 0 ) {
$terms = $termRepository->fetch([ 'parent' => $parent, 'hide_empty' => false ], $taxonomy->get_id());
$fileTerm = $taxonomy->get_id() . "_terms";
if( $terms && sizeof($terms) > 0 ) {
$line = [];
foreach ( $terms as $term ) {
$line[] = $term->get_name();
$line[] = $term->get_description();
for ($i =0; $i < $level; $i++){
array_unshift($line, "" );
}

$line_string = $this->str_putcsv($line);
$this->append_to_file($fileTerm, $line_string."\n");
$this->getTermsRecursively($termRepository, $taxonomy, $term->get_id(), $level + 1);
$line = array();
}
}
}

function str_putcsv($item, $delimiter = ',', $enclosure = '"') {
$fp = fopen('php://temp', 'r+');
fputcsv($fp, $item, $delimiter, $enclosure);
rewind($fp);
$fstats = fstat($fp);
$data = fread($fp, $fstats['size']);
fclose($fp);
return rtrim($data, "\n");
}

}

0 comments on commit a7f548a

Please sign in to comment.