pepijn Posted September 17, 2013 Report Posted September 17, 2013 I added this method to Packages, so you can look for a package based on meta fields. It even includes some hackery to look up values in serialized data. /** * Searches packages that contain the given meta field * * @param string $key They service field key to search * @param string $value The service field value to search * @param string $subkey The key to look for in serialised meta * @return array An array of stdClass objects, each containing a service */ public function searchPackageMeta($key, $value, $subkey=null) { $module_id=1; $this->Record = $this->getPackages(); $qry = $this->Record->innerJoin("package_meta", "package_meta.package_id", "=", "packages.id", false)-> group("packages.id")-> where("package_meta.key", "=", $key)-> where("package_meta.value", "=", $value); if($subkey) { return $qry->orLike("package_meta.value", sprintf('%%s:%d:"%s";s:%d:"%s";%%', strlen($subkey), $subkey, strlen($value), $value))->fetchAll(); } else { return $qry->orLike("package_meta.value", sprintf('%%:%%:%%;s:%d:"%s";%%', strlen($value), $value))->fetchAll(); } } Example usage: find packages that include .com NameCheap domains. blesta.api('packages/searchPackageMeta', {'key': 'tlds', 'value': '.com'}) Find all SSL certificates blesta.api('packages/searchPackageMeta', {'key': 'type', 'value': 'ssl'}) Get all 33" washing machines blesta.api('packages/searchPackageMeta', {'key': 'dimensions', 'value': '33', 'subkey': 'diameter'}) Paul and Kniscasse 2 Quote
Tyson Posted September 17, 2013 Report Posted September 17, 2013 Looks like you have an unused $module_id (probably remnants from testing?). And the group on packages.id is extraneous, as Packages::getPackages() already groups on this field. But otherwise looks good. Quote
pepijn Posted September 18, 2013 Author Report Posted September 18, 2013 /** * Searches packages that contain the given meta field * * @param string $key They service field key to search * @param string $value The service field value to search * @param string $subkey The key to look for in serialised meta * @return array An array of stdClass objects, each containing a service */ public function searchPackageMeta($key, $value, $subkey=null) { $this->Record = $this->getPackages(); $qry = $this->Record->innerJoin("package_meta", "package_meta.package_id", "=", "packages.id", false)-> where("package_meta.key", "=", $key)-> where("package_meta.value", "=", $value); if($subkey) { return $qry->orLike("package_meta.value", sprintf('%%s:%d:"%s";s:%d:"%s";%%', strlen($subkey), $subkey, strlen($value), $value))->fetchAll(); } else { return $qry->orLike("package_meta.value", sprintf('%%:%%:%%;s:%d:"%s";%%', strlen($value), $value))->fetchAll(); } } Ok, fixed that. I think this is a really usefull thing to have, so if you'd be willing to include it in Blesta, I'm happy to give you all the permissions and copyrights and whatnot to do so. Quote
Cody Posted September 25, 2013 Report Posted September 25, 2013 You should create a plugin for this search using the Navigation.getSearchOptions event. For an example of that, take a look at the Support Manager plugin which does just that to create the "Ticket Search" option. All plugins can automatically extend the API too. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.