siteapi

<?php
namespace api\modules\v1\controllers;

use common\models\ContentPages;
use common\models\Contest;
use common\models\Country;
use common\models\LocalizedStrings;
use common\models\Region;
use common\models\ReportContent;
use common\models\ReportContentOptions;
use common\models\User;
use common\models\Video;
use Yii;
use yii\base\ErrorException;
use yii\db\Exception;
use yii\helpers\ArrayHelper;

/** @var User $user */
/** @var Video $video */
/** @var LocalizedStrings $LocalizedStrings */

/** @var Contest $contest */
class ApiController extends BaseApiController
{
    public function actionTestApi()
    {

        // Response params - start
        $response = [];
        $message = "";
        $this->_sendResponse($response, $message);
        // Response params - end
        // to send logical errors - status code will be always 200 but errorCode will be provided at last
        $this->_sendErrorResponse(200, "No input parameters are given", 101);
    }

    /**
     * @api {get} /api/language-data/ Get localizedStrings
     * @apiName LanguageData
     * @apiGroup api
     * @apiDescription Get Data of the given Language type default is en
     *
     * @apiVersion 0.2.0
     *
     * @apiHeader {String} token Users unique access-key.
     *
     * @apiHeaderExample {json} Header Example:
     *     {
     *       "AUTH_TOKEN": "[HASH]"
     *     }
     *
     * @apiParam {String} language .
     * @apiSuccess {String} Language data sent
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       "errorStatus" : "false",
     *       "message" : "Language data sent",
     *       "data" : {
     *          {"key" : "value"},
     *       }
     *     }
     *
     * @apiError ERROR_CODE The code of the error.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 200 ok
     *     {
     *       "errorStatus": true,
     *       "code": error code,
     *       "message": "error message"
     *     }
     */

    public function actionLanguageData()
    {
        $language = Yii::$app->request->get('language');
        $lastTimestamp = Yii::$app->request->get('lastTimestamp',0);
        try {
            if ($language === null) {
                $error_msg = "Language parameter missing";
                $this->_sendErrorResponse(200, $error_msg, 101);
            } elseif (!ArrayHelper::isIn($language, LocalizedStrings::$SupportedLanguages)) {
                $error_msg = "Language parameter is not available in supported languages";
                $this->_sendErrorResponse(200, $error_msg, 101);
            }

            $recordCount = LocalizedStrings::find()
                            ->where('created_at > FROM_UNIXTIME(:lastTimestamp)',[':lastTimestamp'=>$lastTimestamp])
                            ->orWhere('updated_at > FROM_UNIXTIME(:lastTimestamp)',[':lastTimestamp'=>$lastTimestamp])
                            ->count();

            if($recordCount > 0){
                $arrLocalStrings = LocalizedStrings::find()->select(['str_key AS key', $language . '_value AS value'])->asArray()->all();
                $localizedStrings = [];
                foreach ($arrLocalStrings as $objLocalString) {
                    $localizedStrings[$objLocalString['key']] = $objLocalString['value'];
                }
            }else{
                $localizedStrings = [];
            }
            $response['localizedStrings'] = $localizedStrings;
            $response['changedRecordsCount'] = $recordCount;
            $response['language'] = $language;
            $response['timestamp'] = time();
            $message = "Language data sent";
            $this->_sendResponse($response, $message);
            // Response params - end
        } catch (Exception $e) {
            $error_msg = "Internal server error!";
            $this->_sendErrorResponse(200, $error_msg, 101);
        }
    }

    /**
     * @api {post} /api/content-page/ Content page data
     * @apiName ReportContent
     * @apiGroup  api
     * @apiDescription Get content page data
     *
     * @apiVersion 0.2.0
     *
     * @apiHeader {String} token Users unique access-key.
     *
     * @apiHeaderExample {json} Header Example:
     *     {
     *       "AUTH_TOKEN": "[HASH]"
     *     }
     *
     * @apiParam {String} content.
     * @apiSuccess {String} Content page data sent
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *         "errorStatus" : "false",
     *        "timestamp": 1473053349,
     *        "message": "Content page data sent"
     *       "data" : {
     *          {
     *              "terms_and_condition": "",
     *               "language":
     *          }
     *      }
     *     }
     *
     * @apiError ERROR_CODE The code of the error.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 200 ok
     *     {
     *       "errorStatus": true,
     *       "code": "error code",
     *       "message": "error message"
     *     }
     */

    public function actionContentPage()
    {

        $language = Yii::$app->request->get('language');
        $pageKey = Yii::$app->request->get('page_key');
        try {
            if($pageKey === null)
            {
                $error_msg = "Page-key parameter missing";
                $this->_sendErrorResponse(200, $error_msg, 101);
            }
            else if ($language === null) {
                $error_msg = "Language parameter missing";
                $this->_sendErrorResponse(200, $error_msg, 101);
            } elseif (!ArrayHelper::isIn($language, LocalizedStrings::$SupportedLanguages)) {
                $error_msg = "Language parameter is not available in supported languages";
                $this->_sendErrorResponse(200, $error_msg, 101);
            }

            $contentPage = ContentPages::find()
                            ->where(['page_key'=>$pageKey])
                            ->select(['page_key AS key', $language . '_value AS value'])->asArray()->one();

            if($contentPage){
                $response[$contentPage['key']] = $contentPage['value'];
                $response['language'] = $language;
                $message = "Content page data sent";
                $this->_sendResponse($response, $message);
            }
            else{
                $error_msg = "sorry, no content page found with {$pageKey}";
                $this->_sendErrorResponse(200, $error_msg, 101);
            }
            // Response params - end
        } catch (Exception $e) {
            $error_msg = "Internal server error!";
            $this->_sendErrorResponse(200, $error_msg, 101);
        }
    }

    /**
     * @api {get} /api/report-content-options/ Store user feedback for particular video
     * @apiName ReportContentOptions
     * @apiGroup  api
     * @apiDescription Store user feedback for particular video
     *
     * @apiVersion 0.2.0
     *
     *@apiSuccess {String} Report-content options sent
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {  "errorStatus": false,
     *        "timestamp": 1473053349,
     *        "message": ""
     *       "data" : {
     *       "report_content_options":
     *         {
     *          {  "id": "1",
     *              "value": "Not interested in this content",
     *              "created_at": "2016-09-22 14:34:13",
     *              "updated_at": "2016-09-22 14:37:01"
     *          }
     *        }
     *      }
     *     }
     *
     * @apiError ERROR_CODE The code of the error.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 200 ok
     *     {
     *       "errorStatus": true,
     *       "code": "error code",
     *       "message": "error message"
     *     }
     */

    public function actionReportContentOptions()
    {
        $report_content_options = ReportContentOptions::find()->asArray()->all();
        $response = ['report_content_options' => $report_content_options];
        $message = 'Report-content options sent!';
        $this->_sendResponse($response, $message);
    }

    public function actionCountryList()
    {
        $arrCountries = Country::find()->asArray()->all();
        $response = ['countries' => $arrCountries ];
        $message = 'Country-list data sent!';
        $this->_sendResponse($response, $message);
    }
}
Advertisements