<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * UsersController
 * 
 * Handles the actions for users related tasks
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.0
 */

class ListingusersController extends Controller
{
    /**
     * Define the filters for various controller actions
     * Merge the filters with the ones from parent implementation
     */
    public function init()
    {


        parent::init();
    }

    /**
     * List all available users
     */
    public function actionIndex()
    {



        $request = Yii::app()->request;
        $user = new ListingUsers('search');

        $user->unsetAttributes();

        // for filters.
        $user->attributes = (array)$request->getQuery($user->modelName, array());

        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | ' . Yii::t('users', 'Customers List'),
            'pageHeading'       => Yii::t('agent', 'Customers List'),
            'pageBreadcrumbs'   => array(
                Yii::t('capacity', 'Customers') => $this->createUrl('listingusers/index'),
                Yii::t('app', 'View all')
            )
        ));

        $this->render('list', compact('user'));
    }

    /**
     * Create a new user
     */
    public function actionCreate()
    {
        $request = Yii::app()->request;
        $notify = Yii::app()->notify;
        $user = new ListingUsers();
        if ($request->isPostRequest && ($attributes = (array)$request->getPost($user->modelName, array()))) {
            $user->attributes = $attributes;


            if (!$user->save()) {
                $notify->addError(Yii::t('app', 'Your form has a few errors, please fix them and try again!'));
            } else {


                $notify->addSuccess(Yii::t('app', 'Your form has been successfully saved!'));
            }

            Yii::app()->hooks->doAction('controller_action_save_data', $collection = new CAttributeCollection(array(
                'controller' => $this,
                'success'    => $notify->hasSuccess,
                'user'       => $user,
            )));

            if ($collection->success) {
                $this->redirect(array('listingusers/index'));
            }
        }

        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | ' . Yii::t('users', 'Create Customer'),
            'pageHeading'       => Yii::t('agent', 'Create Customer'),
            'pageBreadcrumbs'   => array(
                Yii::t('agent', 'Customer') => $this->createUrl('listingusers/index'),
                Yii::t('app', 'Create new'),
            )
        ));
        $this->getData('pageScripts')->add(array('src' => Yii::app()->apps->getBaseUrl('assets/js/dropzone.min.js')));
        $this->getData('pageStyles')->add(array('src' => Yii::app()->apps->getBaseUrl('assets/css/dropzone.css')));
        $this->render('form', compact('user'));
    }

    /**
     * Update existing user
     */
    public function actionUpdate($id)
    {

        $user = ListingUsers::model()->findByPk((int)$id);

        if (empty($user)) {
            throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
        }




        $request = Yii::app()->request;
        $notify = Yii::app()->notify;

        if ($request->isPostRequest && ($attributes = (array)$request->getPost($user->modelName, array()))) {
            $user->attributes = $attributes;
            if ($user->password == "") {
                unset($user->password);
                unset($user->con_password);
            }
            if (!$user->save()) {
                $notify->addError(Yii::t('app', 'Your form has a few errors, please fix them and try again!'));
            } else {

                $notify->addSuccess(Yii::t('app', 'Your form has been successfully saved!'));
            }

            Yii::app()->hooks->doAction('controller_action_save_data', $collection = new CAttributeCollection(array(
                'controller' => $this,
                'success'    => $notify->hasSuccess,
                'user'       => $user,
            )));

            if ($collection->success) {
                $this->redirect(array('listingusers/index'));
            }
        }

        //  echo  $user->password;echo "SDSD";exit;
        $user->password = "";
        $user->con_password = "";
        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | ' . Yii::t('capacity', 'Update Customer'),
            'pageHeading'       => Yii::t('agent', 'Update Customer'),
            'pageBreadcrumbs'   => array(
                Yii::t('agent', 'Customer') => $this->createUrl('listingusers/index'),
                Yii::t('app', 'Update'),
            )
        ));
        $this->getData('pageScripts')->add(array('src' => Yii::app()->apps->getBaseUrl('assets/js/dropzone.min.js')));
        $this->getData('pageStyles')->add(array('src' => Yii::app()->apps->getBaseUrl('assets/css/dropzone.css')));
        $this->render('form', compact('user'));
    }

    /**
     * Delete existing user
     */
    public function actionDelete($id)
    {
        $user = ListingUsers::model()->findByPk((int)$id);

        if (empty($user)) {
            throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
        }


        $user->delete();
        PlaceAnAd::model()->deleteAllByAttributes(array('user_id' => $id));

        $request = Yii::app()->request;
        $notify = Yii::app()->notify;

        if (!$request->getQuery('ajax')) {
            $notify->addSuccess(Yii::t('app', 'The item has been successfully deleted!'));
            $this->redirect($request->getPost('returnUrl', array('listingusers/index')));
        }
    }
}
