/** * Removes/Adds the item from/to a gallery. * * This method is meant to be called via AJAX. * * @param string $action "remove" or "add" ("remove" "0" and "false" will all remove an item) * @param string $id The item MongoId * @param string $gallery_id The gallery MongoId * @return JSON Resposne */ public function association($action = 'remove', $id = null, $gallery_id = null) { // TODO: Use Minerva's access system (li3_access) // Bigger todo: update li3_acess (Nate's changes) and redo Minerva's access system completely. $user = Auth::check('minerva_user'); if ($user['role'] != 'administrator' && $user['role'] != 'content_editor') { $this->redirect('/'); return; } // Set the response to return $response = array('success' => true); // If there was no item or gallery id provided if (empty($id) || empty($gallery_id)) { $response['success'] = false; } // If $action is anything other than remove, 0, or false, the association will be added. $remove = $action == 'remove' || $action == '0' || $action == 'false' ? true : false; // Check to ensure that JSON was used to make the POST request if (!$this->request->is('json')) { $response['success'] = false; } // If we have what we need, update the item if ($response['success'] === true) { if ($remove) { $item_update_query = array('$pull' => array('_galleries' => $gallery_id)); } else { $item_update_query = array('$addToSet' => array('_galleries' => $gallery_id)); } $response['success'] = Item::update($item_update_query, array('_id' => $id), array('atomic' => false)); // Also $pull the item id from the gallery's document ordering field // The success of this is less important because if for some reason it isn't updated, // it should straighten out later when items are re-ordered and it doesn't even matter // if it's dirty. This is because it's not an association, it's just an ordering and // if an item doesn't exist in the order it will simply be ignored. if ($remove) { $page_update_query = array('$pull' => array('gallery_item_order' => $id)); } else { // If new association, the item will be added at the end of the order $page_update_query = array('$addToSet' => array('gallery_item_order' => $id)); } Page::update($page_update_query, array('_id' => $gallery_id), array('atomic' => false)); } $this->render(array('json' => $response)); }