<commit_before><?php
if(!defined('THUMBLIB_BASE_PATH'))
	require_once _PS_MODULE_DIR_ . 'lookbook/backend/classes/libs/phpthumb/ThumbLib.inc.php';

class LookObject extends ObjectModel
{
	public $id;
	public $id_lookbook;
	public $description;

	// SEO
	public $meta_title;
	public $meta_description;
	public $meta_keywords;
	public $link_rewrite;

	public $position;
	public $active;

	public $date_add;
	public $date_upd;

	protected $table = 'look';
	protected $identifier = 'id_look';

	protected $fieldsRequiredLang = array('meta_title', 'link_rewrite');
	protected $fieldsSizeLang = array(
		'meta_description' => 255,
		'meta_keywords' => 255,
		'meta_title' => 128,
		'link_rewrite' => 128,
		'description' => 3999999999999
	);

	protected $fieldsValidate = array('id_lookbook' => 'isUnsignedInt');
	protected $fieldsValidateLang = array(
		'meta_description' => 'isGenericName', 
		'meta_keywords' => 'isGenericName',
		'meta_title' => 'isGenericName',
		'link_rewrite' => 'isLinkRewrite',
		'description' => 'isString'
	);

	public function getFields()
	{
		parent::validateFields();
		if (isset($this->id))
			$fields['id_look'] = (int)($this->id);
		$fields['id_lookbook'] = (int)($this->id_lookbook);
		$fields['position'] = (int)($this->position);
		$fields['active'] = (int)($this->active);
		$fields['date_add']	 = pSQL($this->date_add);
		$fields['date_upd']	 = pSQL($this->date_upd);

		return $fields;
	}

	public function getTranslationsFieldsChild()
	{
		self::validateFieldsLang();

		return parent::getTranslationsFields(array('meta_title', 'meta_description', 'meta_keywords', 'link_rewrite', 'description'));
	}

	public function __construct($id_look = NULL, $id_lang = NULL, $backoffice = false)
	{
		parent::__construct($id_look, $id_lang);

		$this->products = $this->getProducts($id_lang, $backoffice);
		if($id_look)
		{
			$this->images = $this->getImages();
			$this->cover = $this->getCover();
			$this->nextLook = self::getNextLook($id_look, $id_lang);
			$this->prevLook = self::getPrevLook($id_look, $id_lang);
		}
	}

	public function getImages()
	{
		return Db::getInstance()->ExecuteS('
			SELECT li.`image`, li.`cover`
			FROM `'._DB_PREFIX_.'look_image` li
			WHERE li.`id_look` = ' . (int)$this->id);
	}

	public function getCover()
	{
		return Db::getInstance()->getRow('
			SELECT li.`image`
			FROM `'._DB_PREFIX_.'look_image` li
			WHERE li.`id_look` = '.(int)($this->id).'
			AND li.`cover` = 1');
	}

	public function getProducts($id_lang = NULL, $backoffice = false)
	{
		$products = array();

		if ($id_lang == NULL)
			$id_lang = Configuration::get('PS_LANG_DEFAULT');

		if($backoffice)
		{

		}
		else
		{
			$results = Db::getInstance()->getRow('
			SELECT lp.`id_product`
			FROM `'._DB_PREFIX_.'look_product` lp
			WHERE lp.`id_look` = '.(int)($this->id));
	
			if(!empty($results))
			{
				foreach($results AS $result)
					$products[] = new Product($result['id_product'], false, $id_lang);
			}
		}

		return $products;
	}

	public function setImages($files, $cover)
	{
		// Images
		$images = '';
		$nbImages = count($files['look_imageBox']['tmp_name']);
		for($i = 0; $i <= $nbImages; $i++)
		{
			if(isset($files['look_imageBox']['tmp_name'][$i]) AND !empty($files['look_imageBox']['tmp_name'][$i]))
  		{
  			$imgName = $this->id . '_' . $i;
				$ext = $this->_createSlide($files['look_imageBox']['tmp_name'][$i], $files['look_imageBox']['name'][$i], $imgName, true);
				$images .= "('" . $this->id . "', '" . $imgName . $ext . "', '".($cover == $i ? 1 : 0)."')";
  				if($i + 1 < $nbImages)
						$images .= ', ';
  		}
		}
		if($images != '')
		{
			if(!Db::getInstance()->Execute("INSERT INTO `" . _DB_PREFIX_ . "look_image` VALUES " . $images))
				return false;<commit_msg>Debug get all products<commit_after><?php
if(!defined('THUMBLIB_BASE_PATH'))
	require_once _PS_MODULE_DIR_ . 'lookbook/backend/classes/libs/phpthumb/ThumbLib.inc.php';

class LookObject extends ObjectModel
{
	public $id;
	public $id_lookbook;
	public $description;

	// SEO
	public $meta_title;
	public $meta_description;
	public $meta_keywords;
	public $link_rewrite;

	public $position;
	public $active;

	public $date_add;
	public $date_upd;

	protected $table = 'look';
	protected $identifier = 'id_look';

	protected $fieldsRequiredLang = array('meta_title', 'link_rewrite');
	protected $fieldsSizeLang = array(
		'meta_description' => 255,
		'meta_keywords' => 255,
		'meta_title' => 128,
		'link_rewrite' => 128,
		'description' => 3999999999999
	);

	protected $fieldsValidate = array('id_lookbook' => 'isUnsignedInt');
	protected $fieldsValidateLang = array(
		'meta_description' => 'isGenericName', 
		'meta_keywords' => 'isGenericName',
		'meta_title' => 'isGenericName',
		'link_rewrite' => 'isLinkRewrite',
		'description' => 'isString'
	);

	public function getFields()
	{
		parent::validateFields();
		if (isset($this->id))
			$fields['id_look'] = (int)($this->id);
		$fields['id_lookbook'] = (int)($this->id_lookbook);
		$fields['position'] = (int)($this->position);
		$fields['active'] = (int)($this->active);
		$fields['date_add']	 = pSQL($this->date_add);
		$fields['date_upd']	 = pSQL($this->date_upd);

		return $fields;
	}

	public function getTranslationsFieldsChild()
	{
		self::validateFieldsLang();

		return parent::getTranslationsFields(array('meta_title', 'meta_description', 'meta_keywords', 'link_rewrite', 'description'));
	}

	public function __construct($id_look = NULL, $id_lang = NULL, $backoffice = false)
	{
		parent::__construct($id_look, $id_lang);

		$this->products = $this->getProducts($id_lang);
		if($id_look)
		{
			$this->images = $this->getImages();
			$this->cover = $this->getCover();
			$this->nextLook = self::getNextLook($id_look, $id_lang);
			$this->prevLook = self::getPrevLook($id_look, $id_lang);
		}
	}

	public function getImages()
	{
		return Db::getInstance()->ExecuteS('
			SELECT li.`image`, li.`cover`
			FROM `'._DB_PREFIX_.'look_image` li
			WHERE li.`id_look` = ' . (int)$this->id);
	}

	public function getCover()
	{
		return Db::getInstance()->getRow('
			SELECT li.`image`
			FROM `'._DB_PREFIX_.'look_image` li
			WHERE li.`id_look` = '.(int)($this->id).'
			AND li.`cover` = 1');
	}

	public function getProducts($id_lang = NULL)
	{
		$products = array();

		if ($id_lang == NULL)
			$id_lang = Configuration::get('PS_LANG_DEFAULT');

		$results = Db::getInstance()->ExecuteS('
			SELECT lp.`id_product`
			FROM `'._DB_PREFIX_.'look_product` lp
			WHERE lp.`id_look` = '.(int)($this->id));

		if(!empty($results))
		{
			foreach($results AS $result)
				$products[] = new Product($result['id_product'], false, $id_lang);
		}

		return $products;
	}

	public function setImages($files, $cover)
	{
		// Images
		$images = '';
		$nbImages = count($files['look_imageBox']['tmp_name']);
		for($i = 0; $i <= $nbImages; $i++)
		{
			if(isset($files['look_imageBox']['tmp_name'][$i]) AND !empty($files['look_imageBox']['tmp_name'][$i]))
  		{
  			$imgName = $this->id . '_' . $i;
				$ext = $this->_createSlide($files['look_imageBox']['tmp_name'][$i], $files['look_imageBox']['name'][$i], $imgName, true);
				$images .= "('" . $this->id . "', '" . $imgName . $ext . "', '".($cover == $i ? 1 : 0)."')";
  				if($i + 1 < $nbImages)
						$images .= ', ';
  		}
		}
		if($images != '')
		{
			if(!Db::getInstance()->Execute("INSERT INTO `" . _DB_PREFIX_ . "look_image` VALUES " . $images))
				return false;
{'fraction_non_alphanumeric': 0.19875943905070118, 'fraction_numerical': 0.008090614886731391, 'mean_word_length': 4.535074626865671, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1439472', 'n_tokens_mistral': 3029, 'n_tokens_neox': 2683, 'n_words': 471}