<commit_before><?php
App::uses('AppController', 'Controller');

/**
 * "Listings" controller
 *
 * Handles requests about myself.
 */
class ListingsController extends AppController
{
	/**
	 * Controller name
	 *
	 * @var string
	 */
	public $name = 'Listing';

	/**
	 * The components this controller uses.
	 * 
	 * @var array
	 */
	public $components = array(
		'Products'
	);

	/**
	 * This controller does not use a model
	 *
	 * @var array
	 */
	public $uses = array(
		'Product',
		'ProductListing'
	);
	
	/**
	 * Creates a new listing.
	 */
	public function create()
	{
		if ($this->request->is('post'))
		{
			//Try creating the product first.
			if (!$this->Product->find('count', array(
				'conditions' => array(
					'Product.id' => $this->request->data['product_id'])
				)))
			{
				$this->Product->id = $this->request->data['product_id'];
				$this->Product->save(array());
			}
			
			//If the form data can be validated and saved...
			$this->request->data['creator_id'] = $this->Auth->user('id');
			if ($this->ProductListing->save($this->request->data))
			{
				//Set a session flash message and redirect.
				$this->Session->setFlash('Listing saved.');
				$this->set('_serialize', array());
			}
			else
			{
				debug($this->Recipe->validationErrors);
			}
		}
	}

	/**
	 * Searches for a listing containing the given product.
	 * 
	 * This can be invoked through a call to requestAction, which then will return
	 * the object that would otherwise be serialised to the client.
	 */
	public function search($description, $start = 0)
	{
		$products = $this->Products->searchSemantics3($description, $start);
		$productIds = array_map(function($item) {
				return $item->id;
			}, $products->results);

		$listings = $this->ProductListing->findByProductId($productIds);

		$this->set('listings', $listings);
		$this->set('_serialize', array('listings'));
	}
}<commit_msg>Implemented adding comments for listinsg.<commit_after><?php
App::uses('AppController', 'Controller');

/**
 * "Listings" controller
 *
 * Handles requests about myself.
 */
class ListingsController extends AppController
{
	/**
	 * Controller name
	 *
	 * @var string
	 */
	public $name = 'Listing';

	/**
	 * The components this controller uses.
	 * 
	 * @var array
	 */
	public $components = array(
		'Products'
	);

	/**
	 * This controller does not use a model
	 *
	 * @var array
	 */
	public $uses = array(
		'Product',
		'ProductListing'
	);
	
	public function isAuthorized($user)
	{
		//All registered users can add comments
		//TODO: check for friends-only listings.
		if ($this->action === 'comment' || $this->action === 'deleteComment')
		{
			return parent::isAuthorized($user);
		}

		//The owner of a listing can edit and delete it
		else if (in_array($this->action, array('edit', 'delete')))
		{
			$postId = $this->request->params['pass'][0];
			if ($this->Post->isOwnedBy($postId, $user['id']))
			{
				return true;
			}
		}

		return parent::isAuthorized($user);
	}

	/**
	 * Creates a new listing.
	 */
	public function create()
	{
		if ($this->request->is('post'))
		{
			//Try creating the product first.
			if (!$this->Product->find('count', array(
				'conditions' => array(
					'Product.id' => $this->request->data['product_id'])
				)))
			{
				$this->Product->id = $this->request->data['product_id'];
				$this->Product->save(array());
			}
			
			//If the form data can be validated and saved...
			$this->request->data['creator_id'] = $this->Auth->user('id');
			if ($this->ProductListing->save($this->request->data))
			{
				//Set a session flash message and redirect.
				$this->Session->setFlash('Listing saved.');
				$this->set('_serialize', array());
			}
			else
			{
				debug($this->Recipe->validationErrors);
			}
		}
	}

	/**
	 * Searches for a listing containing the given product.
	 * 
	 * This can be invoked through a call to requestAction, which then will return
	 * the object that would otherwise be serialised to the client.
	 */
	public function search($description, $start = 0)
	{
		$products = $this->Products->searchSemantics3($description, $start);
		$productIds = array_map(function($item) {
				return $item->id;
			}, $products->results);

		$listings = $this->ProductListing->findByProductId($productIds);

		$this->set('listings', $listings);
		$this->set('_serialize', array('listings'));
	}
	
	public function comment()
	{
		if ($this->request->is('post'))
		{
			//Reject invalid listings.
			if (!$this->ProductListing->find('count', array(
				'conditions' => array(
					'ProductListing.id' => $this->request->data['product_listing_id'])
				)))
			{
				throw new FileNotFoundException();
			}
			
			//If the form data can be validated and saved...
			$this->request->data['author_id'] = $this->Auth->user('id');
			if ($this->ProductListing->save($this->request->data))
			{
				//Set a session flash message and redirect.
				$this->Session->setFlash('Listing saved.');
				$this->set('_serialize', array());
			}
			else
			{
				debug($this->Recipe->validationErrors);
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.17700693756194252, 'fraction_numerical': 0.0009910802775024777, 'mean_word_length': 3.2871707731520816, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1075889', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1576, 'n_words': 383}