Jump to content

Can't access image link


SonoguN

Recommended Posts

Hi I am very new to Prestashop and am trying to create a module that will create a PDF of a product. Everything seems to be working fine except I cannot seem to access the image url.. I can get the image ID, but getimagelink() does nor work and keeps giving me the following error: "Notice: Undefined property: Dispatcher::$allow in C:\wamp\www\albert_menes\classes\Link.php on line 381" and also " Notice: Undefined property: Dispatcher::$protocol_content in C:\wamp\www\albert_menes\classes\Link.php on line 387" I am no doubt doing something wrong so if someone could point out how to fix this I would be really grateful. Thanks

<?php

Class testmoduleAllproductsModuleFrontController extends ModuleFrontControllerCore
{
	public function init()
	{
		$this->page_name = 'allproducts'; // page_name and body id
	    parent::init();
	    $this->display_column_left = false;
		$this->display_column_right = false;
	}

	public function initContent()
	{
	    parent::initContent();
	    
	    $products_partial = Product::getProducts($this->context->language->id, 0, 0, 'name', 'asc');
	    $products = Product::getProductsProperties($this->context->language->id, $products_partial);
	 
	    $this->context->smarty->assign(array(
	        'products' => $products,
	        'homeSize' => Image::getSize('home_default')
	    ));
	    $this->setTemplate('allproducts.tpl');

	    
  


	} 


    public function productAccess()
    {

        $products_partial = Product::getProducts($this->context->language->id, 0, 0, 'name', 'asc');
        


        foreach ($products_partial as &$product) {

            $productID = $product['id_product'];
            $thisProductid = $_GET['prodid'];

            if($productID == $thisProductid){

                $productName = $product['name'];
                $productDescription = $product['description_short'];
                $productPdfname = $product['link_rewrite'];
                $productCategoryid = $product['id_category_default'];
                $productWeight = $product['weight'];

                print_r($productPdfname);

                $thisImage = Image::getCover($thisProductid);
                $thisImageID = $thisImage['id_image'];
               // $thisImagelink = Link::getImageLink($productPdfname, $thisImageID, 'large_default');

               
                
                
                echo "<pre>";
                print_r($thisImage);
                print_r($product);
                echo "</pre>";
            }

            
        }


    }
 
}




require('fpdf.php');

class PDF extends FPDF
{
var $B;
var $I;
var $U;
var $HREF;

function PDF($orientation='P', $unit='mm', $size='A4')
{
    // Call parent constructor
    $this->FPDF($orientation,$unit,$size);
    // Initialization
    $this->B = 0;
    $this->I = 0;
    $this->U = 0;
    $this->HREF = '';
}

function WriteHTML($html)
{
    // HTML parser
    $html = str_replace("\n",' ',$html);
    $a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
    foreach($a as $i=>$e)
    {
        if($i%2==0)
        {
            // Text
            if($this->HREF)
                $this->PutLink($this->HREF,$e);
            else
                $this->Write(5,$e);
        }
        else
        {
            // Tag
            if($e[0]=='/')
                $this->CloseTag(strtoupper(substr($e,1)));
            else
            {
                // Extract attributes
                $a2 = explode(' ',$e);
                $tag = strtoupper(array_shift($a2));
                $attr = array();
                foreach($a2 as $v)
                {
                    if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
                        $attr[strtoupper($a3[1])] = $a3[2];
                }
                $this->OpenTag($tag,$attr);
            }
        }
    }
}

function OpenTag($tag, $attr)
{
    // Opening tag
    if($tag=='B' || $tag=='I' || $tag=='U')
        $this->SetStyle($tag,true);
    if($tag=='A')
        $this->HREF = $attr['HREF'];
    if($tag=='BR')
        $this->Ln(5);
}

function CloseTag($tag)
{
    // Closing tag
    if($tag=='B' || $tag=='I' || $tag=='U')
        $this->SetStyle($tag,false);
    if($tag=='A')
        $this->HREF = '';
}

function SetStyle($tag, $enable)
{
    // Modify style and select corresponding font
    $this->$tag += ($enable ? 1 : -1);
    $style = '';
    foreach(array('B', 'I', 'U') as $s)
    {
        if($this->$s>0)
            $style .= $s;
    }
    $this->SetFont('',$style);
}

function PutLink($URL, $txt)
{
    // Put a hyperlink
    $this->SetTextColor(0,0,255);
    $this->SetStyle('U',true);
    $this->Write(5,$txt,$URL);
    $this->SetStyle('U',false);
    $this->SetTextColor(0);
}
}

$html = 'You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
<u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
text, such as <a href="http://www.fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.';


$varMod = new testmoduleAllproductsModuleFrontController;

$varMod->productAccess();

//Link::getImageLink('test-product', 27, 'home_default');


/*$pdf = new PDF();
$link = $pdf->AddLink();


$pdf->AddPage();
$pdf->SetLink($link);
$pdf->Image('http://localhost/albert_menes/modules/testmodule/views/templates/front/logo.png',10,12,30,0,'','http://www.fpdf.org');
$pdf->SetLeftMargin(45);
$pdf->SetFont('Arial','',14);
$pdf->Write(5,"To view this product online, ");
$pdf->Write(5,'click here.',$link);
$pdf->SetFontSize(12);
//$pdf->WriteHTML($html);
$pdf->Output();
*/


?>



			
				


	Edited  by SonoguN
	
	
		(see edit history)
		
	

			
		
Link to comment
Share on other sites

Thanks for the feedback.. hmm I thought that I was pulling all this in correctly. Still couldn't get it to work.. Got it to work in the .tpl file but it just breaks if I try access it in the .php controller. Need to get the url to pass to the pdf class.

 

I have managed to fix this using a work around.. passed the link from the product page via a url parameter, but would prefer to access it in the controller.

 

cheers much appreciated

Link to comment
Share on other sites

Thanks for the feedback.. hmm I thought that I was pulling all this in correctly. Still couldn't get it to work.. Got it to work in the .tpl file but it just breaks if I try access it in the .php controller. Need to get the url to pass to the pdf class.

 

I have managed to fix this using a work around.. passed the link from the product page via a url parameter, but would prefer to access it in the controller.

 

cheers much appreciated

 

the available function inside Link class is not a static function

So you should call the function this way $link->availableClassFunction() instead Link::availableClassFunction()

 

And remember, you should load the link object before use, except the link object is already there somehow in your file.

Maybe it's already there in context object of your file, so you can call $this->context->link->availableClassFunction();

 

Link object can be load like this in your controller file

$link = new Link(); 

OR

$link = $this->context->link;

 

You can do a debugging to make sure link object is already there or not :

// debugging
if(Validate::isLoadedObject($link)) {
   echo '$link object available';
   d($link);
}
elseif(Validate::isLoadedObject($this->context->link)) {
   echo 'context link available';
   d($this->context->link);
}
else
   d('link object not available yet');

If link object not available yet, and you need full URL as result, you should add protocol/host variable when loading link object

$protocol_link = (Configuration::get('PS_SSL_ENABLED') || Tools::usingSecureMode()) ? 'https://' : 'http://';
$useSSL = ((isset($this->ssl) && $this->ssl && Configuration::get('PS_SSL_ENABLED')) || Tools::usingSecureMode()) ? true : false;
$protocol_content = ($useSSL) ? 'https://' : 'http://';
$link = new Link($protocol_link, $protocol_content);
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...