admin/controllers/Utilities.php
public function export()
    {
        if (!userHasPermission('admin:admin:utilities:export')) {
            unauthorised();
        }

        $oDataExport = Factory::service('DataExport', 'nails/module-admin');
        $aSources    = $oDataExport->getAllSources();
        $aFormats    = $oDataExport->getAllFormats();

        // --------------------------------------------------------------------------

        $oInput = Factory::service('Input');
        if ($oInput->post()) {

            try {

                $oFormValidation = Factory::service('FormValidation');
                $oFormValidation->set_rules('source', '', 'required');
                $oFormValidation->set_rules('format', '', 'required');
                $oFormValidation->set_message('required', lang('fv_required'));

                if (!$oFormValidation->run()) {
                    throw new NailsException(lang('fv_there_were_errors'));
                }

                //  Validate source
                $oSelectedSource = $oDataExport->getSourceBySlug($oInput->post('source'));
                if (empty($oSelectedSource)) {
                    throw new NailsException('Invalid data source');
                }

                //  Validate format
                $oSelectedFormat = $oDataExport->getFormatBySlug($oInput->post('format'));
                if (empty($oSelectedFormat)) {
                    throw new NailsException('Invalid data format');
                }

                //  Prepare options
                $aOptions       = [];
                $aPostedOptions = getFromArray($oSelectedSource->slug, (array) $oInput->post('options'));
                foreach ($oSelectedSource->options as $aOption) {
                    $sKey            = getFromArray('key', $aOption);
                    $aOptions[$sKey] = getFromArray($sKey, $aPostedOptions);
                }

                $oDataExportModel = Factory::model('Export', 'nails/module-admin');
                $aData            = [
                    'source'  => $oSelectedSource->slug,
                    'options' => json_encode($aOptions),
                    'format'  => $oSelectedFormat->slug,
                ];
                if (!$oDataExportModel->create($aData)) {
                    throw new NailsException('Failed to schedule export.');
                }

                $this->data['success'] = 'Export Scheduled';

            } catch (\Exception $e) {
                $this->data['error'] = $e->getMessage();
            }
        }

        // --------------------------------------------------------------------------

        $oModel  = Factory::model('Export', 'nails/module-admin');
        $aRecent = $oModel->getAll([
            'where' => [['created_by', activeUser('id')]],
            'sort'  => [['created', 'desc']],
            'limit' => 10,
        ]);

        //  Pretty source labels, format labels, and options
        $aRecent = array_map(function ($oItem) use ($aSources, $aFormats) {

            //  Sources
            foreach ($aSources as $oSource) {
                if ($oSource->slug === $oItem->source) {
                    $oItem->source = $oSource->label;
                }
            }

            if (empty($oItem->source)) {
                $oItem->source = 'Unknown';
            }

            //  Formats
            foreach ($aFormats as $oFormat) {
                if ($oFormat->slug === $oItem->format) {
                    $oItem->format = $oFormat->label;
                }
            }

            if (empty($oItem->format)) {
                $oItem->format = 'Unknown';
            }

            //  Options
            $oOptions = json_decode($oItem->options);
            if ($oOptions) {
                $oItem->options = '<pre>' . json_encode($oOptions, JSON_PRETTY_PRINT) . '</pre>';
            } else {
                $oItem->options = '';
            }

            return $oItem;
        }, $aRecent);

        // --------------------------------------------------------------------------

        //  Cron running?
        $sLastRun   = appSetting('data-export-cron-last-run', 'nails/module-admin');
        $bIsRunning = false;
        if ($sLastRun) {
            $oNow       = Factory::factory('DateTime');
            $oLastRun   = new \DateTime($sLastRun);
            $iDiff      = $oNow->getTimestamp() - $oLastRun->getTimestamp();
            $bIsRunning = $iDiff <= 300;
        }
        if (!$bIsRunning) {
            $this->data['warning'] = '<strong>The data export cron job is not running</strong>';
            $this->data['warning'] .= '<br>The cron job has not been executed within the past 5 minutes.';
        }

        // --------------------------------------------------------------------------

        //  Set view data
        $this->data['page']->title = 'Export Data';
        $this->data['aSources']    = $aSources;
        $this->data['aFormats']    = $aFormats;
        $this->data['aRecent']     = $aRecent;

        // --------------------------------------------------------------------------

        //  Load assets
        $oAsset = Factory::service('Asset');
        $oAsset->load('nails.admin.export.min.js', 'NAILS');

        //  Load views
        Helper::loadView('export/index');
    }
public function export()
    {
        if (!userHasPermission('admin:admin:utilities:export')) {
            unauthorised();
        }

        $oDataExport = Factory::service('DataExport', 'nails/module-admin');
        $aSources    = $oDataExport->getAllSources();
        $aFormats    = $oDataExport->getAllFormats();

        // --------------------------------------------------------------------------

        $oInput = Factory::service('Input');
        if ($oInput->post()) {

            try {

                $oFormValidation = Factory::service('FormValidation');
                $oFormValidation->set_rules('source', '', 'required');
                $oFormValidation->set_rules('format', '', 'required');
                $oFormValidation->set_message('required', lang('fv_required'));

                if (!$oFormValidation->run()) {
                    throw new NailsException(lang('fv_there_were_errors'));
                }

                //  Validate source
                $oSelectedSource = $oDataExport->getSourceBySlug($oInput->post('source'));
                if (empty($oSelectedSource)) {
                    throw new NailsException('Invalid data source');
                }

                //  Validate format
                $oSelectedFormat = $oDataExport->getFormatBySlug($oInput->post('format'));
                if (empty($oSelectedFormat)) {
                    throw new NailsException('Invalid data format');
                }

                //  Prepare options
                $aOptions       = [];
                $aPostedOptions = getFromArray($oSelectedSource->slug, (array) $oInput->post('options'));
                foreach ($oSelectedSource->options as $aOption) {
                    $sKey            = getFromArray('key', $aOption);
                    $aOptions[$sKey] = getFromArray($sKey, $aPostedOptions);
                }

                $oDataExportModel = Factory::model('Export', 'nails/module-admin');
                $aData            = [
                    'source'  => $oSelectedSource->slug,
                    'options' => json_encode($aOptions),
                    'format'  => $oSelectedFormat->slug,
                ];
                if (!$oDataExportModel->create($aData)) {
                    throw new NailsException('Failed to schedule export.');
                }

                $this->data['success'] = 'Export Scheduled';

            } catch (\Exception $e) {
                $this->data['error'] = $e->getMessage();
            }
        }

        // --------------------------------------------------------------------------

        $oModel  = Factory::model('Export', 'nails/module-admin');
        $aRecent = $oModel->getAll([
            'where' => [['created_by', activeUser('id')]],
            'sort'  => [['created', 'desc']],
            'limit' => 10,
        ]);

        //  Pretty source labels, format labels, and options
        $aRecent = array_map(function ($oItem) use ($aSources, $aFormats) {

            //  Sources
            foreach ($aSources as $oSource) {
                if ($oSource->slug === $oItem->source) {
                    $oItem->source = $oSource->label;
                }
            }

            if (empty($oItem->source)) {
                $oItem->source = 'Unknown';
            }

            //  Formats
            foreach ($aFormats as $oFormat) {
                if ($oFormat->slug === $oItem->format) {
                    $oItem->format = $oFormat->label;
                }
            }

            if (empty($oItem->format)) {
                $oItem->format = 'Unknown';
            }

            //  Options
            $oOptions = json_decode($oItem->options);
            if ($oOptions) {
                $oItem->options = '<pre>' . json_encode($oOptions, JSON_PRETTY_PRINT) . '</pre>';
            } else {
                $oItem->options = '';
            }

            return $oItem;
        }, $aRecent);

        // --------------------------------------------------------------------------

        //  Cron running?
        $sLastRun   = appSetting('data-export-cron-last-run', 'nails/module-admin');
        $bIsRunning = false;
        if ($sLastRun) {
            $oNow       = Factory::factory('DateTime');
            $oLastRun   = new \DateTime($sLastRun);
            $iDiff      = $oNow->getTimestamp() - $oLastRun->getTimestamp();
            $bIsRunning = $iDiff <= 300;
        }
        if (!$bIsRunning) {
            $this->data['warning'] = '<strong>The data export cron job is not running</strong>';
            $this->data['warning'] .= '<br>The cron job has not been executed within the past 5 minutes.';
        }

        // --------------------------------------------------------------------------

        //  Set view data
        $this->data['page']->title = 'Export Data';
        $this->data['aSources']    = $aSources;
        $this->data['aFormats']    = $aFormats;
        $this->data['aRecent']     = $aRecent;

        // --------------------------------------------------------------------------

        //  Load assets
        $oAsset = Factory::service('Asset');
        $oAsset->load('nails.admin.export.min.js', 'NAILS');

        //  Load views
        Helper::loadView('export/index');
    }
Export data
@return void