Jump to content

Module/Center Content


Axari

Recommended Posts

I've read Rocky's guide on making a new module, which was very helpful. However, I have a few questions I'm hoping all of you can help me with;

 

1. How do I make a module display it's own (center/content) page? I see in rocky's hook list there isn't a center/content hook, only the home page hook. Which isn't what I need... Does this mean modules are limited to leftColumn, rightColumn, etc standard placements?

 

2. I got the module to install and display text. But the php just kind of spews all over... How do I properly add PHP into the TPL files so the script runs as intended without smarty getting in the way? (I tried {literal} tags, without any success)

 

3. I thought I'd try making my script a module that can run, but obviously running into challenges. If modules are limited (or not intended to be used the way I'm attempting to use them) then what is the best method for implementing my own center/content page, with my own script(s) running on it? (PHP specifically)

 

 

Appreciate everyone's time!

Link to comment
Share on other sites

1. Forum search:

http://www.prestashop.com/forums/topic/129386-display-newsletter-block-in-a-cms-page/page__p__641992?do=findComment&comment=641992

http://www.prestashop.com/forums/topic/142563-creating-a-front-office-page-associated-with-my-custom-module/

 

 

2. PHP directly in TPL files defeats the whole purpose of TPL files and is not recommended due to safety issues A better explanation, and similar questions can be found here:

http://stackoverflow.com/questions/7273602/prestashop-php-include-in-a-tpl-does-not-work

http://stackoverflow.com/questions/7446851/using-php-code-in-smarty-tpl-file

 

3.

You might be able to manually create you own pages in php, and using the css from your templates to give it a similar layout. Not sure if this is a good / easy way to do it.

I would probably recommend writing all your php codes in the main .php file in your module and then using smart variables to transplant them in your .tpl files.

 

What exactly are you trying to make?

 

Thanks,

Ruud

Link to comment
Share on other sites

Thanks Ruud!

 

So what I've learned so far is...

 

A. I spent hours searching, and came up with nothing that was helpful. So I apparently fail at searching.

B. If I want to use a module to do what I want, I need to use a CMS page. Not exactly what I'd like :(

C. Going about this the way I was (module & PHP in TPL files) is not the best approach.

 

I probably need to do my own controller, and have it return my php output as a smarty variable that gets displayed in my .tpl file. I'm going to see if I can work on that... But haven't found any good docs on creating them. So I'll see what I can break.

 

As far as what I'm doing? I need an input box (like a comment box) which will allow people to paste items into it. 1 item per row. Think of that as someone's wishlist they made. Then for every item that's available they'll get an add to cart button. (There's more to it than that, but that's the high level)

 

 

Thanks for taking the time to get me on the right path MeRuud!

Link to comment
Share on other sites

Interesting concept.

 

I can tell you how to make it as a module for the back office in the administrator panel, so you as an administrator can do it, but this kinda defeats the purpose of your idea (and you probably know how to do this yourself)..

Let me dig through the Prestashop files for a bit and see if I can find something out.

 

I will keep you posted if I can find something.

Thanks,

Ruud

Link to comment
Share on other sites

Okay, need some guidance :)

 

I have a test.php file -

require(dirname(__FILE__).'/config/config.inc.php');
ControllerFactory::getController('TestController')->run();

 

Then I have a controller for it -

class TestControllerCore extends FrontController
{
 public $php_self = 'test.php';

 public function TestStuff()
{
   // Get vars
$data = $_POST["data"];
$option = $_POST["option"];

// If the submit button was clicked
if ($data)
  {
  // Run script, connect the database & echo results
  }

// If no criteria
  else
    {
   echo"
   <form id='import' name='import' method='post' action='test.php'>
   Import <br/>
   Paste your wishlist in the box below<br /><br />
   <textarea name='data' id='data' cols='65' rows='30'></textarea>
   <br /> <br />
   <input name='submit' type='submit' value='Submit'>
   </form>";
   }

   return $TestStuff;
}

public function displayContent()
 {
  parent::displayContent();
	  self::$smarty->display(_PS_THEME_DIR_.'test.tpl');
 }
}

 

Then I have a TPL file -

{$TestStuff}
Display something nice.

 

I get a center page (with the shop template around it) and it says "Display something nice."

 

How do I make it display the output of my custom function (TestStuff) though? I probably need to make a change in the controller and the tpl - but I'm not sure what exactly.

 

 

Any help would be appreciated!

 

P.S.

I should also mention, that the first page displayed is a form which posts to itself. So If I need to know anything to make that work, that would also be helpful!

 

[Edit] Added basic example in Controller

Link to comment
Share on other sites

I think you can see example of how to create separate page for module in loyalty and referralprogram modules.

Files are referralprogram-program.php and loyalty-program.php.

Also you can see my post on how to create custom page here but this not connected to modules.

Just custom page not using CMS.

 

And try not to use php in tpl files and also try not touse HTML in php files.

So for example in php file you have

// If the submit button was clicked
if ($data)
  {
  // Run script, connect the database
 // and place them in array
   $result = getFromDB();
  }
//Then pass  that array to tpl file
 $smarty->assign('result', $result );

 return $this->display(__FILE__, 'test.tpl');

 

And check Prestashop code more because there is good chance that for something you want

to do already exist some similar code.

 

Then in test.tpl file check


{if isset($result)}
{foreach from=$result item=row}
/* display something */
{/foreach}

{else}

<form id='import' name='import' method='post' action='test.php'>
Import <br/>
Paste your wishlist in the box below<br /><br />
<textarea name='data' id='data' cols='65' rows='30'></textarea>
<br /> <br />
<input name='submit' type='submit' value='Submit'>
</form>
{/if}

Link to comment
Share on other sites

Thanks Razaro, that got me headed in the right direction :)

 

I'm kind of stuck at the moment though. I have tried creating an array with my data;

 

$result = array(id=>'$id', name=>'$name', price=>'$price', id_img=>'$id_img', addtocart=>'$addtocart');

 

But your example above with the foreach statement doesn't work.

 

So I tried:

 $smarty->assign('id', '$id');
 $smarty->assign('name', '$name');
 $smarty->assign('price', '$price');
 $smarty->assign('id_img', '$id_img');
 $smarty->assign('addtocart', '$addtocart');

 

Thinking that I could at least display it on the tpl. By using (for example):

 

{$id}

 

in the tpl, but it doesn't display anything.

 

Is there a way to tell smarty to display everything that's sent to it? (like a debug mode)

 

Where am I going wrong?

 

(I checked the smarty docs on their website too, but I'm just not getting this)

Link to comment
Share on other sites

Error is that you use '$id' as a string and not like a variable like it should $id.

 

So here is example.

In php file

 $id = 2; $name = 'John'; $price = 20.50; $id_img = 5; $addtocart = 'yes';
 $results = array('id'=>$id, 'name'=>$name, 'price'=>$price, 'id_img'=>$id_img, 'addtocart'=>$addtocart);
 $smarty->assign('results', $results);

Then in tpl file

<div >
   {foreach from=$results key=k item=result}
 <span id={$k} >{$result}</span>
   {/foreach}
</div>

 

And result would be

<div>
	 <span id="id">2</span>
	 <span id="name">John</span>
	 <span id="price">20.5</span>
	 <span id="id_img">5</span>
	 <span id="addtocart">yes</span>
   </div>

 

Also code to assign one variable is

$smarty->assign('id', $id);

 

To display errors turn debug mode on by changing this line in config/config.inc.php to

@ini_set('display_errors', 'on');

 

If you need you can also turn Smarty debug on by changing in tools\smarty\Smarty.class.php this line to

   // debug mode
   public $debugging = true;

It will show all smarty variables with its values.

Link to comment
Share on other sites

You are awesome Razaro! Many thanks, that helped a lot.

 

I have one (I hope...) last hangup. When I submit the form, the form just re-displays. So I'm missing something somewhere. Can you glance at this and possibly give me a suggestion? (Yes, I'm sure there are more efficient ways to code this. Right now I'm just trying to get it to work again. And I probably need to update the database requests for PS 1.4 so ignore the details..)

 

(I don't get any Smarty errors)

 

 

Controller -


<?php

class EImportControllerCore extends FrontController
{
public $php_self = 'e_import.php';

public function EImport()
{
// Get posted vars
$data = $_POST["data"];

// If the submit button was clicked
if ($data)
{
// Function: Replace breaks/returns with commas
function mynl2br($text) {
 return strtr($text, array("\r\n" => ',', "\r" => ','));
}
// Run the function & re-create the string
$data = mynl2br($data);

// Cleanup Array
foreach ($data as &$item)
{
// Trim whitespace
$item = trim($item);

// Slashes
$item = addslashes($item);
}

// Remove blank array elements to clean it up
function array_remove_empty($arr){
  $narr = array();
  while(list($key, $val) = each($arr)){
   if (is_array($val)){
	   $val = array_remove_empty($val);
	   // does the result array contain anything?
	   if (count($val)!=0){
		   // yes :-)
		   $narr[$key] = $val;
	   }
   }
   else {
	   if (trim($val) != ""){
		   $narr[$key] = $val;
	   }
   }
  }
  unset($arr);
  return $narr;
}
$data = array_remove_empty($data);

// Now we need to lookup each item in the array to get the Product ID
//
// Connect to Database
  $connection = mysql_connect("***", "***", "***")
	or die("Could not connect");
  if ($connection == false)
{
  echo mysql_errno().": ".mysql_error()."<BR>";
  exit;
  }

// Select the database
 mysql_select_db("***") or die("Could not select the shop database");

foreach ($data as $item)
{
// Query 1 - get product id & name
$query = "SELECT l.id_product, l.name FROM ps_product_lang l WHERE name = '$item'";
$id_result = mysql_query($query) or die(mysql_errno().": ".mysql_error()."<BR>");

$id = @mysql_result ($id_result, $i, "id_product");
$name = @mysql_result ($id_result, $i, "l.name");

// Query 2 - Grab the item price
$query = "SELECT p.price FROM ps_product p WHERE p.id_product = '$id' ";
$p_result = mysql_query($query) or die(mysql_errno().": ".mysql_error()."<BR>");

$price = @mysql_result ($p_result, $i, "p.price");
$price = number_format($price, 2, '.', ',');

// Query 3 - Grab the image id (graphics in the shop are "id_product.id_image.jpg" format)
$query = "SELECT i.id_image FROM ps_image i WHERE i.id_product = '$id' ";
$i_result = mysql_query($query) or die(mysql_errno().": ".mysql_error()."<BR>");

$id_img = @mysql_result ($i_result, $i, "i.id_image");

// We don't want to display the add to cart button if the price is $0.00
if ($price == 0.00){
$name = "<font color='red'>$item</font>";
$price = "<font color='red'><center>-</center></font>";
$addtocart = "<font color='red'><center>Not Available</center></font>";
}

if ($price != 0.00){
$price = $price';
$addtocart = "<a class='button ajax_add_to_cart_button exclusive' rel='ajax_id_product_$id' href='cart.php?add&id_product=$id'>Add to cart</a>";
}

// Smarty stuff
$results = array('id'=>$id, 'name'=>$name, 'price'=>$price, 'id_img'=>$id_img, 'addtocart'=>$addtocart);
$smarty->assign('results', $results);
}

}

}
public function displayContent()
{
parent::displayContent();
self::$smarty->display(_PS_THEME_DIR_.'e_import.tpl');
}
}
?>

 

Template -

{*
* E Import Template
*}
{capture name=path}{l s='E Import'}{/capture}
{include file="$tpl_dir./breadcrumb.tpl"}
<div class="pl_category">
<h1>{l s='E Import'}</h1>
<div class="pl_ct">
{if isset($results)}
<h4>{l s='E Import Results'}</h4>
 <br>
<div >
{foreach from=$results key=k item=result}
	 <span id={$k} >{$results}</span>
{/foreach}
</div>
{/if}
<br />
<h4>{l s='Welcome to E Import'}</h4>
 <form id='e_import' name='e_import' method='post' action='e_import.php'>
 <br />
 Paste your wishlist in the box below<br /><br />
 <textarea name='data' id='data' cols='65' rows='30'></textarea>
 <br /> <br />
 <input name='submit' type='submit' value='Submit'>
 </form>
</div>
</div>

Link to comment
Share on other sites

  • 2 weeks later...

I've turned on:

 

in config/config.inc.php to

@ini_set('display_errors', 'on');

 

and in tools\smarty\Smarty.class.php to

// debug mode
public $debugging = true;

 

But I can't see what's being posted.

 

Anyone know how I can see if it's getting the data I'm posting?

 

Do I need to add something to the first php file to pass the form data to the controller?

require(dirname(__FILE__).'/config/config.inc.php');
ControllerFactory::getController('TestController')->run();

Link to comment
Share on other sites

Hi

Check ContactController.php for example, this is how you check if your form is submited

 

 if (Tools::isSubmit('submitMessage'))
 {
				  ....
		    }

 

- and this is is how you get specific value

(int)(Tools::getValue('id_order'))

 

Also you should check few classes and controllers - there are function that are already written that you can use.

 

Also I see 2 error is your code

$price = $price';

extra '

 

and

    {foreach from=$results key=k item=result}
			 <span id={$k} >{$results}</span>
    {/foreach}

it should be

<span id={$k} >{$result}</span>

 

And I did mot write correct file for Smarty debuging.

It is smarty.config.inc.php in config folder and line should be

$smarty->debugging = false;

and if you should see popup.

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...