public function get_active(array $args) { $qb = new midgard_query_builder('fi_openkeidas_groups_group'); $qb->set_limit(5); $qb->add_order('metadata.score', 'DESC'); $this->data['groups'] = array_map('fi_openkeidas_groups_controllers_groups::add_members_for_group', $qb->execute()); }
public function get_top(array $args) { $qb = new midgard_query_builder('fi_openkeidas_diary_activity'); $qb->set_limit(5); $qb->add_order('metadata.score', 'DESC'); $this->data['sports'] = $qb->execute(); }
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()); }
/** * Find hanging duplicate marks (that no longer point anywhere) and clear them */ public function _on_execute() { debug_add('_on_execute called'); if (!$this->_config->get('enable_duplicate_search')) { debug_add('Duplicate operations disabled, aborting', MIDCOM_LOG_INFO); return; } // Untill the FIXME below is handled we abort debug_add('Duplicate cleanup disabled since it needs code cleanup for 1.8 Midgfard, aborting', MIDCOM_LOG_ERROR); return; ignore_user_abort(); $qb = new midgard_query_builder('midgard_parameter'); $qb->add_constraint('domain', '=', 'org.openpsa.contacts.duplicates:possible_duplicate'); $qb->add_order('name', 'ASC'); $results = @$qb->execute(); foreach ($results as $param) { try { $obj = midcom::get('dbfactory')->get_object_by_guid($param->name); } catch (midcom_error $e) { debug_add("GUID {$param->name} points to nonexistent person, removing possible duplicate mark", MIDCOM_LOG_INFO); if (!$param->delete()) { debug_add("Failed to delete parameter {$param->guid}, errstr: " . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR); } } } debug_add('Done'); return; }
public function get_graph(array $args) { midgardmvc_core::get_instance()->authorization->require_user(); $stat_types = array('bmi', 'weight', 'vo2max'); $qb = new midgard_query_builder('fi_openkeidas_diary_stat'); $qb->add_constraint('person', '=', midgardmvc_core::get_instance()->authentication->get_person()->id); $qb->add_constraint('stat', 'IN', $stat_types); $qb->add_order('date', 'ASC'); $since = new midgard_datetime('6 months ago'); $qb->add_constraint('date', '>', $since); $stats = $qb->execute(); if (empty($stats)) { throw new midgardmvc_exception_notfound("No stats found"); } $dates = array(); foreach ($stats as $stat) { $date = $stat->date->format('d.m.Y'); if (!isset($dates[$date])) { $dates[$date] = array(); } $dates[$date][] = $stat; } $previous_value = array(); $this->data['stats'] = array(); foreach ($dates as $date => $stats) { foreach ($stats as $stat) { if (!isset($this->data['stats'][$stat->stat])) { $this->data['stats'][$stat->stat] = array(); } $value = round($stat->value, 1); $this->data['stats'][$stat->stat][$date] = $value; $previous_value[$stat->stat] = $value; } foreach ($stat_types as $stat_type) { if (!isset($this->data['stats'][$stat_type])) { continue; } if (!isset($this->data['stats'][$stat_type][$date]) && isset($previous_value[$stat_type])) { $this->data['stats'][$stat_type][$date] = $previous_value[$stat_type]; } } } midgardmvc_core::get_instance()->component->load_library('Graph'); $graph = new ezcGraphLineChart(); foreach ($this->data['stats'] as $name => $stats) { $graph->data[$this->get_label($name)] = new ezcGraphArrayDataSet($stats); $graph->data[$this->get_label($name)]->symbol = ezcGraph::BULLET; } $graph->driver = new fi_openkeidas_diary_graph_gd(); $graph->driver->options->imageFormat = IMG_PNG; $graph->options->font = midgardmvc_core::get_instance()->configuration->graph_font; $graph->legend->position = ezcGraph::BOTTOM; $graph->palette = new ezcGraphPaletteEz(); // render image directly to screen $graph->renderToOutput(575, 200); // wrap up the request midgardmvc_core::get_instance()->dispatcher->end_request(); }
private function prepare_qb(midgardmvc_core_node $node, $limit, $offset = 0) { $qb = new midgard_query_builder('fi_openkeidas_articles_article'); $qb->add_constraint('node', 'INTREE', $node->id); if (!midgardmvc_ui_create_injector::can_use()) { // Regular user, hide unapproved articles // TODO: This check should be moved to authentication service when QB has signals $qb->add_constraint('metadata.isapproved', '=', true); } $qb->add_order('metadata.created', 'DESC'); $qb->set_limit($limit); $qb->set_offset($offset); return $qb; }
/** * Get document list for requested sequences */ public function get_docs_by_seq(array $args) { //$this->authenticate(); // List the requested sequence data $qb = new midgard_query_builder('midgard_sequence'); if (isset($_MIDCOM->dispatcher->get['startkey'])) { $qb->add_constraint('id', '>', (int) $_MIDCOM->dispatcher->get['startkey']); } if (isset($_MIDCOM->dispatcher->get['limit'])) { $qb->set_limit((int) $_MIDCOM->dispatcher->get['limit']); } $qb->add_order('id', 'ASC'); $transactions = $qb->execute(); // Prepare the data list $this->data['total_rows'] = count($transactions); $this->data['offset'] = 0; $this->data['rows'] = array(); // Map the transaction data to what CouchDb protocol wants foreach ($transactions as $transaction) { $this->data['rows'][] = array('id' => $transaction->objectguid, 'key' => $transaction->id, 'value' => array('rev' => $transaction->revision . '-' . $transaction->metadata->revised->format('U'))); } }
/** * Checks if there are any objects (for the mode given in constructor) that need to be processed * * Note: does not check user's privileges or that the objects actually exist (the cleanup cronjob * handles dangling references) * @return boolean indicating need for processing (merge/not duplicate) */ function merge_needed() { switch ($this->_object_mode) { case 'person': break; case 'group': break; default: // object mode not set properly $this->_errstr = 'invalid object mode'; return false; break; } $qb = new midgard_query_builder('midgard_parameter'); $qb->add_constraint('domain', '=', 'org.openpsa.contacts.duplicates:possible_duplicate'); $qb->add_order('name', 'ASC'); $qb->set_limit(1); $results = @$qb->execute(); if (!empty($results)) { return true; } return false; }
public static function get_location_for_person(midgard_person $person, midgard_datetime $when = null) { $qb = new midgard_query_builder('midgardmvc_helper_location_log'); $qb->add_constraint('person', '=', $person->id); if (!is_null($when)) { $qb->add_constraint('metadata.published', '<=', $when); } $qb->set_limit(1); $qb->add_order('metadata.published', 'DESC'); $logs = $qb->execute(); foreach ($logs as $log) { $spot = new midgardmvc_helper_location_spot($log); $spot->source = $log->importer; $spot->text = $log->text; return $spot; } return null; }
private function _get_photolinks() { $qb = new midgard_query_builder('org_routamc_gallery_photolink'); $qb->add_constraint('node', '=', $this->_node->id); $orders = (array) $this->_node->get_parameter('org.routamc.gallery', 'nav_order'); if (sizeof($orders) == 0) { $orders = array('metadata.score reverse', 'photo.taken reverse'); } foreach ($orders as $order) { // Probably an empty string if (!preg_match('/^([^\\s]+)\\s*(.*)/', $order, $regs)) { continue; } if (preg_match('/(rev|desc)/i', $regs[2])) { $qb->add_order($regs[1], 'DESC'); } else { $qb->add_order($regs[1]); } } return $qb->execute(); }
if (count($transactions) == 0) { $latest_sequence = new midgard_datetime('0000-00-00'); } else { foreach ($transactions as $transaction) { $latest_sequence = $transaction->metadata->created; } } $sequence = array(); foreach ($_MIDGARD['schema']['types'] as $type => $val) { if (substr($type, 0, 2) == '__') { continue; } // Updated objects $qb = new midgard_query_builder($type); $qb->add_constraint('metadata_revised', '>', $latest_sequence); $qb->add_order('metadata_revised', 'ASC'); $objects = $qb->execute(); foreach ($objects as $object) { $revised = $object->metadata->revised->format(DATE_ATOM); $sequence["{$revised}_{$object->guid}"] = array('objectguid' => $object->guid, 'type' => $type, 'revision' => $object->metadata->revision); } } ksort($sequence); // Populate the sequence to DB foreach ($sequence as $seq) { $seq_object = new midgard_sequence(); $seq_object->objectguid = $seq['objectguid']; $seq_object->type = $seq['type']; $seq_object->revision = $seq['revision']; $seq_object->create(); }
/** * Static method to add default ("title" and "name") sorts to a QB instance * * @param midgard_query_builder $qb reference to QB instance * @param string $schema_type valid mgdschema class name */ public static function add_schema_sorts_to_qb(&$qb, $schema_type) { // Sort by "title" and "name" if available $ref = midcom_helper_reflector_tree::get($schema_type); $dummy = new $schema_type(); $title_property = $ref->get_title_property($dummy); if (is_string($title_property) && midcom::get('dbfactory')->property_exists($schema_type, $title_property)) { $qb->add_order($title_property); } $name_property = $ref->get_name_property($dummy); if (is_string($name_property) && midcom::get('dbfactory')->property_exists($schema_type, $name_property)) { $qb->add_order($name_property); } unset($title_property, $name_property, $ref, $dummy); }
public function action_stream_page($route_id, &$data, $args) { $qb = new midgard_query_builder('net_nemein_avaudu_message'); $qb->set_limit((int) $this->configuration->get('messages_per_page')); $qb->set_offset((int) $args['page']); $qb->add_order('metadata.published', 'DESC'); $messages = $qb->execute(); $data['statuses'] = array(); foreach ($messages as $message) { $data['statuses'][] = net_nemein_avaudu_controllers_timeline::message2status($message); } // TODO: New MidCOM does this way more gracefully header('Content-type: application/json'); echo json_encode($data); die; }
debug_add('$query is all wildcards, don\'t waste time in adding LIKE constraints'); } else { $qb->begin_group('OR'); foreach ($searchfields as $field) { debug_add("adding search (ORed) constraint: {$field} LIKE '{$query}'"); $qb->add_constraint($field, 'LIKE', $query); } $qb->end_group(); } if (is_array($orders)) { ksort($orders); reset($orders); foreach ($orders as $data) { foreach ($data as $field => $order) { debug_add("adding order: {$field}, {$order}"); $qb->add_order($field, $order); } } } $results = $qb->execute(); if ($results === false) { $response->errstr = "Error when executing QB"; $response->send(); } } if (count($results) <= 0 || !is_array($results)) { $response->status = 2; $response->errstr = "No results found"; $response->send(); } $response->status = 1;
private function _prepare_qb() { $query = $this->_request["term"]; $wildcard_query = $this->_add_wildcards($query); $qb = @call_user_func(array($this->_request['class'], 'new_query_builder')); if (!$qb) { debug_add("use midgard_query_builder"); $qb = new midgard_query_builder($class); } if (!empty($this->_request['constraints']) && is_array($this->_request['constraints'])) { $constraints = $this->_request['constraints']; ksort($constraints); reset($constraints); foreach ($constraints as $key => $data) { if (!array_key_exists('value', $data) || empty($data['field']) || empty($data['op'])) { debug_add("Constraint #{$key} is not correctly defined, skipping", MIDCOM_LOG_WARN); continue; } $qb->add_constraint($data['field'], $data['op'], $data['value']); } } if (preg_match('/^%+$/', $query)) { debug_add('$query is all wildcards, don\'t waste time in adding LIKE constraints'); } else { $reflector = new midgard_reflection_property(midcom_helper_reflector::resolve_baseclass($this->_request['class'])); $qb->begin_group('OR'); foreach ($this->_request['searchfields'] as $field) { $field_type = $reflector->get_midgard_type($field); $operator = 'LIKE'; if (strpos($field, '.')) { //TODO: This should be resolved properly $field_type = MGD_TYPE_STRING; } switch ($field_type) { case MGD_TYPE_GUID: case MGD_TYPE_STRING: case MGD_TYPE_LONGTEXT: debug_add("adding search (ORed) constraint: {$field} LIKE '{$wildcard_query}'"); $qb->add_constraint($field, 'LIKE', $wildcard_query); break; case MGD_TYPE_INT: case MGD_TYPE_UINT: case MGD_TYPE_FLOAT: debug_add("adding search (ORed) constraint: {$field} = {$query}'"); $qb->add_constraint($field, '=', $query); break; default: debug_add("can't handle field type " . $field_type, MIDCOM_LOG_WARN); break; } } $qb->end_group(); } if (!empty($this->_request['orders']) && is_array($this->_request['orders'])) { ksort($this->_request['orders']); reset($this->_request['orders']); foreach ($this->_request['orders'] as $data) { foreach ($data as $field => $order) { $qb->add_order($field, $order); } } } return $qb; }
/** * @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_sidebyside($handler_id, array $args, array &$data) { $this->set_active_leaf('persons_merge'); midcom::get('auth')->require_valid_user(); // Process the selection if present. if (!empty($_POST['org_openpsa_contacts_handler_duplicates_person_keep']) && !empty($_POST['org_openpsa_contacts_handler_duplicates_person_options']) && count($_POST['org_openpsa_contacts_handler_duplicates_person_options']) == 2) { $option1 = new org_openpsa_contacts_person_dba($_POST['org_openpsa_contacts_handler_duplicates_person_options'][1]); $option2 = new org_openpsa_contacts_person_dba($_POST['org_openpsa_contacts_handler_duplicates_person_options'][2]); foreach ($_POST['org_openpsa_contacts_handler_duplicates_person_keep'] as $keep => $dummy) { switch (true) { case $keep == 'both': $option1->require_do('midgard:update'); $option2->require_do('midgard:update'); if (!$option1->parameter('org.openpsa.contacts.duplicates:not_duplicate', $option2->guid, time()) || !$option2->parameter('org.openpsa.contacts.duplicates:not_duplicate', $option1->guid, time())) { $errstr = midcom_connection::get_error_string(); // Failed to set as not duplicate, clear parameters that might have been set $option1->parameter('org.openpsa.contacts.duplicates:not_duplicate', $option2->guid, ''); $option2->parameter('org.openpsa.contacts.duplicates:not_duplicate', $option1->guid, ''); // TODO: Localize midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.contacts'), "Failed to mark #{$option1->id} and # {$option2->id} as not duplicates, errstr: {$errstr}", 'error'); // Switch is a "loop" so we continue 2 levels to get out of the foreach as well continue 2; } // Clear the possible duplicate parameters $option1->parameter('org.openpsa.contacts.duplicates:possible_duplicate', $option2->guid, ''); $option2->parameter('org.openpsa.contacts.duplicates:possible_duplicate', $option1->guid, ''); // TODO: Localize midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.contacts'), "Keeping both \"{$option1->name}\" and \"{$option2->name}\", they will not be marked as duplicates in the future", 'ok'); // Switch is a "loop" so we continue 2 levels to get out of the foreach as well continue 2; // Safety break break; case $keep == $option1->guid: $person1 =& $option1; $person2 =& $option2; break; case $keep == $option2->guid: $person1 =& $option2; $person2 =& $option1; break; default: throw new midcom_error('Something weird happened (basically we got bogus data)'); } $person1->require_do('midgard:update'); $person2->require_do('midgard:delete'); // TODO: Merge person2 data to person1 and then delete person2 $merger = new org_openpsa_contacts_duplicates_merge('person'); if (!$merger->merge_delete($person1, $person2)) { // TODO: Localize midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.contacts'), 'Merge failed, errstr: ' . $merger->errstr(), 'error'); } } //PONDER: redirect to avoid reloading the POST in case user presses reload ?? } // Then find us next pair we have sufficient rights for... $this->_request_data['notfound'] = false; $this->_request_data['person1'] = false; $this->_request_data['person2'] = false; $this->_request_data['loop_i'] = 0; $i =& $this->_request_data['loop_i']; if (isset($_REQUEST['org_openpsa_contacts_handler_duplicates_person_decide_later']) && isset($_REQUEST['org_openpsa_contacts_handler_duplicates_person_loop_i'])) { $i = $_REQUEST['org_openpsa_contacts_handler_duplicates_person_loop_i'] + 1; } while ($i < 100) { debug_add("Loop iteration {$i}"); $qb = new midgard_query_builder('midgard_parameter'); $qb->add_constraint('domain', '=', 'org.openpsa.contacts.duplicates:possible_duplicate'); $qb->add_order('name', 'ASC'); $qb->set_limit(1); if ($i > 0) { $qb->set_offset($i); } $ret = @$qb->execute(); if (empty($ret)) { debug_add("No more results to be had, setting notfound and breaking out of loop"); $this->_request_data['notfound'] = true; break; } $param =& $ret[0]; debug_add("Found duplicate mark on person #{$param->parentguid} for person {$param->name}"); try { $person1 = new org_openpsa_contacts_person_dba($param->parentguid); $person2 = new org_openpsa_contacts_person_dba($param->name); } catch (midcom_error $e) { $i++; continue; } // Make sure we actually have enough rights to do this if (!$person1->can_do('midgard:update') || !$person1->can_do('midgard:delete') || !$person2->can_do('midgard:update') || !$person2->can_do('midgard:delete')) { debug_add("Insufficient rights to merge these two, continuing to see if we have more"); $i++; continue; } // Extra sanity check (in case of semi-successful not-duplicate mark) if ($person1->parameter('org.openpsa.contacts.duplicates:not_duplicate', $person2->guid) || $person2->parameter('org.openpsa.contacts.duplicates:not_duplicate', $person1->guid)) { debug_add("It seems these two (#{$person1->id} and #{$person2->id}) have also marked as not duplicates, some cleanup might be a good thing", MIDCOM_LOG_WARN); $i++; continue; } $this->_request_data['probability'] = (double) $param->value; $this->_request_data['person1'] = $person1; $this->_request_data['person2'] = $person2; break; } }