<?php
namespace backend\controllers;
use yii\base\Action;
use backend\models\Book;
use yii\helpers\Json;
use Yii;



class ShowBookAction extends Action
{
    public function run()
    {
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';

        if($id == 0 || $id == NULL)
        {
            $sql = 'SELECT * FROM book';
            $book = Book::findBySql($sql)->all();
        }
        else{
            $book = Book::findAll('id ='.$id);
        }
        $path = Yii::$app->basePath ;
        $data=array();
        foreach ($book as $item)
        {
            $image= $path.'/web/images/book'.$item->image;
            $data[] = array(
                'id' => $id,
                'title'=> $item->title,
                'author' =>$item->author,
                'publisher'=> $item->publisher,
                'image' => $image,

            );
        }
        ApiController::sendResponse(200, JSON::encode(array(
            'status' => 'SUCCESS',
            'data' => $data,
            'message' => 'OK',
        )));

    }
}