<?php
/**
 * @package     DOCman
 * @copyright   Copyright (C) 2019 Timble CVBA. (http://www.timble.net)
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        http://www.joomlatools.com
 */

namespace Foliolabs\Docman;

use Foliolabs\Component\Migrator;
use Foliolabs\Library;

/**
 * DOCman Importer Class.
 */
class MigratorExport extends Migrator\MigratorExportAbstract
{
    protected function _initialize(Library\ObjectConfig $config)
    {
        $config->append(array(
                'label'     => 'DOCman',
                'extension' => 'docman',
                'jobs'  => array(
                    'export_categories'         => array(
                        'label'      => 'Exporting categories',
                        'table'      => 'docman_categories'
                    ),
                    'export_category_group_access' => array(
                        'label'      => 'Exporting category group access data',
                        'table'      => 'docman_category_group_access'
                    ),
                    'export_document_group_access' => array(
                        'label'      => 'Exporting document group access data',
                        'table'      => 'docman_document_group_access'
                    ),
                    'export_category_folders' => array(
                        'label'      => 'Exporting category folders',
                        'table'      => 'docman_category_folders'
                    ),
                    'export_category_orderings' => array(
                        'label'      => 'Exporting category orderings',
                        'table'      => 'docman_category_orderings'
                    ),
                    'export_category_relations' => array(
                        'label'      => 'Exporting category relations',
                        'table'      => 'docman_category_relations'
                    ),
                    'export_documents'          => array(
                        'label'      => 'Exporting documents',
                        'table'      => 'docman_documents'
                    ),
                    'export_document_contents' => array(
                        'label'      => 'Exporting document contents',
                        'table'      => 'docman_document_contents'
                    ),
                    'export_permissions' => array(
                        'label'      => 'Exporting permissions',
                        'table'      => 'docman_permissions'
                    ),
                    'export_scans' => array(
                        'label'      => 'Exporting scans',
                        'table'      => 'docman_scans'
                    ),
                    'export_tags' => array(
                        'label'      => 'Exporting tags',
                        'table'      => 'docman_tags'
                    ),
                    'export_tags_relations' => array(
                        'label'      => 'Exporting tag relations',
                        'table'      => 'docman_tags_relations'
                    ),
                    'export_usergroups' => array(
                        'label'      => 'Exporting user groups',
                        'table'      => 'docman_usergroups'
                    ),
                    'export_usergroups_users' => array(
                        'label'      => 'Exporting user groups relations',
                        'table'      => 'docman_usergroups_users'
                    ),
                    'export_containers'         => array(
                        'label'      => 'Exporting containers',
                        'table'      => 'files_containers',
                        'callback'  => function($query) {
                            $query->where('slug IN :slugs')->bind(array(
                                'slugs' => array('docman-files', 'docman-icons', 'docman-images')
                            ));
                        }
                    ),
                    'export_options'             => array(
                        'label'      => 'Exporting options',
                        'table'      => 'options',
                        'callback'   => function($query) {
							$query
								->columns(['option_name', 'option_value', 'autoload'])
								->where('option_name LIKE :docman')
								->where('option_name NOT LIKE :docmancache')
								->bind(array(
									'docman'      => 'docman%',
									'docmancache' => 'docman_cache%'
								));
                        }
                    )/*,
                    'export_pages'             => array(
                        'label'      => 'Exporting pages',
                        'table'      => 'posts',
                        'callback'   => function($query) {
							$query
								->where('post_status <> :trashed AND post_type NOT IN :types AND (post_content LIKE :block OR post_content LIKE :shortcode)')
								->bind(array(
									'block'     => '%wp:docman%',
									'shortcode' => '%[docman%]%',
									'types'     => ['revision'],
									'trashed'   => 'trash'
								));
                        }
                    ),
                    'export_pages_metadata'             => array(
                        'label'      => 'Exporting page metadata',
                        'table'      => 'postmeta',
                        'callback'   => function($query) {
                            $subq = $this->getObject('database.query.select')
                                ->table('posts')->columns('ID')
                                ->where('post_type NOT IN :nottypes')
                                ->where('post_content LIKE :block')
                                ->bind(array(
                                    'block' => '%wp:docman%',
                                    'nottypes' => ['revision']
                                ));

                            $query
                                ->where('post_id IN :posts')
                                ->bind(array(
                                    'posts' => $subq
                                ));
                        }
                    )*/
                )
            )
        );

        parent::_initialize($config);
    }
}
