Ansel_View_Base::json PHP Метод

json() публичный статический Метод

..
public static json ( Ansel_Gallery $gallery, array $params = [] ) : string
$gallery Ansel_Gallery The gallery to represent in this view
$params array An array of parameters for this method:
   full       - Should a full URL be generated? [false]
   from       - Starting image count [0]
   count      - The number of images to include (starting at from) [0]
   image_view - The type of ImageGenerator to obtain the src url for. [screen]
   view_links - Should the JSON include links to the Image and/or Gallery View? [false]
   perpage    - Number of images per page [from user prefs]
Результат string A serialized JSON array.
    public static function json(Ansel_Gallery $gallery, $params = array())
    {
        global $conf, $prefs;
        $default = array('full' => false, 'from' => 0, 'count' => 0, 'image_view' => 'screen', 'view_links' => false, 'perpage' => $prefs->getValue('tilesperpage', $conf['thumbnail']['perpage']));
        $params = array_merge($default, $params);
        $json = array();
        $curimage = 0;
        $curpage = 0;
        if (empty($params['images'])) {
            $images = $gallery->getImages($params['from'], $params['count']);
        }
        $style = $gallery->getStyle();
        if ($params['image_view'] == 'thumb' && !empty($params['generator'])) {
            $style->thumbstyle = $params['generator'];
        }
        foreach ($images as $image) {
            // Calculate the page this image will appear on in the gallery view.
            if (++$curimage > $params['perpage']) {
                ++$curpage;
                $curimage = 0;
            }
            $data = array((string) Ansel::getImageUrl($image->id, $params['image_view'], $params['full'], $style), htmlspecialchars($image->filename), $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($image->caption, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL)), $image->id, $curpage);
            if ($params['view_links']) {
                $data[] = (string) Ansel::getUrlFor('view', array('gallery' => $gallery->id, 'slug' => $gallery->get('slug'), 'image' => $image->id, 'view' => 'Image', 'page' => $curpage), true);
                $data[] = (string) Ansel::getUrlFor('view', array('gallery' => $image->gallery, 'slug' => $gallery->get('slug'), 'view' => 'Gallery'), true);
            }
            // Source, Width, Height, Name, Caption, Image Id, Gallery Page
            $json[] = $data;
        }
        return Horde_Serialize::serialize($json, Horde_Serialize::JSON);
    }

Usage Example

Пример #1
0
    /**
     * Get the HTML representing this view.
     *
     * @return string The HTML
     */
    public function html()
    {
        global $conf, $prefs, $registry;
        // Initialize the Horde_View instance.
        $view = $this->_getHordeView();
        // Get JSON data for view
        if ($this->mode == Ansel_GalleryMode_Base::MODE_NORMAL) {
            $json = Ansel_View_Base::json($this->view->gallery, array('full' => !empty($this->view->api), 'perpage' => $this->perpage));
        } else {
            if (!empty($this->date['day']) && $this->numTiles) {
                $json = Ansel_View_Base::json($this->view->gallery, array('full' => !empty($this->view->api), 'perpage' => $this->perpage));
            } else {
                $json = '[]';
            }
        }
        $date_params = Ansel::getDateParameter(array('year' => !empty($this->view->year) ? $this->view->year : 0, 'month' => !empty($this->view->month) ? $this->view->month : 0, 'day' => !empty($this->view->day) ? $this->view->day : 0));
        $pagerurl = $this->_getPagerUrl();
        $graphics_dir = Horde::url(Horde_Themes::img(), true, -1);
        $image_text = _("Photo");
        $of = _("of");
        $flipped = array_flip($date_params);
        if (count($flipped) == 1 && !empty($flipped[0])) {
            $gallery_url = $pagerurl . '?';
        } else {
            $gallery_url = $pagerurl . '&';
        }
        $js = array();
        $js[] = <<<EOT
        LightboxOptions = {
            gallery_json: {$json},
            fileLoadingImage: '{$graphics_dir}/lightbox/loading.gif',
            fileBottomNavCloseImage: '{$graphics_dir}/lightbox/closelabel.gif',
            overlayOpacity: 0.8,   // controls transparency of shadow overlay
            animate: true,         // toggles resizing animations
            resizeSpeed: 7,        // controls the speed of the image resizing animations (1=slowest and 10=fastest)
            borderSize: 10,        // if you adjust the padding in the CSS, you will need to update this variable

            // Used to write: Image # of #.
            labelImage: '{$image_text}',
            labelOf: '{$of}',
            //URL to return to when the lightbox closes
            returnURL: '{$gallery_url}',
            startPage: '{$view->page}'
        };
        document.lb = new Lightbox(LightboxOptions); if (window.location.hash.length) document.lb.start(window.location.hash.substring(1));
EOT;
        $GLOBALS['page_output']->addInlineScript($js, true);
        // Output js/css here if we are calling via the api
        if ($this->view->api) {
            Horde::startBuffer();
            global $page_output;
            $page_output->addThemeStylesheet('lightbox.css');
            $page_output->includeStylesheetFiles(array('nobase' => true), true);
            foreach (array('prototype.js', 'accesskeys.js', 'scriptaculous/effects.js') as $val) {
                $tmp = new Horde_Script_File_JsDir($val, 'horde');
                echo $tmp->tag_full;
            }
            $tmp = new Horde_Script_File_JsDir('lightbox.js');
            echo $tmp->tag_full;
            $page_output->outputInlineScript();
            $html = Horde::endBuffer();
            return $html . $view->render('gallery');
        }
        return $view->render('gallery');
    }