Ejemplo n.º 1
0
 /**
  * Helper to make an array usable with DM2 select datatype for selecting product groups
  *
  * @param mixed $up            Either the ID or GUID of the product group
  * @param string $prefix       Prefix for the code
  * @param string $keyproperty  Property to use as the key of the resulting array
  * @param array $label_fields  Object properties to show in the label (will be shown space separated)
  * @return array
  */
 public static function list_groups($up = 0, $prefix = '', $keyproperty = 'id', $order_by_score = false, $label_fields = array('code', 'title'))
 {
     static $result_cache = array();
     $cache_key = md5($up . $keyproperty . $prefix . $order_by_score . implode('', $label_fields));
     if (isset($result_cache[$cache_key])) {
         return $result_cache[$cache_key];
     }
     $result_cache[$cache_key] = array();
     $ret =& $result_cache[$cache_key];
     if (empty($up)) {
         // TODO: use reflection to see what kind of property this is ?
         if ($keyproperty == 'id') {
             $ret[0] = midcom::get('i18n')->get_string('toplevel', 'org.openpsa.products');
         } else {
             $ret[''] = midcom::get('i18n')->get_string('toplevel', 'org.openpsa.products');
         }
     }
     if (mgd_is_guid($up)) {
         $group = new org_openpsa_products_product_group_dba($up);
         $up = $group->id;
     }
     $mc = org_openpsa_products_product_group_dba::new_collector('up', (int) $up);
     $mc->add_value_property('title');
     $mc->add_value_property('code');
     $mc->add_value_property('id');
     if ($keyproperty !== 'id') {
         $mc->add_value_property($keyproperty);
     }
     foreach ($label_fields as $fieldname) {
         if ($fieldname == 'id' || $fieldname == $keyproperty) {
             continue;
         }
         $mc->add_value_property($fieldname);
     }
     unset($fieldname);
     // Order by score if required
     if ($order_by_score) {
         $mc->add_order('metadata.score', 'DESC');
     }
     $mc->add_order('code');
     $mc->add_order('title');
     $mc->execute();
     $mc_keys = $mc->list_keys();
     foreach ($mc_keys as $mc_key => $dummy) {
         $id = $mc->get_subkey($mc_key, 'id');
         $key = $mc->get_subkey($mc_key, $keyproperty);
         $ret[$key] = $prefix;
         foreach ($label_fields as $fieldname) {
             $field_val = $mc->get_subkey($mc_key, $fieldname);
             $ret[$key] .= "{$field_val} ";
         }
         unset($fieldname, $field_val);
         $ret = $ret + org_openpsa_products_product_group_dba::list_groups($id, "{$prefix} > ", $keyproperty, $label_fields);
         unset($id, $key);
     }
     unset($mc, $mc_keys, $dummy, $mc_key);
     return $ret;
 }
Ejemplo n.º 2
0
 /**
  * Looks up a product to display.
  *
  * @param mixed $handler_id The ID of the handler.
  * @param Array $args The argument list.
  * @param Array &$data The local request data.
  */
 public function _handler_view($handler_id, array $args, array &$data)
 {
     if (preg_match('/_raw$/', $handler_id)) {
         midcom::get()->skip_page_style = true;
     }
     $qb = org_openpsa_products_product_dba::new_query_builder();
     if (preg_match('/^view_product_intree/', $handler_id)) {
         $group_qb = org_openpsa_products_product_group_dba::new_query_builder();
         if (mgd_is_guid($args[0])) {
             $group_qb->add_constraint('guid', '=', $args[0]);
         } else {
             $group_qb->add_constraint('code', '=', $args[0]);
         }
         $groups = $group_qb->execute();
         if (empty($groups)) {
             throw new midcom_error_notfound("Product group {$args[0]} not found");
         }
         $categories_mc = org_openpsa_products_product_group_dba::new_collector('up', $groups[0]->id);
         $categories_in = $categories_mc->get_values('id');
         if (count($categories_in) == 0) {
             /* No matching categories belonging to this group
              * So we can search for the application using only
              * this group id
              */
             $qb->add_constraint('productGroup', 'INTREE', $groups[0]->id);
         } else {
             $categories_in[] = $groups[0]->id;
             $qb->add_constraint('productGroup', 'IN', $categories_in);
         }
         if (mgd_is_guid($args[1])) {
             $qb->add_constraint('guid', '=', $args[1]);
         } else {
             $qb->add_constraint('code', '=', $args[1]);
         }
     } else {
         if (mgd_is_guid($args[0])) {
             $qb->add_constraint('guid', '=', $args[0]);
         } else {
             $qb->add_constraint('code', '=', $args[0]);
         }
     }
     if ($this->_config->get('enable_scheduling')) {
         $qb->add_constraint('start', '<=', time());
         $qb->begin_group('OR');
         /*
          * List products that either have no defined end-of-market dates
          * or are still in market
          */
         $qb->add_constraint('end', '=', 0);
         $qb->add_constraint('end', '>=', time());
         $qb->end_group();
     }
     $results = $qb->execute();
     $this->_request_data['is_linked_from'] = '';
     if (!empty($results)) {
         $this->_product = $results[0];
         if ($this->_config->get('enable_productlinks') && $this->_product->productGroup != 0) {
             $root_group_guid = $this->_config->get('root_group');
             if ($root_group_guid != '') {
                 $root_group = org_openpsa_products_product_group_dba::get_cached($root_group_guid);
             }
             if ($root_group->id != $this->_product->productGroup) {
                 $product_group = new org_openpsa_products_product_group_dba($this->_product->productGroup);
                 $mc_intree = org_openpsa_products_product_group_dba::new_collector('id', $product_group->id);
                 $mc_intree->add_constraint('up', 'INTREE', $root_group->id);
                 $count = $mc_intree->count();
                 if ($count == 0) {
                     $mc_intree = org_openpsa_products_product_link_dba::new_collector('product', $this->_product->id);
                     $mc_intree->add_constraint('productGroup', 'INTREE', $root_group->id);
                     $mc_intree->execute();
                     $results = $mc_intree->list_keys();
                     if (count($results) > 0) {
                         foreach ($results as $guid => $array) {
                             $this->_request_data['is_linked_from'] = $guid;
                         }
                     }
                 }
             }
         }
     } else {
         if (preg_match('/^view_product_intree/', $handler_id)) {
             $this->_product = new org_openpsa_products_product_dba($args[1]);
         } else {
             $this->_product = new org_openpsa_products_product_dba($args[0]);
         }
     }
     if ($GLOBALS['midcom_config']['enable_ajax_editing']) {
         $data['controller'] = midcom_helper_datamanager2_controller::create('ajax');
         $data['controller']->schemadb =& $data['schemadb_product'];
         $data['controller']->set_storage($this->_product);
         $data['controller']->process_ajax();
         $data['datamanager'] =& $data['controller']->datamanager;
     } else {
         $data['controller'] = null;
         $data['datamanager'] = new midcom_helper_datamanager2_datamanager($data['schemadb_product']);
         if (!$data['datamanager']->autoset_storage($this->_product)) {
             throw new midcom_error("Failed to create a DM2 instance for product {$this->_product->guid}.");
         }
     }
     $this->_prepare_request_data();
     $this->bind_view_to_object($this->_product, $data['datamanager']->schema->name);
     if (isset($product_group)) {
         unset($product_group);
     }
     $product_group = null;
     if ($this->_request_data['is_linked_from'] != '') {
         $linked_product = new org_openpsa_products_product_link_dba($data['is_linked_from']);
         if ($linked_product->productGroup != 0) {
             $product_group = new org_openpsa_products_product_group_dba($linked_product->productGroup);
         }
     }
     $breadcrumb = org_openpsa_products_viewer::update_breadcrumb_line($this->_product, $product_group);
     midcom_core_context::get()->set_custom_key('midcom.helper.nav.breadcrumb', $breadcrumb);
     midcom::get('metadata')->set_request_metadata($this->_product->metadata->revised, $this->_product->guid);
     $title = $this->_config->get('product_page_title');
     if (strstr($title, '<PRODUCTGROUP')) {
         try {
             $productgroup = new org_openpsa_products_product_group_dba($this->_product->productGroup);
             $title = str_replace('<PRODUCTGROUP_TITLE>', $productgroup->title, $title);
             $title = str_replace('<PRODUCTGROUP_CODE>', $productgroup->code, $title);
         } catch (midcom_error $e) {
             $title = str_replace('<PRODUCTGROUP_TITLE>', '', $title);
             $title = str_replace('<PRODUCTGROUP_CODE>', '', $title);
         }
     }
     $title = str_replace('<PRODUCT_CODE>', $this->_product->code, $title);
     $title = str_replace('<PRODUCT_TITLE>', $this->_product->title, $title);
     $title = str_replace('<TOPIC_TITLE>', $this->_topic->extra, $title);
     midcom::get('head')->set_pagetitle($title);
 }