public function save($fixGaps = false) { if (!$this->libraryID) { throw new Exception("Library ID must be set before saving"); } Zotero_Searches::editCheck($this); if (!$this->changed) { Z_Core::debug("Search {$this->id} has not changed"); return false; } if (!isset($this->name) || $this->name === '') { throw new Exception("Name not provided for saved search"); } $shardID = Zotero_Shards::getByLibraryID($this->libraryID); Zotero_DB::beginTransaction(); $isNew = !$this->id || !$this->exists(); try { $searchID = $this->id ? $this->id : Zotero_ID::get('savedSearches'); Z_Core::debug("Saving search {$this->id}"); if (!$isNew) { $sql = "DELETE FROM savedSearchConditions WHERE searchID=?"; Zotero_DB::query($sql, $searchID, $shardID); } $key = $this->key ? $this->key : $this->generateKey(); $fields = "searchName=?, libraryID=?, `key`=?, dateAdded=?, dateModified=?,\n\t\t\t\t\t\tserverDateModified=?"; $timestamp = Zotero_DB::getTransactionTimestamp(); $params = array($this->name, $this->libraryID, $key, $this->dateAdded ? $this->dateAdded : $timestamp, $this->dateModified ? $this->dateModified : $timestamp, $timestamp); $shardID = Zotero_Shards::getByLibraryID($this->libraryID); if ($isNew) { $sql = "INSERT INTO savedSearches SET searchID=?, {$fields}"; $stmt = Zotero_DB::getStatement($sql, true, $shardID); Zotero_DB::queryFromStatement($stmt, array_merge(array($searchID), $params)); Zotero_Searches::cacheLibraryKeyID($this->libraryID, $key, $searchID); // Remove from delete log if it's there $sql = "DELETE FROM syncDeleteLogKeys WHERE libraryID=? AND objectType='search' AND `key`=?"; Zotero_DB::query($sql, array($this->libraryID, $key), $shardID); } else { $sql = "UPDATE savedSearches SET {$fields} WHERE searchID=?"; $stmt = Zotero_DB::getStatement($sql, true, $shardID); Zotero_DB::queryFromStatement($stmt, array_merge($params, array($searchID))); } // Close gaps in savedSearchIDs $saveConditions = array(); $i = 1; foreach ($this->conditions as $id => $condition) { if (!$fixGaps && $id != $i) { trigger_error('searchConditionIDs not contiguous and |fixGaps| not set in save() of saved search ' . $this->id, E_USER_ERROR); } $saveConditions[$i] = $condition; $i++; } $this->conditions = $saveConditions; // TODO: use proper bound parameters once DB class is updated foreach ($this->conditions as $searchConditionID => $condition) { $sql = "INSERT INTO savedSearchConditions (searchID,\n\t\t\t\t\t\tsearchConditionID, `condition`, mode, operator,\n\t\t\t\t\t\tvalue, required) VALUES (?,?,?,?,?,?,?)"; $sqlParams = array($searchID, $searchConditionID, $condition['condition'], $condition['mode'] ? $condition['mode'] : '', $condition['operator'] ? $condition['operator'] : '', $condition['value'] ? $condition['value'] : '', $condition['required'] ? 1 : 0); try { Zotero_DB::query($sql, $sqlParams, $shardID); } catch (Exception $e) { $msg = $e->getMessage(); if (strpos($msg, "Data too long for column 'value'") !== false) { throw new Exception("=Value '" . mb_substr($condition['value'], 0, 75) . "…' too long in saved search '" . $this->name . "'"); } throw $e; } } Zotero_DB::commit(); } catch (Exception $e) { Zotero_DB::rollback(); throw $e; } // If successful, set values in object if (!$this->id) { $this->id = $searchID; } if (!$this->key) { $this->key = $key; } return $this->id; }