Tbfileupload Preload Json Of Files Already In Its Default Upload Directory

[size="4"][font="Arial"]Our team is using yiibooster for development of GUI. Its a very nice Booster 3 yii version pack.

So in this current case we encountered a problem of preloading an array of files for downloading and deleting, from the uploads directory.

So here is action which invokes the page, that loads the uploader:[/font][/size]


    public function actionUpdateAd($id, $page) {

        $tab = 'advertisement';

        $tabsub = 'addnew';

        switch ($page) {

            case 1:

                $tabsubs = 'first';

                break;

            case 2:

                $tabsubs = 'second';

                break;

        }


        $categorylist = Categoriestree::getCategoryOption();

        $citylist = User::getCityList();

        $model = $this->loadModelAd($id);


        $picture = $model->getImageUrl();

        $thumber = $model->getImagethumbUrl();


        $modelpicture = new AdvertisementPhoto('upload');


        if (isset($_POST['Advertisement'])) {

            $model->attributes = $_POST['Advertisement'];

            if ($model->validate()) {


                if (!is_dir(ads . $model->id)) {

                    mkdir(ads . $model->id, 0755);

                    mkdir(ads . $model->id . '/gallery', 0755);

                }

                $model->picture = CUploadedFile::getInstance($model, 'picture');


                if ($model->picture !== null && $model->picture != '') {

                    if (is_file(ads . $model->id . '/' . $model->photo_url)) {

                        unlink(ads . $model->id . '/' . $model->photo_url);

                    }

                    if (is_file(ads . $model->id . '/thumb_' . $model->photo_url)) {

                        unlink(ads . $model->id . '/thumb_' . $model->photo_url);

                    }

                    $model->picture->saveAs(

                            ads . $model->id . '/' . $model->picture->name);

                    $thumb = Yii::app()->phpThumb->create(ads . $model->id . '/' . $model->picture->name);

                    $thumb->resize(0, 150);

                    $thumb->cropFromCenter(150, 150);

                    $thumb->save(ads . $model->id . '/thumb_' . $model->picture->name);

                    $model->photo_url = $model->picture->name;

                }

                $model->save();

                $tabsubs = 'second';

            }

        }


        // Viskas kas susije su galerija //

        $gallery = new AdvertisementPhoto();

        $gallery = $gallery->findAllByAttributes(array('advertisement_id' => $id));

        $gallery = AdvertisementPhoto::getImageArray($gallery);

        // Galerijos duomenu pabaiga //


        if ($tabsubs == 'first') {

            $pagerContent = '<br /><br />';

        } elseif ($tabsubs == 'second') {

            $pagerContent = '<div style="float:right">

                <input type="button" class="btn button-next" name="next" value="Next" />

                <!--<input type="button" class="btn button-last" name="last" value="Last" />-->

          </div>

          <div style="float:left">

                <!--<input type="button" class="btn button-first" name="first" value="First" />-->

                <input type="button" class="btn button-previous" name="previous" value="Previous" />

	   </div><br /><br />';

        } else {

            $pagerContent = '<br /><br />';

        }


        $this->render('index', array(

            'model' => $model,

            'tab' => $tab,

            'tabsub' => $tabsub,

            'tabsubs' => $tabsubs,

            'categorylist' => $categorylist,

            'citylist' => $citylist,

            'picture' => $picture,

            'thumb' => $thumber,

            'modelpicture' => $modelpicture,

            'pagerContent' => $pagerContent,

            'gallery' => $gallery,

            'data' => CJSON::encode($gallery)

                )

        );

    }



[size="4"][font="Arial"]Here is actionUpload:[/font][/size]




    public function actionUpload($id) {

        header('Vary: Accept');

        if (isset($_SERVER['HTTP_ACCEPT']) &&

                (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {

            header('Content-type: application/json');

        } else {

            header('Content-type: text/plain');

        }


        $data = array();

        $model = new AdvertisementPhoto('upload');

        $model->picture = CUploadedFile::getInstance($model, 'picture');

        $model->advertisement_id = $id;

        if ($model->picture !== null && $model->validate(array('picture'))) {

            dir(ads . $id . '/gallery') ? null : mkdir(ads . $id . '/gallery', 0755);

            $model->picture->saveAs(ads . $id . '/gallery/' . $model->picture->name);

            $thumb = Yii::app()->phpThumb->create(ads . $id . '/gallery/' . $model->picture->name);

            $thumb->resize(0, 150);

            $thumb->cropFromCenter(150, 150);

            $thumb->save(ads . $id . '/gallery/thumb_' . $model->picture->name);

            $model->url = $model->picture->name;

            $title = explode('.', $model->picture->name);

            $model->title = $title[0];

            // save picture name

            if ($model->save()) {

                // return data to the fileuploader

                $data[] = array(

                    'name' => $model->picture->name,

                    'type' => $model->picture->type,

                    'size' => $model->picture->size,

                    // we need to return the place where our image has been saved

                    'url' => $model->getImageUrl($id), // Should we add a helper method?

                    // we need to provide a thumbnail url to display on the list

                    // after upload. Again, the helper method now getting thumbnail.

                    'thumbnail_url' => $model->getImageThumbUrl($id),

                    // we need to include the action that is going to delete the picture

                    // if we want to after loading

                    'delete_url' => $this->createUrl('user/uploaddelete', array('id' => $model->id, 'method' => 'uploader')),

                    'delete_type' => 'POST'

                );

            } else {

                $data[] = array('error' => 'Unable to save model after saving picture');

            }

        } /*elseif ($_GET["_method"] == "list") {

            $gallery = new AdvertisementPhoto();

            $gallery = $gallery->findAllByAttributes(array('advertisement_id' => $id));

            $gallery = AdvertisementPhoto::getImageArray($gallery);

            if ($gallery !== null) {

                echo json_encode($gallery);

            }

        }*/ else {

            if ($model->hasErrors('pictures')) {

                $data[] = array('error', $model->getErrors('picture'));

            } else {

                throw new CHttpException(500, "Could not upload file " . CHtml::errorSummary($model));

            }

        }

        // JQuery File Upload expects JSON data


        echo json_encode($data);

    }



[size="4"][font="Arial"]Here is function which fetches array for json encoding, which I would like to be used by TbFileUpload, for rendering a list of files already present in the directory, for them to be downloaded or deleted from there. Function for deleting is present and it works, when file is fresh after upload, but if you want to update the record with changes the list of uploaded files is gone.

[/font][/size]


    public function getImageArray($data) {

        $array = array();

        foreach ($data as $attributes) {

            $url = $attributes->attributes['url'];

            $id = $attributes->attributes['advertisement_id'];

            $entry_id = $attributes->attributes['id'];

            $array[] = array(

                'id'=>$entry_id,

                'url' => Yii::app()->getBaseUrl() . '/images/ads/' . $id . '/gallery/' . $url,

                'name'=>$url,

                'thumbnail_url' => Yii::app()->getBaseUrl() . '/images/ads/' . $id . '/gallery/thumb_' . $url,

                'delete_url' => $this->createUrl('user/uploaddelete', array('id' => $entry_id, 'method' => 'uploader')),

                'delete_type' => 'POST'

            );

        }

        return $array;

    }

[size="4"][font="Arial"]And here is the code for the view, which is renderedPartial after main render from controller.[/font][/size]




<?php

define('BaseUrl', Yii::app()->getBaseUrl(true) . '/');

$collapse = $this->beginWidget('TbCollapse'); ?>

<div style="margin: 20px auto; padding: 10px auto;" >


    <div class="panel-group" id="accordion">

        <div class="panel panel-default">

            <div class="panel-heading">

                <h4 class="panel-title">

                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">

                        Straipsnio iškėlimas

                    </a>

                </h4>

            </div>

            <div id="collapseOne" class="panel-collapse collapse in">

                <div class="panel-body">

                </div>

            </div>

        </div>

        <div class="panel panel-default">

            <div class="panel-heading">

                <h4 class="panel-title">

                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">

                        Nuotraukų galerija

                    </a>

                </h4>

            </div>

            <div id="collapseTwo" class="panel-collapse collapse">

                <div class="panel-body">

                    <?php

                    $this->widget('TbFileUpload', array(

                        'id'=>'fileupload',

                        'url' => $this->createUrl("user/upload", array('id' => $model->id)),

                        'model' => $modelpicture,

                        'attribute' => 'picture', // see the attribute?

                        'multiple' => true,

                        'options' => array(

                            'maxFileSize' => 3000000,

                            'acceptFileTypes' => 'js:/(\.|\/)(gif|jpe?g|png)$/i',

                )));

                    ?>

                    

                 <?php $this->renderPartial('adform/gallery', array('gallery'=>$gallery)); ?>

                </div>

            </div>

        </div>

        <div class="panel panel-default">

            <div class="panel-heading">

                <h4 class="panel-title">

                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">

                        Video galerija

                    </a>

                </h4>

            </div>

            <div id="collapseThree" class="panel-collapse collapse">

                <div class="panel-body">


                </div>

            </div>

        </div>

        <div class="panel panel-default">

            <div class="panel-heading">

                <h4 class="panel-title">

                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseFour">

                        Audio galerija

                    </a>

                </h4>

            </div>

            <div id="collapseFour" class="panel-collapse collapse">

                <div class="panel-body">

                    Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

                </div>

            </div>

        </div>

    </div>

</div>

<?php $this->endWidget();


 /*       Yii::app()->clientScript->registerScript('fileupload-AdvertisementPhoto-form',

            '$.getJSON("' .$this->createUrl("upload", array("id" => $model->id, "_method" => "list")). '", function (result) {

                    var objForm = $("#fileupload-AdvertisementPhoto-form");

                    if (result && result.length) {

                        objForm.fileupload("option", "done").call(objForm, null, {result: result});

                    }

                });

            ');*/

?>



[size=“4”][font=“Arial”]Question: how to make the list of files which is already in the directory, where the files for gallery are present to be loaded in the list below uploaded for removing, incase you’d like to change files you want to use?

Bascially the best thing would be, how to load that json into that script so it would be read properly.[/font][/size]