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); }
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; } }
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; }
/** * 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]; }
/** * 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; }
/** * 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); }
/** * 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); } }
/** * 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); }
/** * 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; }
/** * 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; }
/** * 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; }