/** * Adds a category * * @param array $input Raw input data * @return boolean */ public function add(array $input) { $input = $this->prepareInput($input); $category =& $input['data']['category']; // Cover is always empty by default $category['cover'] = ''; // If we have a cover, then we need to upload it if (!empty($input['files']['file'])) { $file =& $input['files']['file']; // Now filter original file's name $this->filterFileInput($file); // Override empty cover's value now $category['cover'] = $file[0]->getName(); } $category['web_page_id'] = ''; if ($this->categoryMapper->insert(ArrayUtils::arrayWithout($category, array('slug', 'menu')))) { $id = $this->getLastId(); // If we have a cover, then we need to upload it if (!empty($input['files']['file'])) { $this->imageManager->upload($id, $input['files']['file']); } $this->track('Added category "%s"', $category['name']); if ($this->webPageManager->add($id, $category['slug'], 'Shop (Categories)', 'Shop:Category@indexAction', $this->categoryMapper)) { // Do the work in case menu widget was injected if ($this->hasMenuWidget()) { $this->addMenuItem($this->webPageManager->getLastId(), $category['name'], $input['data']); } } return true; } }
/** * Updates a member * * @param array $input Raw input data * @return boolean Depending on success */ public function update(array $input) { // Just a reference $form =& $input['data']['team']; if (!empty($input['files'])) { $file =& $input['files']['file']; // When overriding a photo, we need to remove old one from the file-system if ($this->imageManager->delete($form['id'], $form['photo'])) { // Filter names $this->filterFileInput($file); // Now upload a new one $form['photo'] = $file[0]->getName(); $this->imageManager->upload($form['id'], $file); } else { // Failed to remove old photo: return false; } } $this->track('Member "%s" has been updated', $form['name']); return $this->teamMapper->update($form); }
/** * Updates a product * * @param array $input Raw input data * @return boolean */ public function update(array $input) { $input = $this->prepareInput($input); // Product data $product =& $input['data']['product']; // Current product id we're dealing with $productId = $product['id']; // An array of new appended images from a user $appendedImages = $input['files']['file']; if (!empty($input['files'])) { // Array of changed images, representing an id => FileBag instance $changedImages = $this->getChangedImages($input['files']); // Do we have any changed image? if (!empty($changedImages)) { foreach ($changedImages as $imageId => $fileBag) { // First of all we need to remove old image if ($this->imageManager->delete($productId, $this->imageMapper->fetchFileNameById($imageId))) { $this->filterFileInput($fileBag); $this->imageManager->upload($productId, $fileBag); $this->imageMapper->updateFileNameById($imageId, $fileBag[0]->getName()); } } // PHP hasn't block scope, so we have to remove it manually unset($fileBag); } } // New user appended images if (!empty($appendedImages)) { // Upload all appended files firstly if ($this->imageManager->upload($productId, $appendedImages)) { // Then save them foreach ($appendedImages as $fileBag) { $this->imageMapper->insert($productId, $fileBag->getName(), 1, 1); } } } $photos =& $input['data']['photos']; // Do we have images to delete? if (isset($photos['toDelete'])) { // Grab photo ids we're gonna remove $ids = array_keys($photos['toDelete']); foreach ($ids as $imageId) { // Try to remove on file-system first if ($this->imageManager->delete($productId, $this->imageMapper->fetchFileNameById($imageId))) { // If successful, then remove from a storage as well $this->imageMapper->deleteById($imageId); } } } if (isset($photos['published'])) { // Update photos published state foreach ($photos['published'] as $id => $published) { $this->imageMapper->updatePublishedById($id, $published); } } if (isset($photos['order'])) { // Update photos order foreach ($photos['order'] as $id => $order) { $this->imageMapper->updateOrderById($id, $order); } } // Update a cover now if (isset($photos['cover'])) { $product['cover'] = $photos['cover']; } $this->track('Product "%s" has been updated', $product['name']); $this->webPageManager->update($product['web_page_id'], $product['slug']); return $this->productMapper->update(ArrayUtils::arrayWithout($product, array('slug'))); }