private function _list_products($limit = 5, $product_group = '') { $product_qb = new org_openpsa_qbpager('org_openpsa_products_product_dba', 'latest_products'); $this->_request_data['product_qb'] =& $product_qb; $product_qb->results_per_page = $limit; $product_qb->set_limit($limit); $product_qb->add_order('metadata.published', 'DESC'); if ($product_group != '') { $group_qb = org_openpsa_products_product_group_dba::new_query_builder(); $group_qb->add_constraint('code', '=', $product_group); $groups = $group_qb->execute(); if (count($groups) == 0) { return false; // No matching group } else { $categories_qb = org_openpsa_products_product_group_dba::new_query_builder(); $categories_qb->add_constraint('up', '=', $groups[0]->id); $categories = $categories_qb->execute(); $categories_in = array(); if (count($categories) == 0) { /* No matching categories belonging to this group * So we can search for the application using only * this group id */ $product_qb->add_constraint('productGroup', 'INTREE', $groups[0]->id); } else { for ($i = 0; $i < count($categories); $i++) { $categories_in[$i] = $categories[$i]->id; } $product_qb->add_constraint('productGroup', 'IN', $categories_in); } } } if ($this->_config->get('enable_scheduling')) { $product_qb->add_constraint('start', '<=', time()); $product_qb->begin_group('OR'); /* * List products that either have no defined end-of-market dates * or are still in market */ $product_qb->add_constraint('end', '=', 0); $product_qb->add_constraint('end', '>=', time()); $product_qb->end_group(); } $this->_request_data['products'] = $product_qb->execute(); $this->_request_data['product_group'] = $product_group; }
private function _list_group_products() { $product_qb = new org_openpsa_qbpager('org_openpsa_products_product_dba', 'org_openpsa_products_product_dba'); $product_qb->results_per_page = $this->_config->get('products_per_page'); if (count($this->_request_data['linked_products']) > 0) { $product_qb->begin_group('OR'); } if ($this->_request_data['group'] && $this->_request_data['group']->orgOpenpsaObtype == org_openpsa_products_product_group_dba::TYPE_SMART) { // Smart group, query products by stored constraints $constraints = $this->_request_data['group']->list_parameters('org.openpsa.products:constraints'); if (empty($constraints)) { $product_qb->add_constraint('productGroup', '=', $this->_request_data['parent_group']); } $reflector = new midgard_reflection_property('org_openpsa_products_product'); foreach ($constraints as $constraint_string) { $constraint_members = explode(',', $constraint_string); if (count($constraint_members) != 3) { throw new midcom_error("Invalid constraint '{$constraint_string}'"); } // Reflection is needed here for safety $field_type = $reflector->get_midgard_type($constraint_members[0]); switch ($field_type) { case 4: throw new midcom_error("Invalid constraint: '{$constraint_members[0]}' is not a Midgard property"); case MGD_TYPE_INT: $constraint_members[2] = (int) $constraint_members[2]; break; case MGD_TYPE_FLOAT: $constraint_members[2] = (double) $constraint_members[2]; break; case MGD_TYPE_BOOLEAN: $constraint_members[2] = (bool) $constraint_members[2]; break; } $product_qb->add_constraint($constraint_members[0], $constraint_members[1], $constraint_members[2]); } } else { if ($this->_request_data['handler_id'] == 'list_intree') { $product_qb->add_constraint('productGroup', '=', $this->_request_data['parent_category_id']); } else { if ($this->_request_data['handler_id'] == 'listall') { $categories_qb = org_openpsa_products_product_group_dba::new_query_builder(); $categories_qb->add_constraint('up', '=', $this->_request_data['group']->id); $categories = $categories_qb->execute(); for ($i = 0; $i < count($categories); $i++) { $categories_in[$i] = $categories[$i]->id; } $product_qb->add_constraint('productGroup', 'IN', $categories_in); } else { $product_qb->add_constraint('productGroup', '=', $this->_request_data['parent_group']); } } } if (count($this->_request_data['linked_products']) > 0) { $product_qb->add_constraint('id', 'IN', $this->_request_data['linked_products']); $product_qb->end_group(); } // This should be a helper function, same functionality, but with different config-parameter is used in /handler/product/search.php foreach ($this->_config->get('products_listing_order') as $ordering) { $this->_add_ordering($product_qb, $ordering); } if ($this->_config->get('enable_scheduling')) { $product_qb->add_constraint('start', '<=', time()); $product_qb->begin_group('OR'); /* * List products that either have no defined end-of-market dates * or are still in market */ $product_qb->add_constraint('end', '=', 0); $product_qb->add_constraint('end', '>=', time()); $product_qb->end_group(); } $this->_request_data['products'] = $product_qb->execute(); $this->_request_data['products_qb'] =& $product_qb; }
/** * Search products using Midgard 1.8+ Query Builder */ private function _qb_search($constraints) { $qb = new org_openpsa_qbpager('org_openpsa_products_product_dba', 'org_openpsa_products_product_dba'); $qb->results_per_page = $this->_config->get('products_per_page'); // Check that the object has correct schema $mc = new midgard_collector('midgard_parameter', 'domain', 'midcom.helper.datamanager2'); $mc->set_key_property('parentguid'); $mc->add_constraint('name', '=', 'schema_name'); $mc->add_constraint('value', '=', $this->_request_data['search_schema']); $mc->execute(); $keys = $mc->list_keys(); if (!empty($keys)) { $qb->add_constraint('guid', 'IN', array_keys($keys)); } unset($mc, $keys); if ($this->_request_data['search_type'] == 'OR') { $qb->begin_group('OR'); } foreach ($constraints as $constraint) { $storage = $this->_request_data['schemadb_product'][$this->_request_data['search_schema']]->fields[$constraint['property']]['storage']; debug_print_r('constraint', $constraint); debug_print_r('storage', $storage); if (!is_array($storage) || preg_match('/^%+$/', $constraint['value'])) { continue; } if ($storage['location'] == 'parameter' || $storage['location'] == 'configuration') { $mc = new midgard_collector('midgard_parameter', 'domain', $storage['domain']); $mc->set_key_property('parentguid'); $mc->add_constraint('name', '=', $constraint['property']); $mc->add_constraint('value', $constraint['constraint'], $constraint['value']); $mc->execute(); $keys = $mc->list_keys(); if (!empty($keys)) { $qb->add_constraint('guid', 'IN', array_keys($keys)); } unset($mc, $keys); } else { // Simple field storage if (is_numeric($constraint['value'])) { // TODO: When 1.8.4 becomes more common we can reflect this instead $constraint['value'] = (int) $constraint['value']; } $qb->add_constraint($storage['location'], $constraint['constraint'], $constraint['value']); } } if ($this->_request_data['search_type'] == 'OR') { $qb->end_group(); } foreach ($this->_config->get('search_index_order') as $ordering) { $this->_add_ordering($qb, $ordering); } $ret = $qb->execute(); /* FIXME: It this the right way to do this? */ $this->_request_data['search_qb'] =& $qb; // Check schemas this way until the core issue is fixed foreach ($ret as $k => $product) { $schema = $product->get_parameter('midcom.helper.datamanager2', 'schema_name'); debug_add("product schema '{$schema}' vs desired schema '{$this->_request_data['search_schema']}'"); if ($schema == $this->_request_data['search_schema']) { continue; } unset($ret[$k]); } // array_merge reindexes the array to be continous return array_merge($ret); }