1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Foreach in template hook

Discussion in 'XenForo Development Discussions' started by Mythotical, Aug 28, 2011.

  1. Mythotical

    Mythotical Well-Known Member

    Is it possible to do a foreach when using a template hook in the 'message' template? Every time I try it gives me an error that the BbCode_TextWrapper is already in use and cannot be used again.

    Go figure, anyway, not sure how else to go about it. I am using a Listener for Post and a Model.

    Here is my listener:
    PHP:
    class xShop_Listener_Post
    {
    /**
    * If we are getting the post model, extend it with our custom class that holds a method
    * for the images.
    *
    * @param string The name of the class to be created
    * @param array A modifiable list of classes that wish to extend the class.
    */
    public static function listen($class, array &$extend)
    {
    if (
    $class == 'XenForo_Model_Post')
    {
    $extend[] = 'xShop_Model_Post';
    }
    }

    }
    Here is my model:
    PHP:
    /**
    * Model for posts.
    *
    * @package xShop
    */
    class xShop_Model_Post extends XFCP_xShop_Model_Post
    {
        
    /**
        * Constants to allow joins to extra tables in certain queries
        *
        * @var integer Join xShop tables
        */
        
    const FETCH_XSHOP 0x60;

        
    /**
        * Gets the specified posts - extended to include thread ban info
        *
        * @param array $postIds
        * @param array $fetchOptions Collection of options that relate to fetching
        *
        * @return array Format: [post id] => info
        */
        
    public function getPostsByIds(array $postIds, array $fetchOptions = array())
        {
            if (!empty(
    $fetchOptions['join']))
            {
                
    $fetchOptions['join'] |= self::FETCH_XSHOP;
            }
            else
            {
                
    $fetchOptions['join'] = self::FETCH_XSHOP;
            }

            return 
    parent::getPostsByIds($postIds$fetchOptions);
        }

        
    /**
        * Extended to process thread ban joins
        * Checks the 'join' key of the incoming array for the presence of the FETCH_x bitfields in this class
        * and returns SQL snippets to join the specified tables if required
        *
        * @param array $fetchOptions containing a 'join' integer key build from this class's FETCH_x bitfields
        *
        * @return array Containing 'selectFields' and 'joinTables' keys. Example: selectFields = ', user.*, foo.title'; joinTables = ' INNER JOIN foo ON (foo.id = other.id) '
        */
        
    public function preparePostJoinOptions(array $fetchOptions)
        {
            
    $joinOptions parent::preparePostJoinOptions($fetchOptions);

            if (!empty(
    $fetchOptions['join']))
            {
                if (
    $fetchOptions['join'] & self::FETCH_XSHOP)
                {
                    
    $selectFields ',
                        stock.member_id AS stockMember,
                        stock.item_id AS stockItemId,
                        stock.stock_id AS stockId,
                        item.item_id AS itemId,
                        item.item_img AS itemImg'
    ;
                    
    $joinTables '
                        LEFT JOIN xshop_stock AS stock ON(stock.member_id = post.user_id)
                        LEFT JOIN xshop_items AS item ON(item.item_id = stock.item_id)'
    ;

                    
    $joinOptions['selectFields'] .= $selectFields;
                    
    $joinOptions['joinTables'] .= $joinTables;
                }
            }

            return 
    $joinOptions;
        }

        
    /**
        * Returns all posts for a specified thread. Fetch options may limit
        * posts returned. Extended to include thread bans
        *
        * @param integer $threadId
        * @param array $fetchOptions Collection of options that relate to fetching
        *
        * @return array
        */
        
    public function getPostsInThread($threadId, array $fetchOptions = array())
        {
            if (!empty(
    $fetchOptions['join']))
            {
                
    $fetchOptions['join'] |= self::FETCH_XSHOP;
            }
            else
            {
                
    $fetchOptions['join'] = self::FETCH_XSHOP;
            }

            return 
    parent::getPostsInThread($threadId$fetchOptions);
        }

    }
     

Share This Page