/**
  * Saves new/existing model via AJAX call.
  *
  * @param null|string|int $slug Category slug.
  *
  * @throws \EHttpException Thrown if accessed non-ajaxly, no data sent or
  * model with specified ID doesn't exist.
  *
  * @todo update render method(s) to avoid lines of code like the last ones
  *
  * @return void
  * @since 0.1.0
  */
 public function actionAjaxSave()
 {
     if (\Yii::app()->request->isAjaxRequest && (!defined('YII_DEBUG') || !YII_DEBUG)) {
         throw new \EHttpException(400, 'badRequest.ajaxOnly');
     }
     if (!($data = \Yii::app()->request->getPost('Category'))) {
         throw new \EHttpException(400, 'badRequest.noDataReceived');
     }
     if (isset($data['id'])) {
         if (!($category = \Category::model()->findByPk($data['id']))) {
             throw new \EHttpException(404);
         }
     } else {
         $category = new \Category();
     }
     $response = array('success' => true);
     if (!$category->setAndSave($data)) {
         $response['success'] = false;
         $response['errors'] = $category->getErrors();
     } else {
         $response['data'] = $category->getPublicAttributes();
     }
     $this->page->format = 'json';
     header($this->page->generateFormatHeader());
     echo \CJSON::encode($response);
 }