/** * Adds a product * * @param array $input Raw input data * @return boolean */ public function add(array $input) { $input = $this->prepareInput($input); // Short-cuts $product =& $input['data']['product']; // Initial view count $product['views'] = 0; // For cross-database compatibility, the date must be generated here, not in the mapper $product['date'] = date('Y-m-d', time()); $files =& $input['files']['file']; // Insert should be first, because we need to provide an id $this->productMapper->insert(ArrayUtils::arrayWithout($product, array('slug'))); // After insert, now we can get an id $id = $this->getLastId(); // Do we have images? if (!empty($files)) { // So let's first try to upload them if ($this->imageManager->upload($id, $files)) { // And write their base names into storage foreach ($files as $file) { $this->imageMapper->insert($id, $file->getName(), 0, true); } } else { trigger_error('Failed to upload product additional image', E_USER_NOTICE); } } $this->track('Product "%s" has been added', $product['name']); // Add a web page now return $this->webPageManager->add($id, $product['slug'], 'Shop (Products)', 'Shop:Product@indexAction', $this->productMapper); }