public function get_read(array $args)
 {
     parent::get_read($args);
     $this->data['groups'] = array();
     $this->data['is_challenger'] = $this->is_challenger();
     if ($this->data['is_challenger']) {
         $this->data['groups_url'] = midgardmvc_core::get_instance()->dispatcher->generate_url('index', array(), 'fi_openkeidas_groups');
         $this->data['update_url'] = $this->get_url_update();
         $this->data['delete_url'] = midgardmvc_core::get_instance()->dispatcher->generate_url('challenge_delete', array('challenge' => $this->object->guid), $this->request);
     }
     $activityobject = null;
     if ($this->object->activity) {
         $activityobject = fi_openkeidas_diary_activities::get($this->object->activity);
     }
     $this->object->activityobject = $activityobject;
     $this->data['challenger'] = new fi_openkeidas_groups_group($this->object->challenger);
     $mc = new midgard_collector('fi_openkeidas_diary_challenge_participant', 'challenge', $this->object->id);
     //$mc->add_constraint('metadata.isapproved', '=', true);
     $mc->set_key_property('grp');
     $mc->execute();
     $grp_ids = array_keys($mc->list_keys());
     if (count($grp_ids) == 0) {
         return;
     }
     $qb = new midgard_query_builder('fi_openkeidas_groups_group');
     $qb->add_constraint('id', 'IN', $grp_ids);
     $qb->add_order('metadata.score', 'DESC');
     $this->data['groups'] = array_map(function ($group) use($activityobject) {
         $group->url = midgardmvc_core::get_instance()->dispatcher->generate_url('group_read', array('group' => $group->guid), 'fi_openkeidas_groups');
         $group->activity = fi_openkeidas_diary_logs::group_duration_this_week($group, $activityobject);
         return $group;
     }, $qb->execute());
 }
 public function get_read(array $args)
 {
     parent::get_read($args);
     $this->data['admins'] = array();
     $this->data['is_admin'] = false;
     $qb = new midgard_query_builder('fi_openkeidas_groups_group_member');
     $qb->add_constraint('grp', '=', $this->object->id);
     $qb->add_constraint('admin', '=', true);
     $qb->add_constraint('metadata.isapproved', '=', true);
     $admins = $qb->execute();
     foreach ($admins as $admin) {
         $this->data['admins'][] = new midgard_person($admin->person);
         if ($admin->person == midgardmvc_core::get_instance()->authentication->get_person()->id) {
             $this->data['is_admin'] = true;
         }
     }
     if ($this->data['is_admin']) {
         $this->data['members'] = array();
         $qb = new midgard_query_builder('fi_openkeidas_groups_group_member');
         $qb->add_constraint('grp', '=', $this->object->id);
         $qb->add_constraint('admin', '=', false);
         $members = $qb->execute();
         foreach ($members as $member) {
             $this->data['members'][] = new midgard_person($member->person);
         }
         $this->data['approve_url'] = midgardmvc_core::get_instance()->dispatcher->generate_url('group_join_approve', array('group' => $this->object->guid), $this->request);
     }
     $this->data['is_member'] = $this->is_member();
     $this->data['join_url'] = midgardmvc_core::get_instance()->dispatcher->generate_url('group_join', array('group' => $this->object->guid), $this->request);
     if (!$this->object->autocreated) {
         $this->data['leave_url'] = midgardmvc_core::get_instance()->dispatcher->generate_url('group_leave', array('group' => $this->object->guid), $this->request);
     }
     $this->data['challenges'] = array();
     $mc = new midgard_collector('fi_openkeidas_groups_group_member', 'person', midgardmvc_core::get_instance()->authentication->get_person()->id);
     $mc->add_constraint('metadata.isapproved', '=', true);
     $mc->set_key_property('grp');
     $mc->execute();
     $grp_ids = array_keys($mc->list_keys());
     if (count($grp_ids) == 0) {
         return;
     }
     $qb = new midgard_query_builder('fi_openkeidas_diary_challenge');
     $qb->add_constraint('challenger', 'IN', $grp_ids);
     $qb->add_constraint('challenger', '<>', $this->object->id);
     $qb->add_constraint('enddate', '>', new DateTime());
     $challenges = $qb->execute();
     foreach ($challenges as $challenge) {
         $qb = new midgard_query_builder('fi_openkeidas_diary_challenge_participant');
         $qb->add_constraint('challenge', '=', $challenge->id);
         $qb->add_constraint('grp', '=', $this->object->id);
         if ($qb->count() > 0) {
             continue;
         }
         $challenge->url = midgardmvc_core::get_instance()->dispatcher->generate_url('challenge_challenge', array('challenge' => $challenge->guid, 'group' => $this->object->guid), 'fi_openkeidas_diary');
         $this->data['challenges'][] = $challenge;
     }
 }
Exemple #3
0
 public static function group_duration_within(DateTime $from, DateTime $to, fi_openkeidas_groups_group $group, fi_openkeidas_diary_activity $activity = null)
 {
     midgardmvc_core::get_instance()->authorization->require_user();
     $mc = new midgard_collector('fi_openkeidas_groups_group_member', 'grp', $group->id);
     $mc->add_constraint('metadata.isapproved', '=', true);
     $mc->set_key_property('person');
     $mc->execute();
     return self::average_duration_within($from, $to, array_keys($mc->list_keys()), $activity);
 }
Exemple #4
0
 /**
  * Returns the Parent of the Attachment, which is identified by the table/id combination
  * in the attachment record. The table in question is used to identify the object to
  * use. If multiple objects are registered for a given table, the first matching class
  * returned by the dbfactory is used (which is usually rather arbitrary).
  *
  * @return MidgardObject Parent object.
  */
 public static function get_parent_guid_uncached_static($guid, $classname = __CLASS_)
 {
     $mc = new midgard_collector('midgard_attachment', 'guid', $guid);
     $mc->set_key_property('parentguid');
     $mc->execute();
     $link_values = $mc->list_keys();
     if (empty($link_values)) {
         return null;
     }
     return key($link_values);
 }
Exemple #5
0
 /**
  * Returns the object GUID the location is stored for
  *
  * @return string Parent GUID or null if there is none
  */
 public static function get_parent_guid_uncached_static($guid, $classname = __CLASS_)
 {
     $mc = new midgard_collector('org_routamc_positioning_location', 'guid', $guid);
     $mc->set_key_property('parent');
     $mc->execute();
     $link_values = $mc->list_keys();
     if (!$link_values) {
         return null;
     }
     foreach ($link_values as $key => $value) {
         return $key;
     }
 }
 public static function add_challenges_for_group(fi_openkeidas_groups_group $group)
 {
     $mc = new midgard_collector('fi_openkeidas_diary_challenge_participant', 'grp', $group->id);
     //$mc->add_constraint('metadata.isapproved', '=', true);
     $mc->add_constraint('challenge.start', '<=', new DateTime());
     $mc->add_constraint('challenge.enddate', '>', new DateTime());
     $mc->set_key_property('challenge');
     $mc->execute();
     $challenge_ids = array_keys($mc->list_keys());
     if (count($challenge_ids) == 0) {
         return $group;
     }
     $qb = new midgard_query_builder('fi_openkeidas_diary_challenge');
     $qb->add_constraint('id', 'IN', $challenge_ids);
     $group->challenges = array();
     $challenges = $qb->execute();
     foreach ($challenges as $challenge) {
         $challenge->url = midgardmvc_core::get_instance()->dispatcher->generate_url('challenge_read', array('challenge' => $challenge->guid), 'fi_openkeidas_diary');
         $group->challenges[] = $challenge;
     }
     return $group;
 }
Exemple #7
0
 /**
  * Helper function that sets the request data for hour reports
  *
  * @param &$array The array returned by collector
  */
 private function _add_hour_data(&$array)
 {
     static $customer_cache = array();
     if (!isset($customer_cache[$array['task']])) {
         $customer = 0;
         $customer_label = $this->_l10n->get('no customer');
         if ($array['task'] != 0) {
             $mc = new midgard_collector('org_openpsa_task', 'id', $array['task']);
             $mc->set_key_property('id');
             $mc->add_value_property('customer');
             $mc->execute();
             $customer_id = $mc->get_subkey($array['task'], 'customer');
             if ($customer_id) {
                 try {
                     $customer = new org_openpsa_contacts_group_dba($customer_id);
                     $customer_label = $customer->official;
                     $customer = $customer_id;
                 } catch (midcom_error $e) {
                 }
             }
         }
         $customer_cache[$array['task']] = $customer;
         if (!isset($this->_request_data['customers'][$customer])) {
             $this->_request_data['customers'][$customer] = $customer_label;
         }
     }
     $customer = $customer_cache[$array['task']];
     $category = 'uninvoiceable';
     if ($array['invoiceable']) {
         $category = 'invoiceable';
     }
     if (!isset($this->_request_data['hours'][$category][$customer])) {
         $this->_request_data['hours'][$category][$customer] = $array['hours'];
     } else {
         $this->_request_data['hours'][$category][$customer] += $array['hours'];
     }
     $this->_request_data['hours']['total_' . $category] += $array['hours'];
 }
Exemple #8
0
 /**
  * This is an internal helper function, which may only be called statically.
  *
  * It is used by get_all_privileges in case that there is no cache hit. It will query the
  * database and construct all necessary objects out of it.
  *
  * @param string $guid The GUID of the object for which to query ACL data.
  * @param string $type SELF or CONTENT
  * @return Array A list of midcom_core_privilege instances.
  */
 protected static function _query_privileges($guid, $type)
 {
     $result = array();
     $mc = new midgard_collector('midcom_core_privilege_db', 'objectguid', $guid);
     $mc->add_constraint('value', '<>', MIDCOM_PRIVILEGE_INHERIT);
     if ($type == 'CONTENT') {
         $mc->add_constraint('assignee', '<>', 'SELF');
     } else {
         $mc->add_constraint('assignee', '=', 'SELF');
     }
     $mc->set_key_property('guid');
     $mc->add_value_property('id');
     $mc->add_value_property('privilegename');
     $mc->add_value_property('assignee');
     $mc->add_value_property('classname');
     $mc->add_value_property('value');
     midcom_connection::set_error(MGD_ERR_OK);
     $mc->execute();
     $privileges = $mc->list_keys();
     if (!$privileges) {
         if (midcom_connection::get_error() != MGD_ERR_OK) {
             debug_add("Failed to retrieve all {$type} privileges for the Object GUID {$guid}: " . midcom_connection::get_error_string(), MIDCOM_LOG_INFO);
             debug_print_r('Result was:', $result);
             if (isset($php_errormsg)) {
                 debug_add("Error message was: {$php_errormsg}", MIDCOM_LOG_ERROR);
             }
             throw new midcom_error('Privilege collector failed to execute: ' . midcom_connection::get_error_string());
         }
         return $result;
     }
     foreach ($privileges as $privilege_guid => $value) {
         $privilege = $mc->get($privilege_guid);
         $privilege['objectguid'] = $guid;
         $privilege['guid'] = $privilege_guid;
         $privilege_object = new midcom_core_privilege($privilege);
         if (!isset($privilege_object->assignee)) {
             // Invalid privilege, skip
             continue;
         }
         $privilege_object->scope = $privilege_object->_get_scope();
         $return[] = $privilege_object;
     }
     return $return;
 }
Exemple #9
0
 /**
  * Adds a passed rule for the passed class to the querybuilder
  *
  * @param array $rule contains the rule
  * @param string $class name of the class the rule will be added to
  * @param string $person_property contains the name of the property of the
  * passed class which links to the person
  */
 function add_misc_rule(array $rule, $class, $person_property)
 {
     $persons = array(0 => -1);
     $match = $rule['match'];
     $constraint_match = "IN";
     if ($rule['match'] == '<>' || $rule['match'] == 'NOT LIKE') {
         $constraint_match = "NOT IN";
         switch ($rule['match']) {
             case '<>':
                 $match = '=';
                 break;
             case 'NOT LIKE':
                 $match = 'LIKE';
                 break;
             default:
                 $match = '=';
                 break;
         }
     }
     $mc_misc = new midgard_collector($class, 'metadata.deleted', false);
     $mc_misc->set_key_property('id');
     $mc_misc->add_constraint($rule['property'], $match, $rule['value']);
     $mc_misc->add_value_property($person_property);
     $mc_misc->execute();
     $keys = $mc_misc->list_keys();
     foreach ($keys as $key => $value) {
         // get user-id
         $persons[] = $mc_misc->get_subkey($key, $person_property);
     }
     $this->_result_mc->add_constraint('id', $constraint_match, $persons);
 }
Exemple #10
0
 /**
  * Generate a valid cache identifier for a context of the current request
  */
 function generate_request_identifier($context, $customdata = null)
 {
     $module_name = $GLOBALS['midcom_config']['cache_module_content_name'];
     if ($module_name == 'auto') {
         $module_name = midcom_connection::get_unique_host_name();
     }
     $identifier_source = 'CACHE:' . $module_name;
     // Cache the request identifier so that it doesn't change between start and end of request
     static $identifier_cache = array();
     if (isset($identifier_cache[$context])) {
         // FIXME: Use customdata here too
         return $identifier_cache[$context];
     }
     if (!isset($customdata['cache_module_content_caching_strategy'])) {
         $cache_strategy = $GLOBALS['midcom_config']['cache_module_content_caching_strategy'];
     } else {
         $cache_strategy = $customdata['cache_module_content_caching_strategy'];
     }
     switch ($cache_strategy) {
         case 'memberships':
             if (!midcom_connection::get_user()) {
                 $identifier_source .= ';USER=ANONYMOUS';
                 break;
             }
             $mc = new midgard_collector('midgard_member', 'uid', midcom_connection::get_user());
             $mc->set_key_property('gid');
             $mc->execute();
             $gids = $mc->list_keys();
             unset($mc);
             $identifier_source .= ';GROUPS=' . implode(',', array_keys($gids));
             unset($gids);
             break;
         case 'public':
             $identifier_source .= ';USER=EVERYONE';
             break;
         case 'user':
         default:
             $identifier_source .= ';USER='******';URL=' . midcom_core_context::get()->get_key(MIDCOM_CONTEXT_URI);
     } else {
         $identifier_source .= ';URL=' . $_SERVER['REQUEST_URI'];
     }
     // check_dl_hit needs to take config changes into account...
     if (!is_null($customdata)) {
         $identifier_source .= ';' . serialize($customdata);
     }
     // TODO: Add browser capability data (mobile, desktop browser etc) from WURFL here
     debug_add("Generating context {$context} request-identifier from: {$identifier_source}");
     debug_print_r('$customdata was: ', $customdata);
     $identifier_cache[$context] = 'R-' . md5($identifier_source);
     return $identifier_cache[$context];
 }
Exemple #11
0
 /**
  * Modify a query instance for searching by username, with differences between
  * mgd1 and mgd2 abstracted away
  *
  * @param midcom_core_query &$query The QB or MC instance to work on
  * @param string $operator The operator for the username constraint
  * @param string $value The value for the username constraint
  */
 public static function add_username_constraint(midcom_core_query &$query, $operator, $value)
 {
     if (method_exists('midgard_user', 'login')) {
         $mc = new midgard_collector('midgard_user', 'authtype', $GLOBALS['midcom_config']['auth_type']);
         $mc->set_key_property('person');
         $mc->add_constraint('login', $operator, $value);
         $mc->execute();
         $user_results = $mc->list_keys();
         if (count($user_results) < 1) {
             // make sure we don't return any results if no midgard_user entry was found
             $query->add_constraint('id', '=', 0);
         } else {
             $query->add_constraint('guid', 'IN', array_keys($user_results));
         }
     } else {
         $query->add_constraint('username', $operator, $value);
     }
 }
Exemple #12
0
 /**
  * Retrieves a given metadata key, postprocesses it where necessary
  * and stores it into the local cache.
  *
  * - Person references (both guid and id) get resolved into the corresponding
  *   Person object.
  * - created, creator, edited and editor are taken from the corresponding
  *   MidgardObject fields.
  * - Parameters are accessed using their midgard-created member variables
  *   instead of accessing the database using $object->parameter directly for
  *   performance reasons (this will implicitly use the NAP cache for these
  *   values as well. (Implementation note: Variable variables have to be
  *   used for this, as we have dots in the member name.)
  *
  * Note, that we hide any errors from not existent properties explicitly,
  * as a few of the MidCOM objects do not support all of the predefined meta
  * data fields, PHP will default to "0" in these cases. For Person IDs, this
  * "0" is rewritten to "1" to use the MidgardAdministrator account instead.
  *
  * @param string $key The key to retrieve.
  */
 private function _retrieve_value($key)
 {
     switch ($key) {
         // Time-based properties
         case 'created':
         case 'revised':
         case 'published':
         case 'locked':
         case 'approved':
         case 'schedulestart':
         case 'scheduleend':
         case 'exported':
         case 'imported':
             if (extension_loaded('midgard2') && isset($this->__metadata->{$key})) {
                 //This is ugly, but seems the only possible way...
                 if ((string) $this->__metadata->{$key} === "0001-01-01T00:00:00+00:00") {
                     $value = 0;
                 } else {
                     $value = (int) $this->__metadata->{$key}->format('U');
                 }
             } else {
                 if (empty($this->__metadata->{$key}) || $this->__metadata->{$key} == '0000-00-00 00:00:00') {
                     $value = 0;
                 } else {
                     $value = strtotime("{$this->__metadata->{$key}} UTC");
                 }
             }
             break;
             // Person properties
         // Person properties
         case 'creator':
         case 'revisor':
         case 'locker':
         case 'approver':
             $value = $this->__metadata->{$key};
             if (!$value) {
                 // Fall back to "Midgard root user" if person is not found
                 static $root_user_guid = null;
                 if (!$root_user_guid) {
                     $mc = new midgard_collector('midgard_person', 'id', 1);
                     $mc->set_key_property('guid');
                     $mc->execute();
                     $guids = $mc->list_keys();
                     if (empty($guids)) {
                         $root_user_guid = 'f6b665f1984503790ed91f39b11b5392';
                     } else {
                         foreach ($guids as $guid => $val) {
                             $root_user_guid = $guid;
                         }
                     }
                 }
                 $value = $root_user_guid;
             }
             break;
             // Other midgard_metadata properties
         // Other midgard_metadata properties
         case 'revision':
         case 'hidden':
         case 'navnoentry':
         case 'size':
         case 'deleted':
         case 'score':
         case 'authors':
         case 'owner':
             $value = $this->__metadata->{$key};
             break;
             // Fall-back for non-core properties
         // Fall-back for non-core properties
         default:
             $dm = $this->get_datamanager();
             if (!isset($dm->types[$key])) {
                 // Fall back to the parameter reader for non-core MidCOM metadata params
                 $value = $this->__object->get_parameter('midcom.helper.metadata', $key);
             } else {
                 $value = $dm->types[$key]->convert_to_csv();
             }
             break;
     }
     $this->_cache[$key] = $value;
 }
Exemple #13
0
 /**
  * Helper function to create & recalculate existing invoice_items by tasks
  *
  * @param array $tasks array containing the task id's to recalculate for - if empty all tasks will be recalculated
  */
 public function _recalculate_invoice_items($tasks = array(), $skip_invoice_update = false)
 {
     $result_items = array();
     $result_tasks = array();
     //get hour_reports for this invoice - mc ?
     $qb_hour_reports = org_openpsa_projects_hour_report_dba::new_query_builder();
     $qb_hour_reports->add_constraint('invoice', '=', $this->id);
     if (!empty($tasks)) {
         $qb_hour_reports->add_constraint('task', 'IN', $tasks);
         //if there is a task passed it must be calculated even
         //if it doesn't have associated hour_reports
         foreach ($tasks as $task_id) {
             $result_tasks[$task_id] = 0;
         }
     }
     $hour_reports = $qb_hour_reports->execute();
     // sums up the hours of hour_reports for each task
     foreach ($hour_reports as $hour_report) {
         if (!array_key_exists($hour_report->task, $result_tasks)) {
             $result_tasks[$hour_report->task] = 0;
         }
         //only add invoiceable hour_reports
         if ($hour_report->invoiceable) {
             $result_tasks[$hour_report->task] += $hour_report->hours;
         }
     }
     foreach ($result_tasks as $task_id => $hours) {
         $invoice_item = $this->_probe_invoice_item_for_task($task_id);
         //get deliverable for this task
         $mc_task_agreement = new midgard_collector('org_openpsa_task', 'id', $task_id);
         $mc_task_agreement->set_key_property('id');
         $mc_task_agreement->add_value_property('title');
         $mc_task_agreement->add_value_property('agreement');
         $mc_task_agreement->add_constraint('agreement', '<>', 0);
         $mc_task_agreement->execute();
         $mc_task_key = $mc_task_agreement->list_keys();
         $deliverable = null;
         foreach ($mc_task_key as $key => $empty) {
             try {
                 $deliverable = new org_openpsa_sales_salesproject_deliverable_dba((int) $mc_task_agreement->get_subkey($key, 'agreement'));
                 $invoice_item->pricePerUnit = $deliverable->pricePerUnit;
                 $invoice_item->deliverable = $deliverable->id;
                 //calculate price
                 if ($deliverable->invoiceByActualUnits || $deliverable->plannedUnits == 0) {
                     $invoice_item->units = $hours;
                 } else {
                     $invoice_item->units = $deliverable->plannedUnits;
                 }
             } catch (midcom_error $e) {
                 $e->log();
                 $invoice_item->units = $hours;
             }
         }
         if ($invoice_item->description == '') {
             $invoice_item->description = $mc_task_agreement->get_subkey($task_id, 'title');
         }
         $invoice_item->skip_invoice_update = $skip_invoice_update;
         $invoice_item->update();
         $result_items[] = $invoice_item;
     }
     return $result_items;
 }
Exemple #14
0
 /**
  * Get the GUID of the object's parent. This is done by reading up or parent
  * property values, which will give us the parent's ID. Since the ID => GUID relation
  * won't change, the corresponding GUID is then stored in an in-request static cache
  */
 public static function get_parent_guid_uncached_static($object_guid, $class_name)
 {
     static $parent_mapping = array();
     $class_name = midcom::get('dbclassloader')->get_mgdschema_class_name_for_midcom_class($class_name);
     $reflector = new midgard_reflection_property($class_name);
     $up_property = midgard_object_class::get_property_up($class_name);
     if (!empty($up_property)) {
         $target_property = $reflector->get_link_target($up_property);
         // Up takes precedence over parent
         $mc = new midgard_collector($class_name, 'guid', $object_guid);
         $mc->set_key_property($up_property);
         $mc->execute();
         $link_values = $mc->list_keys();
         if (!empty($link_values)) {
             list($link_value, $dummy) = each($link_values);
             unset($mc, $link_values, $dummy);
             if (!empty($link_value)) {
                 if (!array_key_exists($class_name, $parent_mapping)) {
                     $parent_mapping[$class_name] = array();
                 }
                 if (array_key_exists($link_value, $parent_mapping[$class_name])) {
                     return $parent_mapping[$class_name][$link_value];
                 }
                 $mc2 = new midgard_collector($class_name, $target_property, $link_value);
                 $mc2->set_key_property('guid');
                 $mc2->execute();
                 $guids = $mc2->list_keys();
                 if (!is_array($guids)) {
                     unset($mc2, $guids, $link_value);
                     $parent_mapping[$class_name][$link_value] = null;
                     return $parent_mapping[$class_name][$link_value];
                 }
                 list($parent_guid, $dummy) = each($guids);
                 $parent_mapping[$class_name][$link_value] = $parent_guid;
                 unset($mc2, $guids, $link_value, $dummy);
                 return $parent_guid;
             }
         } else {
             unset($mc, $link_values);
         }
     }
     $parent_property = midgard_object_class::get_property_parent($class_name);
     if (!empty($parent_property) && $reflector->get_link_target($parent_property)) {
         $target_property = $reflector->get_link_target($parent_property);
         $target_class = $reflector->get_link_name($parent_property);
         $mc = new midgard_collector($class_name, 'guid', $object_guid);
         $mc->set_key_property($parent_property);
         $mc->execute();
         $link_values = $mc->list_keys();
         if (!empty($link_values)) {
             list($link_value, $dummy) = each($link_values);
             unset($mc, $link_values, $dummy);
             if (!empty($link_value)) {
                 if (!array_key_exists($target_class, $parent_mapping)) {
                     $parent_mapping[$target_class] = array();
                 }
                 if (array_key_exists($link_value, $parent_mapping[$target_class])) {
                     return $parent_mapping[$target_class][$link_value];
                 }
                 $mc2 = new midgard_collector($target_class, $target_property, $link_value);
                 $mc2->set_key_property('guid');
                 $mc2->execute();
                 $guids = $mc2->list_keys();
                 if (!is_array($guids)) {
                     unset($mc2, $guids, $link_value);
                     $parent_mapping[$target_class][$link_value] = null;
                     return $parent_mapping[$target_class][$link_value];
                 }
                 list($parent_guid, $dummy) = each($guids);
                 $parent_mapping[$target_class][$link_value] = $parent_guid;
                 unset($mc2, $guids, $link_value, $dummy);
                 return $parent_guid;
             }
         } else {
             unset($mc, $link_values);
         }
     }
     // FIXME: Handle GUID linking
     return null;
 }
Exemple #15
0
 /**
  * This method returns a list of all groups in which the
  * MidCOM user passed is a member.
  *
  * This function is always called statically.
  *
  * @param midcom_core_user $user The user that should be looked-up.
  * @return Array An array of member groups or false on failure, indexed by their ID.
  */
 public static function list_memberships($user)
 {
     $mc = new midgard_collector('midgard_member', 'uid.guid', $user->guid);
     $mc->add_constraint('gid', '<>', 0);
     $mc->set_key_property('gid');
     @$mc->execute();
     $result = $mc->list_keys();
     if (empty($result)) {
         return $result;
     }
     $return = array();
     foreach ($result as $gid => $empty) {
         try {
             $group = new midcom_core_group($gid);
         } catch (Exception $e) {
             debug_add("The group {$gid} is unknown, skipping the membership record.", MIDCOM_LOG_ERROR);
             debug_add('Last Midgard error was: ' . midcom_connection::get_error_string());
             continue;
         }
         $return[$group->id] = $group;
     }
     return $return;
 }
Exemple #16
0
 /**
  * This is a wrapper for get_user, which allows user retrieval by its email address.
  * If the email is empty or unknown, false is returned.
  *
  * @param string $email The email of the user to look up.
  * @return array|midcom_core_user A reference to the user object matching the email, array if multiple matches
  *     or false if the email is unknown.
  */
 function get_user_by_email($email)
 {
     static $persons_by_email = array();
     if (empty($email)) {
         return false;
     }
     if (array_key_exists($email, $persons_by_email)) {
         return $persons_by_email[$email];
     }
     // Seek user based on the primary email field
     $qb = new midgard_query_builder($GLOBALS['midcom_config']['person_class']);
     $qb->add_constraint('email', '=', $email);
     // FIXME: Some sites like maemo.org instead of deleting users just remove their account and prefix firstname by "DELETE "
     $qb->add_constraint('firstname', 'NOT LIKE', 'DELETE %');
     $results = @$qb->execute();
     if (!$results || count($results) == 0) {
         // Try finding user based on the other email fields
         $person_guids = array();
         $mc = new midgard_collector('midgard_parameter', 'value', $email);
         $mc->set_key_property('parentguid');
         $mc->add_constraint('domain', '=', 'org.imc.vcard:email');
         $mc->execute();
         $guids = $mc->list_keys();
         foreach ($guids as $guid => $array) {
             $person_guids[] = $guid;
         }
         if (empty($person_guids)) {
             return false;
         }
         $qb = new midgard_query_builder($GLOBALS['midcom_config']['person_class']);
         $qb->add_constraint('guid', 'IN', $person_guids);
         // FIXME: Some sites like maemo.org instead of deleting users just remove their account and prefix firstname by "DELETE "
         $qb->add_constraint('firstname', 'NOT LIKE', 'DELETE %');
         $results = @$qb->execute();
         if (empty($results)) {
             $persons_by_email[$email] = false;
             return false;
         }
     }
     if (count($results) > 1) {
         $persons_by_email[$email] = array();
         foreach ($results as $result) {
             $persons_by_email[$email][] = $this->get_user($result);
         }
         return $persons_by_email[$email];
     }
     $persons_by_email[$email] = $this->get_user($results[0]);
     return $persons_by_email[$email];
 }
Exemple #17
0
 /**
  * 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);
 }
Exemple #18
0
 /**
  * Small helper to get the GUID of the user's first group. This is used mainly to populate
  * the owner field during DBa object create calls
  *
  * @return mixed GUID of the first group found or false
  */
 function get_first_group_guid()
 {
     if (!is_null($this->_direct_groups)) {
         if (empty($this->_direct_groups)) {
             // User is not member of any groups
             return false;
         }
         return $this->_direct_groups[key($this->_direct_groups)]->get_storage()->guid;
     }
     //Not yet initialized, try to load one midgard group
     $mc = new midgard_collector('midgard_member', 'uid', $this->_storage->id);
     $mc->add_constraint('gid', '<>', 0);
     $mc->set_key_property('gid');
     $mc->set_limit(1);
     @$mc->execute();
     $result = $mc->list_keys();
     if (!empty($result)) {
         if ($group = midcom::get('auth')->get_group(key($result))) {
             return $group->get_storage()->guid;
         }
     }
     $this->_load_all_groups();
     if (!empty($this->_direct_groups)) {
         return $this->_direct_groups[key($this->_direct_groups)]->get_storage()->guid;
     }
     return false;
 }