<?php

class CompleteSubscription extends CAction {

    public $layout = 'default';

    public function run() {


        if (isset($_POST['NewsletterSubscriber']) && isset($_POST['NewsletterSubscriber']['id'])) {

            $id = $_POST['NewsletterSubscriber']['id'];
            $model = $this->loadModel($id);
            $model->attributes = $_POST['NewsletterSubscriber'];

            if ($model->save()) {
                $flashMessage = Yii::t('admin', 'You have successfully subscribed to the newsletter.');
                $flashMessage = '<div class="successMessage final-successMessage">' . $flashMessage . '</div>';
                $response = array(
                    'status' => 'success',
                    'message' => $flashMessage,
                    'id' => $model->id,
                );
            } else {
                $response = array(
                    'status' => 'error',
                    'message' => $model->getErrors(),
                );
            }
            echo CJSON::encode($response);
            Yii::app()->end();
        }
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer $id the ID of the model to be loaded
     * @return NewsletterSubscriber the loaded model
     * @throws CHttpException
     */
    public function loadModel($id) {
        $model = NewsletterSubscriber::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}
