<?php
    /*********************************************************************************
     * Zurmo is a customer relationship management program developed by
     * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
     *
     * Zurmo is free software; you can redistribute it and/or modify it under
     * the terms of the GNU Affero General Public License version 3 as published by the
     * Free Software Foundation with the addition of the following permission added
     * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
     * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
     * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
     *
     * Zurmo is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
     * details.
     *
     * You should have received a copy of the GNU Affero General Public License along with
     * this program; if not, see http://www.gnu.org/licenses or write to the Free
     * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
     * 02110-1301 USA.
     *
     * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
     * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
     *
     * The interactive user interfaces in original and modified versions
     * of this program must display Appropriate Legal Notices, as required under
     * Section 5 of the GNU Affero General Public License version 3.
     *
     * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
     * these Appropriate Legal Notices must retain the display of the Zurmo
     * logo and Zurmo copyright notice. If the display of the logo is not reasonably
     * feasible for technical reasons, the Appropriate Legal Notices must display the words
     * "Copyright Zurmo Inc. 2017. All rights reserved".
     ********************************************************************************/

    /**
     * Controller Class for managing languages.
     *
     */
    class JobsManagerDefaultController extends ZurmoModuleController
    {
        public function filters()
        {
            return array(
                array(
                    ZurmoBaseController::RIGHTS_FILTER_PATH,
                    'moduleClassName' => 'JobsManagerModule',
                    'rightName' => JobsManagerModule::RIGHT_ACCESS_JOBSMANAGER,
               ),
            );
        }

        public function actionIndex()
        {
            $this->actionList();
        }

        public function actionList()
        {
            $this->processListAction(null);
        }

        protected function processListAction($messageBoxContent = null)
        {
            $breadCrumbLinks = array(
                Zurmo::t('JobsManagerModule',
                    'JobsManagerModuleSingularLabel',
                    LabelUtil::getTranslationParamsForAllModules()),
            );
            $view = new JobsManagerTitleBarAndListView(
                            $this->getId(),
                            $this->getModule()->getId(),
                            JobsToJobsCollectionViewUtil::getMonitorJobData(),
                            JobsToJobsCollectionViewUtil::getNonMonitorJobsData(),
                            $messageBoxContent);
            $view = new JobsManagerPageView(ZurmoDefaultAdminViewUtil::
                            makeViewWithBreadcrumbsForCurrentUser($this, $view, $breadCrumbLinks, 'SettingsBreadCrumbView'));
            echo $view->render();
        }

        /**
         * @param string $type
         */
        public function actionResetJob($type)
        {
            assert('is_string($type) && $type != ""');
            $jobClassName = $type . 'Job';
            try
            {
                $jobInProcess      = JobInProcess::getByType($type);
                $jobInProcess->delete();
                $messageBoxContent = HtmlNotifyUtil::renderHighlightBoxByMessage(
                                     Zurmo::t('JobsManagerModule', 'The job {jobName} has been reset.',
                                         array('{jobName}' => $jobClassName::getDisplayName())));
                $this->processListAction($messageBoxContent);
            }
            catch (NotFoundException $e)
            {
                $messageBoxContent = HtmlNotifyUtil::renderHighlightBoxByMessage(
                                 Zurmo::t('JobsManagerModule', 'The job {jobName} was not found to be stuck and therefore was not reset.',
                                         array('{jobName}' => $jobClassName::getDisplayName())));
                $this->processListAction($messageBoxContent);
            }
        }

        /**
         * @param string $type
         */
        public function actionJobLogsModalList($type)
        {
            assert('is_string($type) && $type != ""');
            $jobClassName = $type . 'Job';
            $searchAttributeData = array();
            $searchAttributeData['clauses'] = array(
                1 => array(
                    'attributeName'        => 'type',
                    'operatorType'         => 'equals',
                    'value'                => $type,
                ),
            );
            $searchAttributeData['structure'] = '1';
            $pageSize     = Yii::app()->pagination->resolveActiveForCurrentUserByType('subListPageSize');
            $dataProvider = new RedBeanModelDataProvider( 'JobLog', 'startDateTime', true,
                                                                $searchAttributeData, array(
                                                                    'pagination' => array(
                                                                        'pageSize' => $pageSize,
                                                                    )
                                                                ));
            Yii::app()->getClientScript()->setToAjaxMode();
            $jobLogsListView = new JobLogsModalListView(
                                    $this->getId(),
                                    $this->getModule()->getId(),
                                    'JobLog',
                                    $dataProvider,
                                    'modal');
            $view = new ModalView($this, $jobLogsListView);
            echo $view->render();
        }

        public function actionJobLogDetails($id)
        {
            $jobLog      = JobLog::getById(intval($id));
            $detailsView = new JobLogDetailsView($this->getId(), $this->getModule()->getId(), $jobLog, strval($jobLog));
            $view   = new JobsManagerPageView(ZurmoDefaultAdminViewUtil::makeStandardViewForCurrentUser($this, $detailsView));
            echo $view->render();
        }

        public function actionRunJob($type, $timeLimit = 500, $messageLoggerClassName = 'MessageLogger')
        {
            if (!Group::isUserASuperAdministrator(Yii::app()->user->userModel))
            {
                echo Zurmo::t('JobsManagerModule', 'Only super administrators can run jobs from the browser');
                Yii::app()->end(0, false);
            }
            $breadCrumbLinks = array(
                Zurmo::t('JobsManagerModule',
                    'JobsManagerModuleSingularLabel',
                    LabelUtil::getTranslationParamsForAllModules()) => array('/jobsManager/default'),
                Zurmo::t('JobsManagerModule', 'Run Job'),
            );
            $runJobView = new RunJobView($this->getId(), $this->getModule()->getId(), $type, (int)$timeLimit, $messageLoggerClassName);
            $view = new JobsManagerPageView(ZurmoDefaultAdminViewUtil::
            makeViewWithBreadcrumbsForCurrentUser($this, $runJobView, $breadCrumbLinks, 'SettingsBreadCrumbView'));
            echo $view->render();
        }

        public function actionRunAjaxJob($type, $timeLimit = 500, $messageLoggerClassName = 'MessageLogger')
        {
            Yii::app()->getClientScript()->setToAjaxMode();
            $template = '<li>{message}</li>';
            $isJobInProgress = false;
            try
            {
                JobsManagerUtil::runFromJobManagerCommandOrBrowser($type, (int)$timeLimit, $messageLoggerClassName,
                    $isJobInProgress, true, $template);
            }
            catch (Exception $e)
            {
                echo Zurmo::t('JobsManagerModule', 'There was an error. Please check logs for more details');
            }
            Yii::app()->end(0, false);
        }

        public function actionQueueJob($type, $delay = 0, $messageLoggerClassName = 'MessageLogger')
        {
            if (!Group::isUserASuperAdministrator(Yii::app()->user->userModel))
            {
                echo Zurmo::t('JobsManagerModule', 'Only super administrators can run jobs from the browser');
                Yii::app()->end(0, false);
            }
            if (!Yii::app()->jobQueue->isEnabled())
            {
                echo Zurmo::t('JobsManagerModule', 'Job queuing must be enabled in order to queue a job');
                Yii::app()->end(0, false);
            }
            $breadCrumbLinks = array(
                Zurmo::t('JobsManagerModule',
                         'JobsManagerModuleSingularLabel',
                         LabelUtil::getTranslationParamsForAllModules()) => array('/jobsManager/default'),
                         Yii::app()->jobQueue->getQueueJobLabel(),
            );
            $messageLogger = new $messageLoggerClassName();
            $queueJobView = new QueueJobView($this->getId(), $this->getModule()->getId(), $type, (int)$delay, $messageLogger);
            $view = new JobsManagerPageView(ZurmoDefaultAdminViewUtil::
                        makeViewWithBreadcrumbsForCurrentUser($this, $queueJobView, $breadCrumbLinks, 'SettingsBreadCrumbView'));
            echo $view->render();
            $template = ZurmoHtml::script("$('#logging-table ol').append('<li>{message}</li>');");
        }
    }
?>