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

How to extend - class XenForo_ViewPublic_Attachment_View

Discussion in 'XenForo Development Discussions' started by AndyB, Sep 22, 2013.

  1. AndyB

    AndyB Well-Known Member

    I'm creating a new add-on and I can't get Listener.php to echo a test string. I expect the word "test" to be echo'ed when I view a thread with attachments.

    The file I'm trying to extend is:

    /library/Xenforo/ViewPublic/Attachment/View.php

    When I create a new Code Event Listener, should I listen to load_class or load_class_view ??

    Thank you for your help.
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    load_class_view
     
  3. AndyB

    AndyB Well-Known Member

    Hi Chris,

    I've tried listening to load_class_view, but not able to verify if the listener is working.

    My folders and file structure

    library
    --Andy
    ----OpenPDF
    ------Lisener.php

    Contents of Listener.php

    PHP:
    <?php

    class Andy_OpenPDF_Listener
    {
       public static function 
    View($class, array &$extend)
       {
         echo 
    'test';
         
    $extend[] = 'Andy_OpenPDF_ViewPublic_Attachment_View';
       }
    }

    ?>
    Code Event Listener

    pic001.jpg
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    Your event hint has a typo.

    Should be XenForo rather than Xenforo
     
  5. AndyB

    AndyB Well-Known Member

    Thank you, Chris. That was the problem.

    I added the "die;" code to verify Listener.php file is working. With the add-on active attachments are broken.

    PHP:
    <?php

    class Andy_OpenPDF_Listener
    {
       public static function 
    View($class, array &$extend)
       {
         die;
         
    $extend[] = 'Andy_OpenPDF_ViewPublic_Attachment_View';
       }
    }

    ?>
     
    Last edited: Sep 23, 2013
  6. AndyB

    AndyB Well-Known Member

    I added the following folders and file:

    library
    --Andy
    ----OpenPDF
    ------Lisener.php
    ------ViewPublic
    --------Attachment
    ----------View.php

    The contents of View.php

    PHP:
    <?php

    class Andy_OpenPDF_ViewPublic_Attachment_View extends XFCP_Andy_OpenPDF_ViewPublic_Attachment_View
    {
       public function 
    actionOpenPDF()
       {
         
    $imageTypes = array(
         
    'gif' => 'image/gif',
         
    'jpg' => 'image/jpeg',
         
    'jpeg' => 'image/jpeg',
         
    'jpe' => 'image/jpeg',
         
    'pdf' => 'application/pdf',
         
    'png' => 'image/png'
         
    );
       }  
    }

    ?>
    Notice I added the following to the $imageTypes array:

    'pdf' => 'application/pdf',

    The above code does not work. What is the correct way to modify the $imageTypes array ??
     
  7. Marcus

    Marcus Well-Known Member

    You have to work with parent::

    :)
     
  8. AndyB

    AndyB Well-Known Member

    Thank you for your reply, Marcus. Unfortunately I don't know what working with "parent::" means.

    Also what is the best way to verify in View.php is working? Is the code line

    public function actionOpenPDF()

    correct?
     
  9. AndyB

    AndyB Well-Known Member

    I have verified that View.php is being called using the "die;" code as shown here:

    PHP:
    <?php
    die;
    class 
    Andy_OpenPDF_ViewPublic_Attachment_View extends XFCP_Andy_OpenPDF_ViewPublic_Attachment_View
    {
       public function 
    actionOpenPDF()
       {
         
    // update $imageTypes array
       
    }   
    }

    ?>
     
  10. AndyB

    AndyB Well-Known Member

    However the "die;" doesn't do anything if I put it here:

    PHP:
    <?php

    class Andy_OpenPDF_ViewPublic_Attachment_View extends XFCP_Andy_OpenPDF_ViewPublic_Attachment_View
    {
       public function 
    actionOpenPDF()
       {
         die;
       }   
    }

    ?>
    This leads me to think the following code is incorrect:

    public function actionOpenPDF()
     
  11. Marcus

    Marcus Well-Known Member

    You extend this:
    PHP:
    class XenForo_ViewPublic_Attachment_View extends XenForo_ViewPublic_Base
    {
       public function 
    renderRaw()
       {
         
    $attachment $this->_params['attachment'];

         
    $extension XenForo_Helper_File::getFileExtension($attachment['filename']);
         
    $imageTypes = array(
           
    'gif' => 'image/gif',
           
    'jpg' => 'image/jpeg',
           
    'jpeg' => 'image/jpeg',
           
    'jpe' => 'image/jpeg',
           
    'png' => 'image/png'
         
    );

         if (
    in_array($extensionarray_keys($imageTypes)))
         {
           
    $this->_response->setHeader('Content-type'$imageTypes[$extension], true);
           
    $this->setDownloadFileName($attachment['filename'], true);
         }
         else
         {
           
    $this->_response->setHeader('Content-type''application/octet-stream'true);
           
    $this->setDownloadFileName($attachment['filename']);
         }

         
    $this->_response->setHeader('ETag'$attachment['attach_date'], true);
         
    $this->_response->setHeader('Content-Length'$attachment['file_size'], true);
         
    $this->_response->setHeader('X-Content-Type-Options''nosniff');

         return new 
    XenForo_FileOutput($this->_params['attachmentFile']);
       }
    }
    you can only hook in from the beginning or at the end of a function. I would output the content of "new XenForo_FileOutput($this->_params['attachmentFile']);" and then try to modify it if it is a pdf file.
     
  12. AndyB

    AndyB Well-Known Member

    Hi Marcus,

    Thank you so much for the code. I updated my View.php file with the code you show in post #11. Normal attached images still show fine, but when I click a PDF attachment, I get the following error:

    Fatal error: Cannot redeclare class XenForo_ViewPublic_Attachment_View in /home/*/www/forums/library/Andy/OpenPDF/ViewPublic/Attachment/View.php on line 35
     
  13. Jeremy

    Jeremy XenForo Moderator Staff Member

    He posted the code that you'll be extending, not the code to copy.
     
  14. AndyB

    AndyB Well-Known Member

    In my View.php file, I can't get past the public function actionOpenPDF()

    PHP:
    <?php

    class Andy_OpenPDF_ViewPublic_Attachment_View extends XFCP_Andy_OpenPDF_ViewPublic_Attachment_View
    {
       public function 
    actionOpenPDF()
       {
         
    // can't seem to get to this point
         
    setcookie("test""test"time()+3600*24*365*1"/");
       } 
    }

    ?>
     
    Last edited: Sep 22, 2013
  15. Jeremy

    Jeremy XenForo Moderator Staff Member

    The same as XenForo:
    PHP:
    return new XenForo_FileOutput($this->_params['attachmentFile']);
     
  16. AndyB

    AndyB Well-Known Member

    I'm sorry, Jeremy. I was editing my post #14 while you were replying to the original question.

    What I don't understand is why doesn't the code run past public function actionOpenPDF() ?? A cookie should be set if the code runs that function.
     
    Last edited: Sep 22, 2013
  17. AndyB

    AndyB Well-Known Member

  18. Jeremy

    Jeremy XenForo Moderator Staff Member

    is actionOpenPDF a function in the original view class? Or are you completely adding this function?
     
  19. AndyB

    AndyB Well-Known Member

    I added that. What should I be using?
     
  20. Jeremy

    Jeremy XenForo Moderator Staff Member

    If you are adding a function, you'll need to modify what happens when you click on a pdf to go to your function. You'll need to reroute within renderRaw.
     

Share This Page