Using 3rd-Party Libraries

Hello guys,

Hope you are enjoying Yii.

But i am in trouble.

I am trying to use use Codeigniter Cart library. For this i have followed the instruction from -using 3rd part library.

But i am getting the error mesage-

No direct script access allowed

Any help will be appreciated.

Thanks in advance. :-*

i have tried but faced similar problem.

Anyone have any idea…

This Link

Should work perfectly.Make sure you have imported all the required classes in the controller in the beginning Of controller file.

And the Path of your imported class should be exact same and last A class which you are trying use have a public scope.

I am quite sure it will work.

Hello jayant

Thanks for reply.

I have followed the instruction.

In Codeigniter Framework ‘Cart’ library uses another library ‘Session’.

Thats why i stored both library in the Vendors\ci directory.

In my constructor class of Controller i have done following:


public function init(){

               Yii::import('application.vendors.ci.*');

               require_once('ci/Cart.php');

               require_once('ci/Session.php');

               parent::init();

        }

And then in the actionMethod i have just instantiated the class.


$cart=new CI_Cart();

But getting "No direct script access allowed" reply in browser.

I hope this should be the way.

Anyway if you provide some example code to use it will be much helpful.

Thanks again.

Be smart and remove the




<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>



From the CI files, right at the first line.

Or, you could just define ‘BASEPATH’ before using any CI stuff. ;)

@ Jacmoe: what will be the BASEPATH and Where to define it?

@ twisted1919 if i remove the first line from CI file


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>

then another error is generated.

Call to undefined function get_instance()

Thanks both for answering…

@bit0123 - of course it does throw an error. You need to understand how ci works if you want to integrate something in Yii.

get_instance() is a ci function that gets you access to the CI super object, and in the CART class this is used to access another library, the session library to store the cart items.

I had a neeed for the cart class a while ago and i adapted it to Yii, and because i like to share, here it is:




<?php  if ( ! defined('YII_PATH')) exit('No direct script access allowed');

/**

 * CodeIgniter

 *

 * An open source application development framework for PHP 5.1.6 or newer

 *

 * @package		CodeIgniter

 * @author		ExpressionEngine Dev Team

 * @copyright	Copyright (c) 2006 - 2011, EllisLab, Inc.

 * @license		http://codeigniter.com/user_guide/license.html

 * @link		http://codeigniter.com

 * @since		Version 1.0

 * @filesource

 */


// ------------------------------------------------------------------------


/**

 * Shopping Cart Class

 *

 * @package		CodeIgniter

 * @subpackage	Libraries

 * @category	Shopping Cart

 * @author		ExpressionEngine Dev Team

 * @link		http://codeigniter.com/user_guide/libraries/cart.html

 */

class CI_Cart extends CApplicationComponent{


	// These are the regular expression rules that we use to validate the product ID and product name

	public $product_id_rules	= '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods

	public $product_name_rules	= '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods


	// Private variables.  Do not change!

	private $cart_contents	= array();




	/**

	 * Shopping Class Constructor

	 *

	 * The constructor loads the Session class, used to store the shopping cart contents.

	 */

	public function init()

	{

		// Grab the shopping cart array from the session table, if it exists

		if(Yii::app()->session->get('cart_contents'))

		{

			$this->cart_contents = Yii::app()->session->get('cart_contents');

		}

		else

		{

			// No cart exists so we'll set some base values

			$this->cart_contents['cart_total'] = 0;

			$this->cart_contents['total_items'] = 0;

		}

	}


	// --------------------------------------------------------------------


	/**

	 * Insert items into the cart and save it to the session table

	 *

	 * @access	public

	 * @param	array

	 * @return	bool

	 */

	function insert($items = array())

	{

		// Was any cart data passed? No? Bah...

		if ( ! is_array($items) OR count($items) == 0)

		{

			return FALSE;

		}


		// You can either insert a single product using a one-dimensional array,

		// or multiple products using a multi-dimensional one. The way we

		// determine the array type is by looking for a required array key named "id"

		// at the top level. If it's not found, we will assume it's a multi-dimensional array.


		$save_cart = FALSE;

		if (isset($items['id']))

		{

			if (($rowid = $this->_insert($items)))

			{

				$save_cart = TRUE;

			}

		}

		else

		{

			foreach ($items as $val)

			{

				if (is_array($val) AND isset($val['id']))

				{

					if ($this->_insert($val))

					{

						$save_cart = TRUE;

					}

				}

			}

		}


		// Save the cart data if the insert was successful

		if ($save_cart == TRUE)

		{

			$this->_save_cart();

			return isset($rowid) ? $rowid : TRUE;

		}


		return FALSE;

	}


	// --------------------------------------------------------------------


	/**

	 * Insert

	 *

	 * @access	private

	 * @param	array

	 * @return	bool

	 */

	function _insert($items = array())

	{

		// Was any cart data passed? No? Bah...

		if ( ! is_array($items) OR count($items) == 0)

		{

			return FALSE;

		}


		// --------------------------------------------------------------------


		// Does the $items array contain an id, quantity, price, and name?  These are required

		if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))

		{

			return FALSE;

		}


		// --------------------------------------------------------------------


		// Prep the quantity. It can only be a number.  Duh...

		$items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));

		// Trim any leading zeros

		$items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));


		// If the quantity is zero or blank there's nothing for us to do

		if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)

		{

			return FALSE;

		}


		// --------------------------------------------------------------------


		// Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods

		// Not totally sure we should impose this rule, but it seems prudent to standardize IDs.

		// Note: These can be user-specified by setting the $this->product_id_rules variable.

		if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id']))

		{

			return FALSE;

		}


		// --------------------------------------------------------------------


		// Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.

		// Note: These can be user-specified by setting the $this->product_name_rules variable.

		if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name']))

		{

			//return FALSE;

		}


		// --------------------------------------------------------------------


		// Prep the price.  Remove anything that isn't a number or decimal point.

		$items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));

		// Trim any leading zeros

		$items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));


		// Is the price a valid number?

		if ( ! is_numeric($items['price']))

		{

			return FALSE;

		}


		// --------------------------------------------------------------------


		// We now need to create a unique identifier for the item being inserted into the cart.

		// Every time something is added to the cart it is stored in the master cart array.

		// Each row in the cart array, however, must have a unique index that identifies not only

		// a particular product, but makes it possible to store identical products with different options.

		// For example, what if someone buys two identical t-shirts (same product ID), but in

		// different sizes?  The product ID (and other attributes, like the name) will be identical for

		// both sizes because it's the same shirt. The only difference will be the size.

		// Internally, we need to treat identical submissions, but with different options, as a unique product.

		// Our solution is to convert the options array to a string and MD5 it along with the product ID.

		// This becomes the unique "row ID"

		if (isset($items['options']) AND count($items['options']) > 0)

		{

			$rowid = md5($items['id'].implode('', $items['options']));

		}

		else

		{

			// No options were submitted so we simply MD5 the product ID.

			// Technically, we don't need to MD5 the ID in this case, but it makes

			// sense to standardize the format of array indexes for both conditions

			$rowid = md5($items['id']);

		}


		// --------------------------------------------------------------------


		// Now that we have our unique "row ID", we'll add our cart items to the master array


		// let's unset this first, just to make sure our index contains only the data from this submission

		unset($this->cart_contents[$rowid]);


		// Create a new index with our new row ID

		$this->cart_contents[$rowid]['rowid'] = $rowid;


		// And add the new items to the cart array

		foreach ($items as $key => $val)

		{

			$this->cart_contents[$rowid][$key] = $val;

		}


		// Woot!

		return $rowid;

	}


	// --------------------------------------------------------------------


	/**

	 * Update the cart

	 *

	 * This function permits the quantity of a given item to be changed.

	 * Typically it is called from the "view cart" page if a user makes

	 * changes to the quantity before checkout. That array must contain the

	 * product ID and quantity for each item.

	 *

	 * @access	public

	 * @param	array

	 * @param	string

	 * @return	bool

	 */

	function update($items = array())

	{

		// Was any cart data passed?

		if ( ! is_array($items) OR count($items) == 0)

		{

			return FALSE;

		}


		// You can either update a single product using a one-dimensional array,

		// or multiple products using a multi-dimensional one.  The way we

		// determine the array type is by looking for a required array key named "id".

		// If it's not found we assume it's a multi-dimensional array

		$save_cart = FALSE;

		if (isset($items['rowid']) AND isset($items['qty']))

		{

			if ($this->_update($items) == TRUE)

			{

				$save_cart = TRUE;

			}

		}

		else

		{

			foreach ($items as $val)

			{

				if (is_array($val) AND isset($val['rowid']) AND isset($val['qty']))

				{

					if ($this->_update($val) == TRUE)

					{

						$save_cart = TRUE;

					}

				}

			}

		}


		// Save the cart data if the insert was successful

		if ($save_cart == TRUE)

		{

			$this->_save_cart();

			return TRUE;

		}


		return FALSE;

	}


	// --------------------------------------------------------------------


	/**

	 * Update the cart

	 *

	 * This function permits the quantity of a given item to be changed.

	 * Typically it is called from the "view cart" page if a user makes

	 * changes to the quantity before checkout. That array must contain the

	 * product ID and quantity for each item.

	 *

	 * @access	private

	 * @param	array

	 * @return	bool

	 */

	function _update($items = array())

	{

		// Without these array indexes there is nothing we can do

		if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->cart_contents[$items['rowid']]))

		{

			return FALSE;

		}


		// Prep the quantity

		$items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);


		// Is the quantity a number?

		if ( ! is_numeric($items['qty']))

		{

			return FALSE;

		}


		// Is the new quantity different than what is already saved in the cart?

		// If it's the same there's nothing to do

		if ($this->cart_contents[$items['rowid']]['qty'] == $items['qty'])

		{

			return FALSE;

		}


		// Is the quantity zero?  If so we will remove the item from the cart.

		// If the quantity is greater than zero we are updating

		if ($items['qty'] == 0)

		{

			unset($this->cart_contents[$items['rowid']]);

		}

		else

		{

			$this->cart_contents[$items['rowid']]['qty'] = $items['qty'];

		}


		return TRUE;

	}


	// --------------------------------------------------------------------


	/**

	 * Save the cart array to the session DB

	 *

	 * @access	private

	 * @return	bool

	 */

	function _save_cart()

	{

		// Unset these so our total can be calculated correctly below

		unset($this->cart_contents['total_items']);

		unset($this->cart_contents['cart_total']);


		// Lets add up the individual prices and set the cart sub-total

		$total = 0;

		$items = 0;

		foreach ($this->cart_contents as $key => $val)

		{

			// We make sure the array contains the proper indexes

			if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))

			{

				continue;

			}


			$total += ($val['price'] * $val['qty']);

			$items += $val['qty'];


			// Set the subtotal

			$this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']);

		}


		// Set the cart total and total items.

		$this->cart_contents['total_items'] = $items;

		$this->cart_contents['cart_total'] = $total;


		// Is our cart empty?  If so we delete it from the session

		if (count($this->cart_contents) <= 2)

		{

			Yii::app()->session->remove('cart_contents');


			// Nothing more to do... coffee time!

			return FALSE;

		}


		// If we made it this far it means that our cart has data.

		// Let's pass it to the Session class so it can be stored

        Yii::app()->session->add('cart_contents',$this->cart_contents);

		// Woot!

		return TRUE;

	}


	// --------------------------------------------------------------------


	/**

	 * Cart Total

	 *

	 * @access	public

	 * @return	integer

	 */

	function total()

	{

		return $this->cart_contents['cart_total'];

	}


	// --------------------------------------------------------------------


	/**

	 * Total Items

	 *

	 * Returns the total item count

	 *

	 * @access	public

	 * @return	integer

	 */

	function total_items()

	{

		return $this->cart_contents['total_items'];

	}


	// --------------------------------------------------------------------


	/**

	 * Cart Contents

	 *

	 * Returns the entire cart array

	 *

	 * @access	public

	 * @return	array

	 */

	function contents()

	{

		$cart = $this->cart_contents;


		// Remove these so they don't create a problem when showing the cart table

		unset($cart['total_items']);

		unset($cart['cart_total']);


		return $cart;

	}


	// --------------------------------------------------------------------


	/**

	 * Has options

	 *

	 * Returns TRUE if the rowid passed to this function correlates to an item

	 * that has options associated with it.

	 *

	 * @access	public

	 * @return	array

	 */

	function has_options($rowid = '')

	{

		if ( ! isset($this->cart_contents[$rowid]['options']) OR count($this->cart_contents[$rowid]['options']) === 0)

		{

			return FALSE;

		}


		return TRUE;

	}


	// --------------------------------------------------------------------


	/**

	 * Product options

	 *

	 * Returns the an array of options, for a particular product row ID

	 *

	 * @access	public

	 * @return	array

	 */

	function product_options($rowid = '')

	{

		if ( ! isset($this->cart_contents[$rowid]['options']))

		{

			return array();

		}


		return $this->cart_contents[$rowid]['options'];

	}


	// --------------------------------------------------------------------


	/**

	 * Format Number

	 *

	 * Returns the supplied number with commas and a decimal point.

	 *

	 * @access	public

	 * @return	integer

	 */

	function format_number($n = '')

	{

		if ($n == '')

		{

			return '';

		}


		// Remove anything that isn't a number or decimal point.

		$n = trim(preg_replace('/([^0-9\.])/i', '', $n));


		return number_format($n, 2, '.', ',');

	}


	// --------------------------------------------------------------------


	/**

	 * Destroy the cart

	 *

	 * Empties the cart and kills the session

	 *

	 * @access	public

	 * @return	null

	 */

	function destroy()

	{

		$this->cart_contents=array();


		$this->cart_contents['cart_total'] = 0;

		$this->cart_contents['total_items'] = 0;


		Yii::app()->session->remove('cart_contents');

	}




}



Add it into your config/main.php in the components section as:




'cart'=>array(

            'class'=>'path.to.CI_Cart',

        ),



And use it like:




Yii::app()->cart->insert($items);



@twisted1919

Thanks a lot :-*