How to access prestashop functions in wordpress - module

I am using prestashop 1.4.3 version. And I want to get user data in wordpress. I have tried below code with no luck. Giving me this Fatal error:
Class 'FrontController' not found in
/opt/lampp/htdocs/domin.com/prestashop/init.php on line 28.
Here is my script:
$psabspath = '';
$psabspath = $_SERVER['DOCUMENT_ROOT'] . '/domin.com/prestashop/';
if ( $psabspath != '' && file_exists( $psabspath . 'config/config.inc.php' )
&& ( file_exists( $psabspath . 'classes/FrontController.php' ) ||
file_exists( $psabspath . 'classes/controller/FrontController.php' ) ) ) {
require_once( $psabspath . 'init.php' );
require_once( $psabspath . 'config/config.inc.php' );
class PrestaShopIntegration_FrontController extends FrontController {
public function __destruct()
{
if (Context::getContext()->cookie)
Context::getContext()->cookie->write();
}
public function init() {
$this->page_name = 'wordpress';
parent::init();
if ( isset( $this->context) ) {
if ( version_compare(_PS_VERSION_, '1.5.4', '>=') )
$this->setMedia();
if ( $this->checkAccess() ) {
$this->postProcess();
$this->initHeader();
if ( $this->viewAccess() )
$this->initContent();
else
$this->errors[] = Tools::displayError('Access denied.');
$this->initFooter();
}
}
}
}
}

You should first include config.php file and then you will be able to include init.php.
$psabspath = '';
$psabspath = $_SERVER['DOCUMENT_ROOT'] . '/myskinrecipes.com/shop/';
if ( $psabspath != '' && file_exists( $psabspath . 'config/config.inc.php' )
&& ( file_exists( $psabspath . 'classes/FrontController.php' ) ||
file_exists( $psabspath . 'classes/controller/FrontController.php' ) ) ) {
require_once( $psabspath . 'config/config.inc.php' );
require_once( $psabspath . 'init.php' );
class PrestaShopIntegration_FrontController extends FrontController {
public function __destruct()
{
if (Context::getContext()->cookie)
Context::getContext()->cookie->write();
}
public function init() {
$this->page_name = 'wordpress';
parent::init();
if ( isset( $this->context) ) {
if ( version_compare(_PS_VERSION_, '1.5.4', '>=') )
$this->setMedia();
if ( $this->checkAccess() ) {
$this->postProcess();
$this->initHeader();
if ( $this->viewAccess() )
$this->initContent();
else
$this->errors[] = Tools::displayError('Access denied.');
$this->initFooter();
}
}
}
}
}
config.php will load Prestashop configuration files and load the entire Framework.
init.php is only there to load the controller.

Related

Mixed content on Prestashop with smartblog-v3.0.0 module

I installed a SSL cerfificate to a website created with Prestashop 1.7. But it indicates to me that it is not totally safe since there is a request to the image of the smartblog v 3.0.0 by HTTP. Checking the code from Firefox developer tools. Previously, I tried to force the module to read photos only via https. Code editing did not produce results. From the information collected, it appears that the blog module has a problem loading photos via https. Therefore, duplicate content.
Link to the website
Screenshot dev tools
Part of the smartblog.php file:
public static function GetSmartBlogUrl()
{
$ssl_enable = Configuration::get('PS_SSL_ENABLED');
$id_lang = (int) Context::getContext()->language->id;
$id_shop = (int) Context::getContext()->shop->id;
$rewrite_set = (int) Configuration::get('PS_REWRITING_SETTINGS');
$ssl = null;
static $force_ssl = null;
if ($ssl === null) {
if ($force_ssl === null)
$force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE'));
$ssl = $force_ssl;
}
if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && $id_shop !== null)
$shop = new Shop($id_shop);
else
$shop = Context::getContext()->shop;
$base = ($ssl == 1 && $ssl_enable == 1) ? 'https://' . $shop->domain_ssl : 'http://' . $shop->domain;
$langUrl = Language::getIsoById($id_lang) . '/';
if ((!$rewrite_set && in_array($id_shop, array((int) Context::getContext()->shop->id, null))) || !Language::isMultiLanguageActivated($id_shop) || !(int) Configuration::get('PS_REWRITING_SETTINGS', null, null, $id_shop))
$langUrl = '';
return $base . $shop->getBaseURI() . $langUrl;
}
public static function GetSmartBlogLink($rewrite = 'smartblog', $params = null, $id_shop = null, $id_lang = null)
{
$url = smartblog::GetSmartBlogUrl();
$dispatcher = Dispatcher::getInstance();
$id_lang = (int) Context::getContext()->language->id;
$force_routes = (bool) Configuration::get('PS_REWRITING_SETTINGS');
if ($params != null) {
return $url . $dispatcher->createUrl($rewrite, $id_lang, $params, $force_routes);
} else {
$params = array();
return $url . $dispatcher->createUrl($rewrite, $id_lang, $params, $force_routes);
}
}
which template of smartblog that is not displaying correctly the images via https? I found a issue in that module a few weeks ago, just check the variable who is printing the route.
I had the same problem loading the post images with ssl actived. Just replacing the code in the line http:// for https:// . clear the cache and works.

PDO: My DELETE statement in the User->logout() is not executing

my errors:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '* FROM tbl_usersession WHERE BenutzerID = ?' at line 1 in C:\xampp7-4-3\htdocs\Corona\classes\db.php:52 Stack trace: #0 C:\xampp7-4-3\htdocs\Corona\classes\db.php(52): PDO->prepare('DELETE * FROM t...') #1 C:\xampp7-4-3\htdocs\Corona\classes\db.php(94): DB->query('DELETE * FROM t...', Array) #2 C:\xampp7-4-3\htdocs\Corona\classes\db.php(111): DB->action('DELETE *', 'tbl_usersession', Array) #3 C:\xampp7-4-3\htdocs\Corona\classes\user.php(135): DB->delete('tbl_usersession', Array) #4 C:\xampp7-4-3\htdocs\Corona\logout.php(5): User->logout() #5 {main} thrown in C:\xampp7-4-3\htdocs\Corona\classes\db.php on line 52
the code:
<?php
require_once 'core/init.php';
$user = new User();
$user->logout();
// Redirect::to('index.php');
?>
my user class:
<?php
class User
{
private $_db,
$_data,
$_sessionName,
$_cookieName,
$_isLoggedIn;
public function __construct($user = null)
{
$this->_db = DB::getInstance();
$this->_sessionName = Config::get('session/session_name');
$this->_cookieName = Config::get('remember/cookie_name');
if(!$user)
{
if(Session::exists($this->_sessionName))
{
$user = Session::get($this->_sessionName);
if($this->find($user))
{
$this->_isLoggedIn = true;
}
else
{
//process logout
}
}
}
else
{
$this->find($user);
}
}
public function create($fields = array())
{
if
(
$this->_db->insert('tbl_benutzer', $fields)
)
{
throw new Exception('Es gab einen Fehler bei der Erstellung Ihres Kontos.');
}
echo "Ihr Benutzerkonto wurde erfolgreich angelegt. Sie können sich jetzt anmelden.";
}
public function find($email = null)
{
if($email)
{
$field = (is_numeric($email)) ? 'id' : 'Email';
$data = $this->_db->get('tbl_benutzer', array($field, '=', $email));
if($data->count())
{
$this->_data = $data->first();
return true;
}
return false;
}
}
public function login($email = null, $password = null, $remember = false)
{
// echo "Remember=" . $remember . "<br>";
$user = $this->find($email);
if(!$email && !$password && $this->exists())
{
Session::put($this->_sessionName, $this->data()->ID);
}
else
{
$user = $this->find($email);
if($user)
{
if(password_verify($password, $this->data()->Hash))
{
Session::put($this->_sessionName, $this->data()->ID);
echo "Remember=" . $remember . "<br>";
if($remember)
{
$hash = Hash::unique();
echo "Hash=" . $hash . "<br>";
echo "id=" . $this->data()->ID . "<br>";
$hashCheck = $this->_db->get('tbl_usersession', array('BenutzerID', "=", $this->data()->ID));
echo "HashCheckCount= " . $hashCheck->count() . "<br>";
if(!$hashCheck->count())
{
$this->_db->insert
(
'tbl_usersession',
array
(
'BenutzerID' => $this->data()->ID,
'Hash' => $hash
)
);
}
else
{
$hash = $hashCheck->first()->Hash;
}
}
Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
return true;
}
else return false;
}
}
return false;
}
public function exists()
{
return (!empty($this->data)) ? true : false;
}
public function logout()
{
$this->_db->delete('tbl_usersession', array('BenutzerID', '=', $this->data()->ID));
print_r($this->data());
// Wieso geht das delete nicht?
Session::delete($this->_sessionName);
Cookie::delete($this->_cookieName);
}
public function data()
{
return $this->_data;
}
public function isLoggedIn()
{
return $this->_isLoggedIn;
}
}
?>
my db class:
<?php
class DB
{
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0;
private function __construct()
{
try
{
$this->_pdo = new PDO
(
'mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'),
Config::get('mysql/username'),
Config::get('mysql/password')
);
// Error tracking:
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch
(
PDOException $e
)
{
die($e->getMessage());
}
}
public static function getInstance()
{
if
(
!isset(self::$_instance)
)
{
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array())
{
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql))
{
$x = 1;
if(count($params))
{
foreach($params as $param)
{
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute())
{
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}
else
{
$this->_error = true;
}
}
return $this;
}
public function action($action, $table, $where = array())
{
if(count($where) === 3)
{
$operators = array('=', '<', '>', '<=', '>=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators))
{
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if($this->query($sql, array($value)))
{
return $this;
}
}
}
return false;
}
public function get($table, $where)
{
return $this->action('SELECT *', $table, $where);
}
public function delete($table, $where)
{
return $this->action('DELETE *', $table, $where);
}
public function insert($table, $fields = array())
{
if
(
count($fields)
)
{
$keys = array_keys($fields);
$values = null;
$x = 1;
foreach($fields as $field)
{
$values .= '?';
if
(
$x < count($fields)
)
{
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO " . $table . " (" . implode(", ", $keys) . ") VALUES ({$values})";
if
(
$this->query($sql, $fields)->error()
)
{
return true;
}
}
}
public function update($table, $id, $fields = array())
{
$set = ' ';
$x = 1;
foreach
(
$fields as $name => $value
)
{
$set .= "{$name} = ?";
if
(
$x < count($fields)
)
{
$set .= ', ';
}
$x++;
}
$sql = "UPDATE {$table} SET {$set} WHERE ID = {$id}";
if
(
$this->query($sql, $fields)->error()
)
{
return true;
}
return false;
}
public function results()
{
return $this->_results;
}
public function first()
{
return $this->results()[0];
}
public function error()
{
return $this->_error;
}
public function count()
{
return $this->_count;
}
}
?>
It just basic syntax error for DELETE Statement shown as below:
The correct syntax is
DELETE FROM table
But your wrong syntax is
DELETE * FROM table
So debug your delete function will be solved your syntax error
public function delete($table, $where)
{
return $this->action('DELETE', $table, $where);
}

Prestashop ajax error when decommissioning or quantity increase

my big problem is when I choose a version of product or that I update the quantity I have an error ajax "Looking at the console in the console the url of the query ajax the parameters token, id_product and id_customization are duplicated like this:
https://xeof.com/index.php?controller=product?token=64c9f388aad0883a78f350addff907a8&id_product=81&id_customization=0&group%5B19%5D=291&qty=2&token=64c9f388aad0883a78f350addff907a8&id_product=119&id_customization=0&token=64c9f388aad0883a78f350addff907a8&id_product=74&id_customization=0
but that's not all in the log php error I have this error: Undefined variable: find_best in G: \ Dropbox \ Sites \ xeof.com \ classes \ Product.php on line 6103
In the class the call method is this:
/**
* Get an id_product_attribute by an id_product and one or more
* id_attribute.
*
* e.g: id_product 8 with id_attribute 4 (size medium) and
* id_attribute 5 (color blue) returns id_product_attribute 9 which
* is the dress size medium and color blue.
*
* #param int $idProduct
* #param int|int[] $idAttributes
* #param bool $findBest
* #return int
* #throws PrestaShopException
*/
public static function getIdProductAttributeByIdAttributes($idProduct, $idAttributes, $findBest = false)
{
$idProduct = (int) $idProduct;
if (!is_array($idAttributes) && is_numeric($idAttributes)) {
$idAttributes = array((int) $idAttributes);
}
if (!is_array($idAttributes) || empty($idAttributes)) {
throw new PrestaShopException(sprintf('Invalid parameter $idAttributes with value: "%s"', print_r($idAttributes, true)));
}
$idAttributesImploded = implode(',', array_map('intval', $idAttributes));
$idProductAttribute = Db::getInstance()->getValue('
SELECT
pac.`id_product_attribute`
FROM
`' . _DB_PREFIX_ . 'product_attribute_combination` pac
INNER JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON pa.id_product_attribute = pac.id_product_attribute
WHERE
pa.id_product = ' . $idProduct . '
AND pac.id_attribute IN (' . $idAttributesImploded . ')
GROUP BY
pac.`id_product_attribute`
HAVING
COUNT(pa.id_product) = ' . count($idAttributes)
);
if ($idProductAttribute === false && $findBest) {
//find the best possible combination
//first we order $idAttributes by the group position
$orderred = array();
$result = Db::getInstance()->executeS('
SELECT
a.`id_attribute`
FROM
`'._DB_PREFIX_.'attribute` a
INNER JOIN `'._DB_PREFIX_.'attribute_group` g ON a.`id_attribute_group` = g.`id_attribute_group`
WHERE
a.`id_attribute` IN (' . $idAttributesImploded . ')
ORDER BY
g.`position` ASC'
);
foreach ($result as $row) {
$orderred[] = $row['id_attribute'];
}
while ($idProductAttribute === false && count($orderred) > 0) {
array_pop($orderred);
$idProductAttribute = Db::getInstance()->getValue('
SELECT
pac.`id_product_attribute`
FROM
`'._DB_PREFIX_.'product_attribute_combination` pac
INNER JOIN `'._DB_PREFIX_.'product_attribute` pa ON pa.id_product_attribute = pac.id_product_attribute
WHERE
pa.id_product = '.(int)$idProduct.'
AND pac.id_attribute IN ('.implode(',', array_map('intval', $orderred)).')
GROUP BY
pac.id_product_attribute
HAVING
COUNT(pa.id_product) = '.count($orderred)
);
}
}
if (empty($idProductAttribute)) {
throw new PrestaShopObjectNotFoundException('Can not retrieve the id_product_attribute');
}
return $idProductAttribute;
}
I replace the variable like this on line 6103:
Before:
if ($ idProductAttribute === false && $ find_best)
After:
if ($ idProductAttribute === false && $ findBest)
But no luck I have an exception:
Can not retrieve the id_product_attribute at line 6143 in file classes / Product.php
which I do not find the solution!
I noticed that when I change theme for the classic without having to change php code these settings are not duplicate and I am no other php error.
change this:
if ($ idProductAttribute === false && $ findBest)
to:
$findBest = true;
if ($ idProductAttribute === false && $ findBest)
On Prestashop 1.7.4 I have added this override/classes/Products.php file in order to solve the error during AJAX calls
<?php
class Product extends ProductCore {
public static function getIdProductAttributeByIdAttributes($idProduct, $idAttributes, $findBest = true)
{
return parent::getIdProductAttributeByIdAttributes($idProduct, $idAttributes, $findBest);
}
/**
* #deprecated 1.7.3.1
* #see Product::getIdProductAttributeByIdAttributes()
*/
public static function getIdProductAttributesByIdAttributes($id_product, $id_attributes, $find_best = true)
{
return self::getIdProductAttributeByIdAttributes($id_product, $id_attributes, $find_best);
}
}
Now combinations works properly

Filtering from multiple object ID

I'm trying to filter an eZPublish fetch.
I've got a class which has an object relationS attribute. My filter allows to fetch objects of this class filtered by the object relations attribute.
I've started using the Enhanced Object Relations Filtering extension (http://projects.ez.no/index.php/enhanced_object_relation_filter), but this one only works with "AND" conditions and I want "OR".
I was able to edit the file to add the "OR" logic and this is what I got:
<?php
class EORExtendedFilter
{
function CreateSqlParts( $params )
{
$db =& eZDB::instance();
$tables = array();
$joins = array();
// foreach filtered attribute, we add a join the relation table and filter
// on the attribute ID + object ID
foreach( $params as $param )
{
if ( !is_array( $param ) )
continue;
if ( !is_numeric( $param[0] ) )
{
$classAttributeId = eZContentObjectTreeNode::classAttributeIDByIdentifier( $param[0] );
}
else
{
$classAttributeId = $param[0];
}
// multiple objects ids
if ( is_array($param[1]) )
{
foreach( $param[1] as $objectId )
{
$join = array(); // 'OR' logic
if ( is_numeric( $objectId ) )
{
$tableName = 'eor_link_' . $objectId;
$tables[] = 'ezcontentobject_link ' . $tableName;
$join[] = $tableName . '.from_contentobject_id = ezcontentobject.id';
$join[] = $tableName . '.from_contentobject_version = ezcontentobject.current_version';
$join[] = $tableName . '.contentclassattribute_id = ' . $classAttributeId;
$join[] = $tableName . '.to_contentobject_id = ' . $objectId;
}
// 'OR' logic
$joins[] = $join;
}
}
// single object id
else
{
$objectId = $param[1];
$tableName = 'eor_link_' . $objectId;
$tables[] = 'ezcontentobject_link ' . $tableName;
$joins[] = $tableName . '.from_contentobject_id = ezcontentobject.id';
$joins[] = $tableName . '.from_contentobject_version = ezcontentobject.current_version';
$joins[] = $tableName . '.contentclassattribute_id = ' . $classAttributeId;
$joins[] = $tableName . '.to_contentobject_id = ' . $objectId;
}
}
if ( !count( $tables ) or !count( $joins ) )
{
$tables = $joins = '';
}
else
{
$tables = "\n, " . implode( "\n, ", $tables );
// 'OR' logic
if ( is_array($param[1]) )
{
$andClauses = array();
foreach ($joins as $attr)
{
$andClauses[] = implode( " AND\n ", $attr );
}
$joins = implode( " OR\n ", $andClauses ) . " AND\n ";
}
else
{
$joins = implode( " AND\n ", $joins ) . " AND\n ";
}
}
return array( 'tables' => $tables, 'joins' => $joins );
}
}
This extended attribute filter produces this string :
eor_link_126.from_contentobject_id = ezcontentobject.id AND eor_link_126.from_contentobject_version = ezcontentobject.current_version AND eor_link_126.contentclassattribute_id = 537 AND eor_link_126.to_contentobject_id = 126 OR (eor_link_127.from_contentobject_id = ezcontentobject.id AND eor_link_127.from_contentobject_version = ezcontentobject.current_version AND eor_link_127.contentclassattribute_id = 537 AND eor_link_127.to_contentobject_id = 127 AND
I know it misses some parenthesis so I edited the string to test it directly in phpMyAdmin. This is the query:
SELECT DISTINCT
ezcontentobject.*,
ezcontentobject_tree.*,
ezcontentclass.serialized_name_list as class_serialized_name_list,
ezcontentclass.identifier as class_identifier,
ezcontentclass.is_container as is_container
, ezcontentobject_name.name as name, ezcontentobject_name.real_translation
, a0.sort_key_int
FROM
ezcontentobject_tree,
ezcontentobject,ezcontentclass
, ezcontentobject_name
, ezcontentobject_attribute a0
, ezcontentobject_link eor_link_126
, ezcontentobject_link eor_link_127
WHERE
ezcontentobject_tree.parent_node_id = 1443 and
((eor_link_126.from_contentobject_id = ezcontentobject.id AND
eor_link_126.from_contentobject_version = ezcontentobject.current_version AND
eor_link_126.contentclassattribute_id = 537 AND
eor_link_126.to_contentobject_id = 126) OR
(eor_link_127.from_contentobject_id = ezcontentobject.id AND
eor_link_127.from_contentobject_version = ezcontentobject.current_version AND
eor_link_127.contentclassattribute_id = 537 AND
eor_link_127.to_contentobject_id = 127)) AND
a0.contentobject_id = ezcontentobject.id AND
a0.contentclassattribute_id = 191 AND
a0.version = ezcontentobject_name.content_version AND
( a0.language_id & ezcontentobject.language_mask > 0 AND
( ( ezcontentobject.language_mask - ( ezcontentobject.language_mask & a0.language_id ) ) & 1 )
+ ( ( ( ezcontentobject.language_mask - ( ezcontentobject.language_mask & a0.language_id ) ) & 2 ) )
<
( a0.language_id & 1 )
+ ( ( a0.language_id & 2 ) )
)
AND
ezcontentclass.version=0 AND
ezcontentobject_tree.contentobject_id = ezcontentobject.id AND
ezcontentclass.id = ezcontentobject.contentclass_id AND
ezcontentobject.contentclass_id IN ( 17 ) AND
ezcontentobject_tree.contentobject_id = ezcontentobject_name.contentobject_id and
ezcontentobject_tree.contentobject_version = ezcontentobject_name.content_version and
( ezcontentobject_name.language_id & ezcontentobject.language_mask > 0 AND
( ( ezcontentobject.language_mask - ( ezcontentobject.language_mask & ezcontentobject_name.language_id ) ) & 1 )
+ ( ( ( ezcontentobject.language_mask - ( ezcontentobject.language_mask & ezcontentobject_name.language_id ) ) & 2 ) )
<
( ezcontentobject_name.language_id & 1 )
+ ( ( ezcontentobject_name.language_id & 2 ) )
)
AND ezcontentobject_tree.is_invisible = 0
AND
ezcontentobject.language_mask & 3 > 0
ORDER BY a0.sort_key_int DESC
LIMIT 0, 10
The thing is, when I execute the query above, nothing is returned because the query never stops. The CPU hits 100% and I have to restart mysql which means it's not a syntax issue.
Here is the explain of the query :
If anyone has any clue about this it will be very helpful.
I've solved it by rewriting everything using eZPersistentObject::fetchObjectList().

Applying a SQL function on Zend_Db_Table_Row::save()

Is it possible, when saving a Zend_Db_Table_Row, to make ZF apply a SQL function on one column?
For example, if $row->save() generates by default this SQL query:
UPDATE table SET field = ? WHERE id = ?;
I would like it to automatically apply the GeomFromText() function on this field:
UPDATE table SET field = GeomFromText(?) WHERE id = ?;
Thanks for any hint on how to do this with Zend_Db!
Define a custom update method in your class that inherits from Zend_Db_Table (not from the Zend_Db_Table_Row) and use a Zend_Db_Expr to set the column to the function return value.
See the docs here: http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.extending.insert-update.
I am just guessing but you could try this:
<?php
class MyTable extends Zend_Db_Table_Abstract
{
protected $_name = 'my_table';
public function update(array $data, $where) {
/**
* Build "col = ?" pairs for the statement,
* except for Zend_Db_Expr which is treated literally.
*/
$set = array();
$i = 0;
foreach ($data as $col => $val) {
if ($val instanceof Zend_Db_Expr) {
$val = $val->__toString();
unset($data[$col]);
} else {
if ($this->_db->supportsParameters('positional')) {
$val = ($col == 'field') ? 'GeomFromText(?)' : '?';
} else {
if ($this->_db->supportsParameters('named')) {
unset($data[$col]);
$data[':col'.$i] = $val;
$val = ($col == 'field') ? 'GeomFromText(:col'.$i.')' : ':col'.$i;
$i++;
} else {
/** #see Zend_Db_Adapter_Exception */
require_once 'Zend/Db/Adapter/Exception.php';
throw new Zend_Db_Adapter_Exception(get_class($this) ." doesn't support positional or named binding");
}
}
}
$set[] = $this->_db->quoteIdentifier($col, true) . ' = ' . $val;
}
$where = $this->_whereExpr($where);
/**
* Build the UPDATE statement
*/
$sql = "UPDATE "
. $this->_db->quoteIdentifier($this->_name , true)
. ' SET ' . implode(', ', $set)
. (($where) ? " WHERE $where" : '');
/**
* Execute the statement and return the number of affected rows
*/
if ($this->_db->supportsParameters('positional')) {
$stmt = $this->_db->query($sql, array_values($data));
} else {
$stmt = $this->_db->query($sql, $data);
}
$result = $stmt->rowCount();
return $result;
}
protected function _whereExpr($where)
{
if (empty($where)) {
return $where;
}
if (!is_array($where)) {
$where = array($where);
}
foreach ($where as $cond => &$term) {
// is $cond an int? (i.e. Not a condition)
if (is_int($cond)) {
// $term is the full condition
if ($term instanceof Zend_Db_Expr) {
$term = $term->__toString();
}
} else {
// $cond is the condition with placeholder,
// and $term is quoted into the condition
$term = $this->quoteInto($cond, $term);
}
$term = '(' . $term . ')';
}
$where = implode(' AND ', $where);
return $where;
}
}
?>