• 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

AndyB

Well-known member
#1
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.
 

AndyB

Well-known member
#3
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
 

AndyB

Well-known member
#5
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:

AndyB

Well-known member
#6
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 ??
 

AndyB

Well-known member
#8
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?
 

AndyB

Well-known member
#9
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
   }   
}

?>
 

AndyB

Well-known member
#10
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()
 

Marcus

Well-known member
#11
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($extension, array_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.
 

AndyB

Well-known member
#12
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
 

AndyB

Well-known member
#14
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:

AndyB

Well-known member
#16
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:

Jeremy

Well-known member
#20
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.