Jump to content

Developer Documentation

core/EditorMedia

What it is

An EditorMedia extension is designed to allow users to share content from your application within any editor in the suite using the "Insert other media" menu at the bottom left. By default, the user's existing attachments are available for them to share, and if Gallery is installed then any images the user has uploaded in Gallery are also available to be easily shared.

How to use

The extension must define two methods

    /**
     * Get Counts
     *
     * @param    \IPS\Member    $member        The member
     * @param    string        $postKey    The post key
     * @param    string|null    $search        The search term (or NULL for all)
     * @return    int
     */
    public function count( $member, $postKey, $search=NULL )
    {
        // Get the count and return it as an int
    }

The count method is passed the member who is posting, the post key, and any search term (the insert other media feature supports searching) that you should filter by. You must return a count of the total number of insertable media files that match the parameters.

    /**
     * Get Files
     *
     * @param    \IPS\Member    $member    The member
     * @param    string|null    $search    The search term (or NULL for all)
     * @param    string        $postKey    The post key
     * @param    int            $page    Page
     * @param    int            $limit    Number to get
     * @return    array        array( 'Title' => array( (IPS\File, \IPS\File, ... ), ... )
     */
    public function get( $member, $search, $postKey, $page, $limit )
    {
        // Get files and return
    }

The get() method accepts an object $member representing the member who is posting, the search string as $search, the post key of the current submission, a $page parameter representing what page of media objects the user is viewing, and a $limit parameter that represents how many files per page are being displayed. The method should then return an array of media objects that the user may insert.

The most common implementation will formulate a database query restricting submissions to the member and the search term supplied (if any), limiting by ( $page -1 ) * $limit, $limit, loop over the results and store them in an array, and then return the array. The following example from Gallery will help illustrate how this is done

    /**
     * Get Files
     *
     * @param    \IPS\Member    $member    The member
     * @param    string|null    $search    The search term (or NULL for all)
     * @param    string        $postKey    The post key
     * @param    int            $page    Page
     * @param    int            $limit    Number to get
     * @return    array        array( 'Title' => array( (IPS\File, \IPS\File, ... ), ... )
     */
    public function get( $member, $search, $postKey, $page, $limit )
    {
        $where = array(
            array( "image_member_id=? AND image_approved=?", $member->member_id, 1 ),
        );
        
        if ( $search )
        {
            $where[] = array( "image_caption LIKE ( CONCAT( '%', ?, '%' ) )", $search );
        }
        
        $return = array();
        foreach ( \IPS\Db::i()->select( '*', 'gallery_images', $where, 'image_date DESC', array( ( $page - 1 ) * $limit, $limit ) ) as $row )
        {
            $image = \IPS\gallery\Image::load( $row['image_id'] );
            $obj = \IPS\File::get( 'gallery_Images', $row['image_masked_file_name'] );
            $obj->contextInfo = $image->caption;
            $return[ (string) $image->url() ] = $obj;
        }
        
        return $return;
    }

A where clause is crafted to return images that the current member has submitted and which are approved, and then restricted based on the search term if applicable. We then craft a database query to fetch images based on this where clause, newest to oldest, and limited as previously described.

The image is loaded, and then the \IPS\File object is fetched for this image. Some additional context information is passed to the file object (in this case, the image caption), and then a return entry is stored with the URL that clicking the image should take you to as the key, and the \IPS\File object as the value. The array is then returned.


  Report Document


×
×
  • Create New...