示例#1
0
    /**
     * Load items by the given categories or collection ID
     * After the Items are loaded create a map of loaded items by categories
     *
     * @param array of category ids
     * @param integer collection ID
     * @return boolean true if load items was required and it was loaded successfully, false otherwise
     */
    function load_by_categories($cat_array, $coll_ID)
    {
        global $DB, $posttypes_specialtypes;
        if (empty($cat_array) && empty($coll_ID)) {
            // Nothing to load
            return false;
        }
        // In case of an empty cat_array param, use categoriesfrom the given collection
        if (empty($cat_array)) {
            // Get all categories from the given subset
            $ChapterCache =& get_ChapterCache();
            $subset_chapters = $ChapterCache->get_chapters_by_subset($coll_ID);
            $cat_array = array();
            foreach ($subset_chapters as $Chapter) {
                $cat_array[] = $Chapter->ID;
            }
        }
        // Check which category is not loaded
        $not_loaded_cat_ids = array();
        foreach ($cat_array as $cat_ID) {
            if (!isset($this->items_by_cat_map[$cat_ID])) {
                // This category is not loaded
                $not_loaded_cat_ids[] = $cat_ID;
                // Initialize items_by_cat_map for this cat_ID
                $this->items_by_cat_map[$cat_ID] = array('items' => array(), 'sorted' => false);
            }
        }
        if (empty($not_loaded_cat_ids)) {
            // Requested categories items are all loaded
            return false;
        }
        // Query to load all Items from the given categories
        $sql = 'SELECT postcat_cat_ID as cat_ID, postcat_post_ID as post_ID FROM T_postcats
					WHERE postcat_cat_ID IN ( ' . implode(', ', $not_loaded_cat_ids) . ' )
					ORDER BY postcat_post_ID';
        $cat_posts = $DB->get_results($sql, ARRAY_A, 'Get all category post ids pair by category');
        // Initialize $Blog from coll_ID
        $BlogCache =& get_BlogCache();
        $Blog = $BlogCache->get_by_ID($coll_ID);
        $visibility_statuses = is_admin_page() ? get_visibility_statuses('keys', array('trash')) : get_inskin_statuses($coll_ID, 'post');
        // Create ItemQuery for loading visible items
        $ItemQuery = new ItemQuery($this->dbtablename, $this->dbprefix, $this->dbIDname);
        // Set filters what to select
        $ItemQuery->SELECT($this->dbtablename . '.*');
        $ItemQuery->where_chapter2($Blog, $not_loaded_cat_ids, "");
        $ItemQuery->where_visibility($visibility_statuses);
        $ItemQuery->where_datestart(NULL, NULL, NULL, NULL, $Blog->get_timestamp_min(), $Blog->get_timestamp_max());
        $ItemQuery->where_types('-' . implode(',', $posttypes_specialtypes));
        // Clear previous items from the cache and load by the defined SQL
        $this->clear(true);
        $this->load_by_sql($ItemQuery);
        foreach ($cat_posts as $row) {
            // Iterate through the post - cat pairs and fill the map
            if (empty($this->cache[$row['post_ID']])) {
                // The Item was not loaded because it does not correspond to the defined filters
                continue;
            }
            // Add to the map
            $this->items_by_cat_map[$row['cat_ID']]['items'][] = $this->get_by_ID($row['post_ID']);
        }
    }
    /**
     * Display the widget!
     *
     * @param array MUST contain at least the basic display params
     */
    function display($params)
    {
        global $localtimenow, $DB, $Blog;
        $this->init_display($params);
        $blog_ID = intval($this->disp_params['blog_ID']);
        if (empty($blog_ID)) {
            // Use current blog by default
            $blog_ID = $Blog->ID;
        }
        $BlogCache =& get_BlogCache();
        if (!$BlogCache->get_by_ID($blog_ID, false, false)) {
            // No blog exists
            return;
        }
        // Display photos:
        // TODO: permissions, complete statuses...
        // TODO: A FileList object based on ItemListLight but adding File data into the query?
        //          overriding ItemListLigth::query() for starters ;)
        // Init caches
        $FileCache =& get_FileCache();
        $ItemCache =& get_ItemCache();
        // Query list of files and posts fields:
        // Note: We use ItemQuery to get attachments from all posts which should be visible ( even in case of aggregate blogs )
        $ItemQuery = new ItemQuery($ItemCache->dbtablename, $ItemCache->dbprefix, $ItemCache->dbIDname);
        $ItemQuery->SELECT('post_ID, post_datestart, post_datemodified, post_main_cat_ID, post_urltitle, post_canonical_slug_ID,
									post_tiny_slug_ID, post_ityp_ID, post_title, post_excerpt, post_url, file_ID, file_type,
									file_title, file_root_type, file_root_ID, file_path, file_alt, file_desc, file_path_hash');
        $ItemQuery->FROM_add('INNER JOIN T_links ON post_ID = link_itm_ID');
        $ItemQuery->FROM_add('INNER JOIN T_files ON link_file_ID = file_ID');
        $ItemQuery->where_chapter($blog_ID);
        if ($this->disp_params['item_visibility'] == 'public') {
            // Get images only of the public items
            $ItemQuery->where_visibility(array('published'));
        } else {
            // Get image of all available posts for current user
            $ItemQuery->where_visibility(NULL);
        }
        $ItemQuery->WHERE_and('( file_type = "image" ) OR ( file_type IS NULL )');
        $ItemQuery->WHERE_and('post_datestart <= \'' . remove_seconds($localtimenow) . '\'');
        $ItemQuery->WHERE_and('link_position != "cover"');
        if (!empty($this->disp_params['item_type'])) {
            // Get items only with specified type
            $ItemQuery->WHERE_and('post_ityp_ID = ' . intval($this->disp_params['item_type']));
        }
        $ItemQuery->GROUP_BY('link_ID');
        // fp> TODO: because no way of getting images only, we get 4 times more data than requested and hope that 25% at least will be images :/
        // asimo> This was updated and we get images and those files where we don't know the file type yet. Now we get 2 times more data than requested.
        // Maybe it would be good to get only the requested amount of files, because after a very short period the file types will be set for all images.
        $ItemQuery->LIMIT(intval($this->disp_params['limit']) * 2);
        $ItemQuery->ORDER_BY(gen_order_clause($this->disp_params['order_by'], $this->disp_params['order_dir'], 'post_', 'post_ID ' . $this->disp_params['order_dir'] . ', link_ID'));
        // Init FileList with the above defined query
        $FileList = new DataObjectList2($FileCache);
        $FileList->sql = $ItemQuery->get();
        $FileList->query(false, false, false, 'Media index widget');
        $layout = $this->disp_params['thumb_layout'];
        $nb_cols = $this->disp_params['grid_nb_cols'];
        $count = 0;
        $r = '';
        /**
         * @var File
         */
        while ($File =& $FileList->get_next()) {
            if ($count >= $this->disp_params['limit']) {
                // We have enough images already!
                break;
            }
            if (!$File->is_image()) {
                // Skip anything that is not an image
                // Only images are selected or those files where we don't know the file type yet.
                // This check is only for those files where we don't know the filte type. The file type will be set during the check.
                continue;
            }
            if ($layout == 'grid') {
                // Grid layout
                if ($count % $nb_cols == 0) {
                    $r .= $this->disp_params['grid_colstart'];
                }
                $r .= $this->disp_params['grid_cellstart'];
            } elseif ($layout == 'flow') {
                // Flow block layout
                $r .= $this->disp_params['flow_block_start'];
            } else {
                // List layout
                $r .= $this->disp_params['item_start'];
            }
            // 1/ Hack a dirty permalink( will redirect to canonical):
            // $link = url_add_param( $Blog->get('url'), 'p='.$post_ID );
            // 2/ Hack a link to the right "page". Very daring!!
            // $link = url_add_param( $Blog->get('url'), 'paged='.$count );
            // 3/ Instantiate a light object in order to get permamnent url:
            $ItemLight = new ItemLight($FileList->get_row_by_idx($FileList->current_idx - 1));
            // index had already been incremented
            $r .= '<a href="' . $ItemLight->get_permanent_url() . '">';
            // Generate the IMG THUMBNAIL tag with all the alt, title and desc if available
            $r .= $File->get_thumb_imgtag($this->disp_params['thumb_size'], '', '', $ItemLight->title);
            $r .= '</a>';
            if ($this->disp_params['disp_image_title']) {
                // Dislay title of image or item
                $title = $File->get('title') ? $File->get('title') : $ItemLight->title;
                if (!empty($title)) {
                    $r .= '<span class="note">' . $title . '</span>';
                }
            }
            ++$count;
            if ($layout == 'grid') {
                // Grid layout
                $r .= $this->disp_params['grid_cellend'];
                if ($count % $nb_cols == 0) {
                    $r .= $this->disp_params['grid_colend'];
                }
            } elseif ($layout == 'flow') {
                // Flow block layout
                $r .= $this->disp_params['flow_block_end'];
            } else {
                // List layout
                $r .= $this->disp_params['item_end'];
            }
        }
        // Exit if no files found
        if (empty($r)) {
            return;
        }
        echo $this->disp_params['block_start'];
        // Display title if requested
        $this->disp_title();
        echo $this->disp_params['block_body_start'];
        if ($layout == 'grid') {
            // Grid layout
            echo $this->disp_params['grid_start'];
        } elseif ($layout == 'flow') {
            // Flow block layout
            echo $this->disp_params['flow_start'];
        } else {
            // List layout
            echo $this->disp_params['list_start'];
        }
        echo $r;
        if ($layout == 'grid') {
            // Grid layout
            if ($count && $count % $nb_cols != 0) {
                echo $this->disp_params['grid_colend'];
            }
            echo $this->disp_params['grid_end'];
        } elseif ($layout == 'flow') {
            // Flow block layout
            echo $this->disp_params['flow_end'];
        } else {
            // List layout
            echo $this->disp_params['list_end'];
        }
        echo $this->disp_params['block_body_end'];
        echo $this->disp_params['block_end'];
        return true;
    }