Skip to content

Commit

Permalink
add package importer
Browse files Browse the repository at this point in the history
  • Loading branch information
vnmedeiros committed Jul 19, 2019
1 parent a7f548a commit a6a128b
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 46 deletions.
8 changes: 4 additions & 4 deletions src/exporter/class-tainacan-exporter.php
Expand Up @@ -698,9 +698,9 @@ private function map_item_metadata(\Tainacan\Entities\Item $item) {

}

public function add_new_file($key) {
public function add_new_file($key, $use_prefix) {
$upload_dir_info = wp_upload_dir();
$prefix = $this->get_id();
$prefix = ($use_prefix ? $this->get_id() : "" );
$upload_dir = trailingslashit( $upload_dir_info['basedir'] );
$upload_url = trailingslashit( $upload_dir_info['baseurl'] );
$exporter_folder = 'tainacan/exporter';
Expand All @@ -724,13 +724,13 @@ public function add_new_file($key) {
* @param string $key The file identifier. (it is the name of the file, with extension, and will be prefixed with the process ID)
* @param string $data The content to be appended to the file
*/
public function append_to_file($key, $data) {
public function append_to_file($key, $data, $prefix=true) {
if ( array_key_exists ( $key , $this->output_files ) ) {
$fp = fopen($this->output_files[$key]['filename'], 'a');
fwrite($fp, $data);
fclose($fp);
} else { // será?
$this->add_new_file($key);
$this->add_new_file($key, $prefix);
$this->append_to_file($key, $data);
}
}
Expand Down
106 changes: 64 additions & 42 deletions src/exporter/class-tainacan-package-exporter.php
Expand Up @@ -31,15 +31,37 @@
class Package_Exporter extends Exporter {

protected $steps = [
[
'name' => 'Check structure',
'progress_label' => 'Exporting checking structure',
'callback' => 'checkStructure'
],
[
'name' => 'Taxonomies',
'progress_label' => 'Exporting taxonomies/terms',
'callback' => 'exporting_taxonomies'
'progress_label' => 'Exporting taxonomies',
'callback' => 'exportingTaxonomies'
],
[
'name' => 'Terms',
'progress_label' => 'Exporting terms',
'callback' => 'exportingTerms'
],
[
'name' => 'compress package',
'progress_label' => 'Exporting compressing package',
'callback' => 'compressPackage'
]

];

public function __construct($attributes = array()){
parent::__construct($attributes);

$upload_dir = trailingslashit( wp_upload_dir()['basedir'] );
$exporter_folder = 'tainacan/exporter';
$package_folder = "/package";
$this->final_folder = $upload_dir . $exporter_folder . $package_folder;

$this->set_default_options([]);
}

Expand All @@ -63,61 +85,61 @@ public function process_item($index, $collection_definition) {
return true;
}

/**
*
*/
public function exporting_taxonomies() {
public function checkStructure() {
if (!is_dir($this->final_folder)) {
if (!mkdir($this->final_folder, 0777, true)) {
$this->add_error_log( 'Erro on create the folder: ' . $this->final_folder);
return false;
}
}
}

public function compressPackage() {

}

public function exportingTaxonomies() {
$taxonomyRepository = Repositories\Taxonomies::get_instance();
$termRepository = Repositories\Terms::get_instance();
$taxonomies = $taxonomyRepository->fetch();
if($taxonomies->have_posts()) {
$fileTaxonomies = "package/tnc_taxonomies";
$listTaxonomies = [];
while ($taxonomies->have_posts()) {
$taxonomies->the_post();
$taxonomy = new Entities\Taxonomy($taxonomies->post);
$this->getTermsRecursively( $termRepository, $taxonomy );
$listTaxonomies[] = $taxonomy->_toJson();
}
$this->append_to_file($fileTaxonomies, '[' . \implode(",", $listTaxonomies) . ']', false);
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();
public function exportingTerms() {

$term_exporte = new Term_Exporter();
$term_repo = Repositories\Terms::get_instance();
$taxonomies = Repositories\Taxonomies::get_instance()->fetch();

if($taxonomies->have_posts()) {
$listTaxonomies = [];
while ($taxonomies->have_posts()) {
$taxonomies->the_post();
$taxonomy = new Entities\Taxonomy($taxonomies->post);
$listTaxonomies[] = $taxonomy->get_id();

$term_exporte->get_terms_recursively( $term_repo, $taxonomy );
}
wp_reset_postdata();
}
}

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");
// $termRepository = Repositories\Terms::get_instance();
// $terms = $termRepository->fetch(['hide_empty' => false], $listTaxonomies);
// $fileTerms = "package/tnc_terms";
// foreach ($terms as $term) {
// $listTerms[] = $term->_toJson();
// }
// $this->append_to_file($fileTerms, '[' . \implode(",", $listTerms) . ']', false);
return true;
}

}
9 changes: 9 additions & 0 deletions src/importer/class-tainacan-importer-handler.php
Expand Up @@ -79,6 +79,15 @@ public function init() {
'manual_mapping' => true,
]);

$this->register_importer([
'name' => 'Package Tainacan',
'description' => __('Import package Tainacan', 'tainacan'),
'slug' => 'packagetainacan',
'class_name' => '\Tainacan\Importer\Package_Importer',
'manual_collection' => false,
'manual_mapping' => false,
]);

do_action('tainacan_register_importers');

add_action( 'tainacan-enqueue-admin-scripts', array($this, 'enqueue_scripts') );
Expand Down
90 changes: 90 additions & 0 deletions src/importer/class-tainacan-package-importer.php
@@ -0,0 +1,90 @@
<?php

namespace Tainacan\Importer;
use Tainacan;
use Tainacan\Entities;
use Tainacan\Repositories;

class Package_Importer extends Importer {
private $new_ids;
public function __construct($attributes = array()) {
parent::__construct($attributes);

$this->new_ids = [];

$this->package_folder = "/var/www/html/wp-content/uploads/tainacan/exporter/package/";

}

protected $steps = [
[
'name' => 'Create Taxonomies',
'progress_label' => 'Creating taxonomies',
'callback' => 'create_taxonomies'
]
];

public function set_option($key,$value) {
$this->default_options[$key] = $value;
}

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

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

public function create_taxonomies() {
$file_taxonomies = "tnc_taxonomies";

$taxonomy_repository = Repositories\Taxonomies::get_instance();
$str_json_taxonomies = file_get_contents($this->package_folder . $file_taxonomies);
$taxonomiesList = \json_decode($str_json_taxonomies);

foreach ($taxonomiesList as $tax) {
$taxonomy = new Entities\Taxonomy();
foreach ($tax as $key => $value){
$set_ = 'set_' . $key;
if (method_exists( $taxonomy, $set_ ) ) {
$taxonomy->$set_($value);
}
}
if ($taxonomy->validate()) {
$t = $taxonomy_repository->insert($taxonomy);
$this->new_ids['taxonomy'][$taxonomy->id] = $taxonomy->get_id();
}
}
}

public function create_terms() {
$file_taxonomies = "tnc_terms";

$term_repository = Repositories\Terms::get_instance();
$str_json_terms = file_get_contents($this->package_folder . $file_taxonomies);
$terms_list = \json_decode($str_json_terms);

// foreach ($terms_list as $term) {
// $new_term = new Entities\Term();
// foreach ($term as $key => $value) {
// $set_ = 'set_' . $key;
// if (method_exists( $new_term, $set_ ) ) {
// $new_term->$set_($value);
// }
// }
// if ($new_term->validate()) {

// $new_term->set_taxonomy($this->new_ids['taxonomy'][$new_term->get_taxonomy()]);

// $t = $term_repository->insert($new_term);
// $this->new_ids['term'][$term->id] = $new_term->get_id();
// }
// }

}
}

0 comments on commit a6a128b

Please sign in to comment.