/** * Manipulate a Select object based on a set of criteria. * * @param Omeka_Db_Select $select * @param array $params Possible parameters include: * <ul> * <li>record_types - array - Usually one or more of the following: * All, Item, File</li> * <li>sort - string - One of the following values: alpha</li> * <li>element_set_name - string - Name of the element set to which * results should belong.</li> * </ul> */ public function applySearchFilters($select, $params) { $db = $this->getDb(); // Retrieve only elements matching a specific record type. if (array_key_exists('record_types', $params)) { $where = array(); foreach ($params['record_types'] as $recordTypeName) { if ($recordTypeName == 'All') { $where[] = 'element_sets.record_type IS NULL'; } else { $where[] = 'element_sets.record_type = ' . $db->quote($recordTypeName); } } $select->where('(' . join(' OR ', $where) . ')'); } if (array_key_exists('sort', $params)) { if ($params['sort'] == 'alpha') { $select->order('elements.name ASC'); } else { if ($params['sort'] == 'alphaBySet') { $select->order('element_sets.name ASC')->order('elements.name ASC'); } } } if (array_key_exists('element_set_name', $params)) { $select->where('element_sets.name = binary ?', (string) $params['element_set_name']); } if (array_key_exists('element_name', $params)) { $select->where('elements.name = binary ?', (string) $params['element_name']); } // Retrive results including, but not limited to, a specific item type. if (array_key_exists('item_type_id', $params)) { $select->joinLeft(array('item_types_elements' => $db->ItemTypesElements), 'item_types_elements.element_id = elements.id', array()); $select->where('item_types_elements.item_type_id = ? OR item_types_elements.item_type_id IS NULL', (int) $params['item_type_id']); } else { if (array_key_exists('exclude_item_type', $params)) { $select->where('element_sets.name != ?', ElementSet::ITEM_TYPE_NAME); } else { if (array_key_exists('item_type', $params)) { //for the API for item_types $select->joinLeft(array('item_types_elements' => $db->ItemTypesElements), 'item_types_elements.element_id = elements.id', array()); $select->where('item_types_elements.item_type_id = ? ', (int) $params['item_type']); } } } // REST API params. if (array_key_exists('name', $params)) { $select->where("elements.name = ?", $params['name']); } if (array_key_exists('element_set', $params)) { $select->where("elements.element_set_id = ?", $params['element_set']); } }