/**
  * Get list of attached files
  * 
  * INNER JOIN on files ensures we only get back file links
  * 
  * @param integer Limit max result
  * @param string Restrict to files/images linked to a specific position.
  *               Position can be 'teaser'|'aftermore'|'inline'
  *               Use comma as separator
  * @param string order by
  * @return DataObjectList2 on success or NULL if no linked files found
  */
 function get_attachment_FileList($limit = 1000, $position = NULL, $order = 'link_ID')
 {
     if (!isset($GLOBALS['files_Module'])) {
         return NULL;
     }
     load_class('_core/model/dataobjects/_dataobjectlist2.class.php', 'DataObjectList2');
     $FileCache =& get_FileCache();
     $FileList = new DataObjectList2($FileCache);
     // IN FUNC
     $SQL = new SQL();
     $SQL->SELECT('file_ID, file_title, file_root_type, file_root_ID, file_path, file_alt, file_desc, link_ID');
     $SQL->FROM('T_links INNER JOIN T_files ON link_file_ID = file_ID');
     $SQL->WHERE($this->get_where_condition());
     if (!empty($position)) {
         global $DB;
         $position = explode(',', $position);
         $SQL->WHERE_and('link_position IN ( ' . $DB->quote($position) . ' )');
     }
     //$SQL->ORDER_BY( $order );
     $SQL->ORDER_BY('link_order');
     $SQL->LIMIT($limit);
     $FileList->sql = $SQL->get();
     $FileList->query(false, false, false, 'get_attachment_FileList');
     if ($FileList->result_num_rows == 0) {
         // Nothing found
         $FileList = NULL;
     }
     return $FileList;
 }
 /**
  * If the list is sorted by category...
  *
  * Note: this only supports one level of categories (nested cats will be flatened)
  */
 function &get_category_group()
 {
     global $row;
     if (empty($this->current_Obj)) {
         // Very first call
         // Do a normal get_next()
         parent::get_next();
     }
     if (empty($this->current_Obj)) {
         // We have reached the end of the list
         return $this->current_Obj;
     }
     $this->group_by_cat = 1;
     // Memorize main cat
     $this->main_cat_ID = $this->current_Obj->main_cat_ID;
     return $this->current_Obj;
 }
Beispiel #3
0
    /**
     * Run Query: GET DATA ROWS *** HEAVY ***
     */
    function query()
    {
        global $DB;
        if (!is_null($this->rows)) {
            // Query has already executed:
            return;
        }
        // INIT THE QUERY:
        $this->query_init();
        $select_temp_order = '';
        if (!empty($this->ItemQuery->order_by) && strpos($this->ItemQuery->order_by, 'post_order') !== false) {
            // Move the items with NULL order to the end of the list
            $select_temp_order = ', IF( post_order IS NULL, 999999999, post_order ) AS temp_order';
            $this->ItemQuery->ORDER_BY(str_replace('post_order', 'temp_order', $this->ItemQuery->get_order_by('')));
        }
        // Results style orders:
        // $this->ItemQuery->ORDER_BY_prepend( $this->get_order_field_list() );
        // We are going to proceed in two steps (we simulate a subquery)
        // 1) we get the IDs we need
        // 2) we get all the other fields matching these IDs
        // This is more efficient than manipulating all fields at once.
        // *** STEP 1 ***
        // walter> Accordding to the standart, to DISTINCT queries, all columns used
        // in ORDER BY must appear in the query. This make que query work with PostgreSQL and
        // other databases.
        // fp> That can dramatically fatten the returned data. You must handle this in the postgres class (check that order fields are in select)
        $step1_sql = 'SELECT DISTINCT ' . $this->Cache->dbIDname . $select_temp_order . $this->ItemQuery->get_from() . $this->ItemQuery->get_where() . $this->ItemQuery->get_group_by() . $this->ItemQuery->get_order_by() . $this->ItemQuery->get_limit();
        // echo $DB->format_query( $step1_sql );
        // Get list of the IDs we need:
        $ID_list = implode(',', $DB->get_col($step1_sql, 0, 'ItemList2::Query() Step 1: Get ID list'));
        // *** STEP 2 ***
        $this->sql = 'SELECT *' . $select_temp_order . '
			              FROM ' . $this->Cache->dbtablename;
        if (!empty($ID_list)) {
            $this->sql .= ' WHERE ' . $this->Cache->dbIDname . ' IN (' . $ID_list . ') ' . $this->ItemQuery->get_order_by();
        } else {
            $this->sql .= ' WHERE 0';
        }
        //echo $DB->format_query( $this->sql );
        // ATTENTION: we skip the parent on purpose here!! fp> refactor
        DataObjectList2::query(false, false, false, 'ItemList2::Query() Step 2');
    }
 /**
  * Run Query: GET DATA ROWS *** HEAVY ***
  */
 function query()
 {
     global $DB, $Session, $localtimenow;
     if (!is_null($this->rows)) {
         // Query has already executed:
         return;
     }
     // INIT THE QUERY:
     $this->query_init();
     // We are going to proceed in two steps (we simulate a subquery)
     // 1) we get the IDs we need
     // 2) we get all the other fields matching these IDs
     // This is more efficient than manipulating all fields at once.
     // *** STEP 1 ***
     $user_IDs = isset($this->filters['users']) ? $this->filters['users'] : array();
     if ($this->refresh_query || $localtimenow - $Session->get($this->filterset_name . '_refresh_time') > 7200) {
         // We should create new list of user IDs
         global $Timer;
         $Timer->start('Users_IDs', false);
         $step1_SQL = new SQL();
         $step1_SQL->SELECT('T_users.user_ID, IF( user_avatar_file_ID IS NOT NULL, 1, 0 ) as has_picture, COUNT( DISTINCT blog_ID ) AS nb_blogs');
         if (!empty($this->filters['reported']) && $this->filters['reported']) {
             // Filter is set to 'Reported users'
             $step1_SQL->SELECT_add(', COUNT( DISTINCT urep_reporter_ID ) AS user_rep');
         }
         $step1_SQL->FROM($this->UserQuery->get_from(''));
         $step1_SQL->WHERE($this->UserQuery->get_where(''));
         $step1_SQL->GROUP_BY($this->UserQuery->get_group_by(''));
         $step1_SQL->ORDER_BY($this->UserQuery->get_order_by(''));
         $step1_SQL->LIMIT(0);
         // Get list of the IDs we need:
         $user_IDs = $DB->get_col($step1_SQL->get(), 0, 'UserList::Query() Step 1: Get ID list');
         // Update filter with user IDs
         $this->filters['users'] = $user_IDs;
         $this->save_filterset();
         $Timer->stop('Users_IDs');
     }
     // GET TOTAL ROW COUNT:
     parent::count_total_rows(count($user_IDs));
     // Pagination, Get user IDs from array for current page
     $user_IDs_paged = array_slice($user_IDs, ($this->page - 1) * $this->limit, $this->limit);
     // *** STEP 2 ***
     $step2_SQL = $this->UserQuery;
     if (!empty($user_IDs_paged)) {
         // Init sql query to get users by IDs
         $step2_SQL->WHERE($this->Cache->dbIDname . ' IN (' . implode(',', $user_IDs_paged) . ') ');
         $step2_SQL->ORDER_BY('FIND_IN_SET( user_ID, "' . implode(',', $user_IDs_paged) . '" )');
     } else {
         // No users
         $step2_SQL->WHERE('user_ID IS NULL');
     }
     $this->sql = $step2_SQL->get();
     // ATTENTION: we skip the parent on purpose here!! fp> refactor
     DataObjectList2::query(false, false, false, 'UserList::Query() Step 2');
 }
    /**
     * Display the widget!
     *
     * @param array MUST contain at least the basic display params
     */
    function display($params)
    {
        global $localtimenow;
        $this->init_display($params);
        if ($this->disp_params['order_by'] == 'RAND' && isset($this->BlockCache)) {
            // Do NOT cache if display order is random
            $this->BlockCache->abort_collect();
        }
        global $Blog;
        $list_blogs = $this->disp_params['blog_ID'] ? $this->disp_params['blog_ID'] : $Blog->ID;
        //pre_dump( $list_blogs );
        // 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 ;)
        $FileCache =& get_FileCache();
        $FileList = new DataObjectList2($FileCache);
        // Query list of files:
        $SQL = new SQL();
        $SQL->SELECT('post_ID, post_datestart, post_datemodified, post_main_cat_ID, post_urltitle, post_canonical_slug_ID,
									post_tiny_slug_ID, post_ptyp_ID, post_title, post_excerpt, post_url, file_ID,
									file_title, file_root_type, file_root_ID, file_path, file_alt, file_desc');
        $SQL->FROM('T_categories INNER JOIN T_postcats ON cat_ID = postcat_cat_ID
									INNER JOIN T_items__item ON postcat_post_ID = post_ID
									INNER JOIN T_links ON post_ID = link_itm_ID
									INNER JOIN T_files ON link_file_ID = file_ID');
        $SQL->WHERE('cat_blog_ID IN (' . $list_blogs . ')');
        // fp> TODO: want to restrict on images :]
        $SQL->WHERE_and('post_status = "published"');
        // TODO: this is a dirty hack. More should be shown.
        $SQL->WHERE_and('post_datestart <= \'' . remove_seconds($localtimenow) . '\'');
        if (!empty($this->disp_params['item_type'])) {
            // Get items only with specified type
            $SQL->WHERE_and('post_ptyp_ID = ' . intval($this->disp_params['item_type']));
        }
        $SQL->GROUP_BY('link_ID');
        $SQL->LIMIT($this->disp_params['limit'] * 4);
        // fp> TODO: because we have no way of getting images only, we get 4 times more data than requested and hope that 25% at least will be images :/
        $SQL->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'));
        $FileList->sql = $SQL->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
                // fp> TODO: maybe this property should be stored in link_ltype_ID or in the files table
                continue;
            }
            if ($layout == 'grid') {
                if ($count % $nb_cols == 0) {
                    $r .= $this->disp_params['grid_colstart'];
                }
                $r .= $this->disp_params['grid_cellstart'];
            } else {
                $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']) {
                $title = $File->get('title') ? $this->get('title') : $ItemLight->title;
                $r .= '<span class="note">' . $title . '</span>';
            }
            ++$count;
            if ($layout == 'grid') {
                $r .= $this->disp_params['grid_cellend'];
                if ($count % $nb_cols == 0) {
                    $r .= $this->disp_params['grid_colend'];
                }
            } else {
                $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();
        if ($layout == 'grid') {
            echo $this->disp_params['grid_start'];
        } else {
            echo $this->disp_params['list_start'];
        }
        echo $r;
        if ($layout == 'grid') {
            if ($count && $count % $nb_cols != 0) {
                echo $this->disp_params['grid_colend'];
            }
            echo $this->disp_params['grid_end'];
        } else {
            echo $this->disp_params['list_end'];
        }
        echo $this->disp_params['block_end'];
        return true;
    }
 /**
  * Get next object in list
  */
 function &get_next()
 {
     $Comment =& parent::get_next();
     if (empty($Comment)) {
         $r = false;
         return $r;
     }
     return $Comment;
 }
 /**
  * Template function: display message if list is empty
  *
  * @return boolean true if empty
  */
 function display_if_empty($params = array())
 {
     // Make sure we are not missing any param:
     $params = array_merge(array('msg_empty' => T_('No comment yet...')), $params);
     return parent::display_if_empty($params);
 }
 /**
  * Get list of attached Links
  *
  * @param integer Limit max result
  * @param string Restrict to files/images linked to a specific position.
  *               Position can be 'teaser'|'aftermore'|'inline'
  *               Use comma as separator
  * @param string File type: 'image', 'audio', 'other'; NULL - to select all
  * @param array Params
  * @return DataObjectList2 on success or NULL if no linked files found
  */
 function get_attachment_LinkList($limit = 1000, $position = NULL, $file_type = NULL, $params = array())
 {
     if (!isset($GLOBALS['files_Module'])) {
         return NULL;
     }
     $params = array_merge(array('sql_select_add' => '', 'sql_order_by' => 'link_order'), $params);
     global $DB;
     load_class('_core/model/dataobjects/_dataobjectlist2.class.php', 'DataObjectList2');
     $LinkCache =& get_LinkCache();
     $LinkList = new DataObjectList2($LinkCache);
     // IN FUNC
     $SQL = new SQL();
     $SQL->SELECT('l.*');
     $SQL->SELECT_add($params['sql_select_add']);
     $SQL->FROM('T_links AS l');
     $SQL->WHERE($this->get_where_condition());
     if (!empty($position)) {
         $position = explode(',', $position);
         $SQL->WHERE_and('link_position IN ( ' . $DB->quote($position) . ' )');
     }
     $SQL->ORDER_BY($params['sql_order_by']);
     $SQL->LIMIT($limit);
     if (!is_null($file_type)) {
         // Restrict the Links by File type
         $SQL->FROM_add('INNER JOIN T_files ON link_file_ID = file_ID');
         $SQL->WHERE_and('file_type = ' . $DB->quote($file_type) . ' OR file_type IS NULL');
     }
     $LinkList->sql = $SQL->get();
     $LinkList->query(false, false, false, 'get_attachment_LinkList');
     if ($LinkList->result_num_rows == 0) {
         // Nothing found
         $LinkList = NULL;
     }
     return $LinkList;
 }
 /**
  * Display the widget!
  *
  * @param array MUST contain at least the basic display params
  */
 function display($params)
 {
     $this->init_display($params);
     $UserCache =& get_UserCache();
     $UserList = new DataObjectList2($UserCache);
     switch ($this->disp_params['order_by']) {
         case 'regdate':
             $sql_order = 'user_created_datetime DESC';
             break;
         case 'moddate':
             $sql_order = 'user_profileupdate_date DESC';
             break;
         case 'random':
         default:
             $sql_order = 'RAND()';
             break;
     }
     // Query list of files:
     $SQL = new SQL();
     $SQL->SELECT('*');
     $SQL->FROM('T_users');
     $SQL->WHERE('user_avatar_file_ID IS NOT NULL');
     $SQL->ORDER_BY($sql_order);
     $SQL->LIMIT($this->disp_params['limit']);
     $UserList->sql = $SQL->get();
     $UserList->query(false, false, false, 'User avatars widget');
     $layout = $this->disp_params['thumb_layout'];
     $nb_cols = $this->disp_params['grid_nb_cols'];
     $count = 0;
     $r = '';
     /**
      * @var User
      */
     while ($User =& $UserList->get_next()) {
         if ($layout == 'grid') {
             if ($count % $nb_cols == 0) {
                 $r .= $this->disp_params['grid_colstart'];
             }
             $r .= $this->disp_params['grid_cellstart'];
         } else {
             $r .= $this->disp_params['item_start'];
         }
         $identity_url = get_user_identity_url($User->ID);
         $avatar_tag = $User->get_avatar_imgtag($this->disp_params['thumb_size']);
         if ($this->disp_params['bubbletip'] == '1') {
             // Bubbletip is enabled
             $bubbletip_param = ' rel="bubbletip_user_' . $User->ID . '"';
             $avatar_tag = str_replace('<img ', '<img ' . $bubbletip_param . ' ', $avatar_tag);
         }
         if (!empty($identity_url)) {
             $r .= '<a href="' . $identity_url . '">' . $avatar_tag . '</a>';
         } else {
             $r .= $avatar_tag;
         }
         ++$count;
         if ($layout == 'grid') {
             $r .= $this->disp_params['grid_cellend'];
             if ($count % $nb_cols == 0) {
                 $r .= $this->disp_params['grid_colend'];
             }
         } else {
             $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();
     if ($layout == 'grid') {
         echo $this->disp_params['grid_start'];
     } else {
         echo $this->disp_params['list_start'];
     }
     echo $r;
     if ($layout == 'grid') {
         if ($count && $count % $nb_cols != 0) {
             echo $this->disp_params['grid_colend'];
         }
         echo $this->disp_params['grid_end'];
     } else {
         echo $this->disp_params['list_end'];
     }
     echo $this->disp_params['block_end'];
     return true;
 }
    /**
     * Display the widget!
     *
     * @param array MUST contain at least the basic display params
     */
    function display($params)
    {
        $this->init_display($params);
        $UserCache =& get_UserCache();
        $UserList = new DataObjectList2($UserCache);
        switch ($this->disp_params['order_by']) {
            case 'regdate':
                $sql_order = 'user_created_datetime DESC';
                break;
            case 'moddate':
                $sql_order = 'user_profileupdate_date DESC';
                break;
            case 'random':
            default:
                $sql_order = 'RAND()';
                break;
        }
        // Query list of users with picture and not closed:
        $SQL = new SQL();
        $SQL->SELECT('*');
        $SQL->FROM('T_users');
        $SQL->WHERE('user_avatar_file_ID IS NOT NULL');
        $SQL->WHERE_and('user_status <> "closed"');
        if (is_logged_in()) {
            // Add filters
            global $current_User, $DB;
            switch ($this->disp_params['gender']) {
                // Filter by gender
                case 'same':
                    $SQL->WHERE_and('user_gender = "' . $current_User->gender . '"');
                    break;
                case 'opposite':
                    $SQL->WHERE_and('user_gender != "' . $current_User->gender . '"');
                    break;
            }
            switch ($this->disp_params['location']) {
                // Filter by location
                case 'city':
                    $SQL->WHERE_and('user_city_ID ' . (empty($current_User->city_ID) ? 'IS NULL' : '= "' . $current_User->city_ID . '"'));
                case 'subregion':
                    $SQL->WHERE_and('user_subrg_ID ' . (empty($current_User->subrg_ID) ? 'IS NULL' : '= "' . $current_User->subrg_ID . '"'));
                case 'region':
                    $SQL->WHERE_and('user_rgn_ID ' . (empty($current_User->rgn_ID) ? 'IS NULL' : '= "' . $current_User->rgn_ID . '"'));
                case 'country':
                    $SQL->WHERE_and('user_ctry_ID ' . (empty($current_User->ctry_ID) ? 'IS NULL' : '= "' . $current_User->ctry_ID . '"'));
                    break;
                case 'closest':
                    if (!empty($current_User->city_ID)) {
                        // Check if users exist with same city
                        $user_exists = $DB->get_var('SELECT user_ID
							 FROM T_users
							WHERE user_city_ID ="' . $current_User->city_ID . '"
							  AND user_ID != "' . $current_User->ID . '"
							LIMIT 1');
                        if (!empty($user_exists)) {
                            $SQL->WHERE_and('user_city_ID = "' . $current_User->city_ID . '"');
                            $SQL->WHERE_and('user_subrg_ID = "' . $current_User->subrg_ID . '"');
                            $SQL->WHERE_and('user_rgn_ID = "' . $current_User->rgn_ID . '"');
                            $SQL->WHERE_and('user_ctry_ID = "' . $current_User->ctry_ID . '"');
                            break;
                        }
                    }
                    if (!empty($current_User->subrg_ID) && empty($user_exists)) {
                        // Check if users exist with same sub-region
                        $user_exists = $DB->get_var('SELECT user_ID
							 FROM T_users
							WHERE user_subrg_ID ="' . $current_User->subrg_ID . '"
							  AND user_ID != "' . $current_User->ID . '"
							LIMIT 1');
                        if (!empty($user_exists)) {
                            $SQL->WHERE_and('user_subrg_ID = "' . $current_User->subrg_ID . '"');
                            $SQL->WHERE_and('user_rgn_ID = "' . $current_User->rgn_ID . '"');
                            $SQL->WHERE_and('user_ctry_ID = "' . $current_User->ctry_ID . '"');
                            break;
                        }
                    }
                    if (!empty($current_User->rgn_ID) && empty($user_exists)) {
                        // Check if users exist with same region
                        $user_exists = $DB->get_var('SELECT user_ID
							 FROM T_users
							WHERE user_rgn_ID ="' . $current_User->rgn_ID . '"
							  AND user_ID != "' . $current_User->ID . '"
							LIMIT 1');
                        if (!empty($user_exists)) {
                            $SQL->WHERE_and('user_rgn_ID = "' . $current_User->rgn_ID . '"');
                            $SQL->WHERE_and('user_ctry_ID = "' . $current_User->ctry_ID . '"');
                            break;
                        }
                    }
                    if (!empty($current_User->ctry_ID) && empty($user_exists)) {
                        // Check if users exist with same country
                        $user_exists = $DB->get_var('SELECT user_ID
							 FROM T_users
							WHERE user_ctry_ID ="' . $current_User->ctry_ID . '"
							  AND user_ID != "' . $current_User->ID . '"
							LIMIT 1');
                        if (!empty($user_exists)) {
                            $SQL->WHERE_and('user_ctry_ID = "' . $current_User->ctry_ID . '"');
                        }
                    }
                    break;
            }
        }
        $SQL->ORDER_BY($sql_order);
        $SQL->LIMIT(intval($this->disp_params['limit']));
        $UserList->sql = $SQL->get();
        $UserList->query(false, false, false, 'User avatars widget');
        $avatar_link_attrs = '';
        if ($this->disp_params['style'] == 'badges') {
            // Remove borders of <td> elements
            $this->disp_params['grid_cellstart'] = str_replace('>', ' style="border:none">', $this->disp_params['grid_cellstart']);
            $avatar_link_attrs = ' class="avatar_rounded"';
        }
        $layout = $this->disp_params['thumb_layout'];
        $nb_cols = intval($this->disp_params['grid_nb_cols']);
        $count = 0;
        $r = '';
        /**
         * @var User
         */
        while ($User =& $UserList->get_next()) {
            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'];
            }
            $identity_url = get_user_identity_url($User->ID);
            $avatar_tag = $User->get_avatar_imgtag($this->disp_params['thumb_size']);
            if ($this->disp_params['bubbletip'] == '1') {
                // Bubbletip is enabled
                $bubbletip_param = ' rel="bubbletip_user_' . $User->ID . '"';
                $avatar_tag = str_replace('<img ', '<img ' . $bubbletip_param . ' ', $avatar_tag);
            }
            if (!empty($identity_url)) {
                $r .= '<a href="' . $identity_url . '"' . $avatar_link_attrs . '>';
                $r .= $avatar_tag;
                if ($this->disp_params['style'] == 'badges') {
                    // Add user login after picture
                    $r .= '<br >' . $User->get_colored_login();
                }
                $r .= '</a>';
            } else {
                $r .= $avatar_tag;
            }
            ++$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') {
            echo $this->disp_params['grid_start'];
        } elseif ($layout == 'flow') {
            // Flow block layout
            echo $this->disp_params['flow_start'];
        } else {
            echo $this->disp_params['list_start'];
        }
        echo $r;
        if ($layout == 'grid') {
            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 {
            echo $this->disp_params['list_end'];
        }
        echo $this->disp_params['block_body_end'];
        echo $this->disp_params['block_end'];
        return true;
    }
    /**
     * 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;
    }