示例#1
0
 /**
  * Returns an array of posted forms that belong to the given package.
  * Each element of the array is appended with submitter info as well
  * as some css styling helper (odd or even row), and a local url where
  * the form can be viewed by admins.
  *
  * @param guid GUID of the package
  * @param integer number of the page
  *                Items shown on one page is configurable;
  *                see: defaults.yml
  *
  * @return array of com_meego_package_forms_posted objects
  */
 public function get_posted_forms($package_guid = null, $page = 1)
 {
     $cnt = 0;
     $retval = array();
     $retval['forms'] = null;
     $retval['previous_page'] = 1;
     $retval['next_page'] = 1;
     // @todo: maybe do a GUID check here?
     if (!$package_guid) {
         return $retval;
     }
     $posts = self::get_all_forms($package_guid);
     if (is_array($posts) && count($posts)) {
         $total = count($posts);
         $limit = $this->mvc->configuration->rows_per_page * $this->mvc->configuration->items_per_row;
         $paged_posts = com_meego_packages_pager::page($limit, $page, $posts);
         $retval['total'] = $total;
         $retval['items_shown'] = $paged_posts['items_shown'];
         $retval['previous_page'] = $paged_posts['previous_page'];
         $retval['next_page'] = $paged_posts['next_page'];
         foreach ($paged_posts['content'] as $post) {
             ++$cnt % 2 == 0 ? $post->rawclass = 'even' : ($post->rawclass = 'odd');
             $post->localurl = $this->mvc->dispatcher->generate_url('package_posted_form_instance', array('forminstance' => $post->forminstanceguid), $this->request);
             $post->submitter = "n/a";
             // get the login name for the submitter
             $user = com_meego_packages_utils::get_user_by_person_guid($post->submitterguid);
             if ($user) {
                 $post->submitter = $user->login;
             }
             if (!isset($retval['forms'][$post->formtitle]['title'])) {
                 $retval['forms'][$post->formtitle]['title'] = $post->formtitle;
             }
             $retval['forms'][$post->formtitle]['posts'][$post->forminstanceguid] = $post;
         }
     }
     return $retval;
 }
示例#2
0
 /**
  * retreives a form instance
  */
 public function get_posted_form_instance(array $args)
 {
     $forminstance = new midgardmvc_ui_forms_form_instance($args['forminstance']);
     $form = new midgardmvc_ui_forms_form($forminstance->form);
     // get user info
     $currentuser = com_meego_packages_utils::get_current_user();
     $formcreator = com_meego_packages_utils::get_user_by_person_guid($forminstance->metadata->creator);
     $form_to_return = midgardmvc_ui_forms_load::load_form(midgardmvc_ui_forms_generator::get_by_guid($form->guid), $forminstance);
     $form_to_return->set_readonly(true);
     if ($currentuser) {
         if ($forminstance->metadata->creator == $currentuser->person) {
             $form_to_return->set_readonly(false);
         }
     }
     $login = '******';
     if ($formcreator) {
         $login = $formcreator->login;
     }
     $this->data['title'] = $form->title . " submitted by " . $login;
     $this->data['form'] = $form_to_return;
     unset($form_to_return);
 }
 /**
  * Retrieves all ratings belonging to the object having the guid: $this->data['to'].
  *
  * Passes all ratings to the view ($this->data['ratings']).
  * Calculates the average rating and passes that to the view too ($this->data['average']).
  * Sets the rated flag ($this->data['rated']) to show if object was ever rated or not.
  * Sets the can_post flag ($this->data['can_post']), so that the view can determine
  * whether to show a POST form or not.
  *
  * @param array arguments
  */
 public function get_ratings(array $args)
 {
     $this->get_average($args);
     if (midgardmvc_core::get_instance()->authentication->is_user()) {
         $this->data['can_post'] = true;
     } else {
         $this->data['can_post'] = false;
     }
     // @todo: can't add elements to head from here.. why?
     // Enable jQuery in case it is not enabled yet
     midgardmvc_core::get_instance()->head->enable_jquery();
     // Add rating CSS
     $css = array('href' => MIDGARDMVC_STATIC_URL . '/com_meego_ratings/js/jquery.rating/jquery.rating.css', 'rel' => 'stylesheet');
     midgardmvc_core::get_instance()->head->add_link($css);
     // Add rating js
     midgardmvc_core::get_instance()->head->add_jsfile(MIDGARDMVC_STATIC_URL . '/com_meego_ratings/js/jquery.rating/jquery.rating.pack.js', true);
     $this->data['to'] = midgard_object_class::get_object_by_guid($args['to']);
     $storage = new midgard_query_storage('com_meego_ratings_rating_author');
     $q = new midgard_query_select($storage);
     $q->set_constraint(new midgard_query_constraint(new midgard_query_property('to'), '=', new midgard_query_value($this->data['to']->guid)));
     $q->add_order(new midgard_query_property('posted'), SORT_DESC);
     $q->execute();
     $ratings = $q->list_objects();
     $this->data['ratings'] = array();
     if (count($ratings)) {
         $this->data['rated'] = true;
         foreach ($ratings as $rating) {
             $rating->stars = '';
             // get comment if available
             if ($rating->ratingcomment) {
                 $comment = new com_meego_comments_comment($rating->ratingcomment);
                 $rating->ratingcommentcontent = $comment->content;
             }
             // add a new property containing the stars to the rating object
             $rating->stars = $this->draw_stars($rating->rating);
             // pimp the posted date
             $rating->date = gmdate('Y-m-d H:i e', strtotime($rating->posted));
             // avatar part
             $rating->avatar = false;
             if ($rating->authorguid) {
                 // get the midgard user name from rating->authorguid
                 $user = com_meego_packages_utils::get_user_by_person_guid($rating->authorguid);
                 if ($user && $user->login != 'admin') {
                     // get avatar and url to user profile page only if the user is not the midgard admin
                     try {
                         $rating->avatar = com_meego_packages_utils::get_avatar($user->login);
                         $rating->avatarurl = midgardmvc_core::get_instance()->configuration->user_profile_prefix . $user->login;
                     } catch (Exception $e) {
                         // no avatar
                     }
                 }
             }
             array_push($this->data['ratings'], $rating);
         }
     }
 }
示例#4
0
 /**
  * @todo: docs
  */
 private function comment_to_ocs(com_meego_package_ratings $rating, com_meego_ocs_OCSWriter $ocs)
 {
     $ocs->startElement('comment');
     $ocs->writeElement('id', $rating->commentid);
     $ocs->writeElement('subject', $rating->version . ':' . $rating->title);
     $ocs->writeElement('text', $rating->comment);
     // todo: no support of subcomments yet
     $userid = '';
     $user = com_meego_packages_utils::get_user_by_person_guid($rating->authorguid);
     if ($user) {
         $userid = $user->login;
     }
     $ocs->writeElement('user', $userid);
     $ocs->writeElement('date', $rating->posted->format('c'));
     $ocs->writeElement('score', $rating->rating);
     $ocs->endElement();
 }
示例#5
0
 /**
  * Creates an account
  */
 private function create_account(array $ldapuser, array $tokens)
 {
     $user = null;
     $person = null;
     midgardmvc_core::get_instance()->authorization->enter_sudo('midgardmvc_core');
     $transaction = new midgard_transaction();
     $transaction->begin();
     $persons = $this->get_persons($ldapuser);
     if (count($persons) == 0) {
         $person = $this->create_person($ldapuser, $tokens);
     } else {
         // we have multiple persons with the same firstname and lastname
         // let's see the corresponding midgard_user object and its login field
         foreach ($persons as $person) {
             $user = com_meego_packages_utils::get_user_by_person_guid($person->guid);
             if ($user->login == $tokens['login']) {
                 break;
             } else {
                 $user = null;
                 $person = null;
             }
         }
     }
     if (!$user) {
         if (!$person) {
             $person = $this->create_person($ldapuser, $tokens);
         }
         if ($person) {
             $user = new midgard_user();
             $user->login = $tokens['login'];
             $user->password = '';
             $user->usertype = 1;
             $user->authtype = 'LDAP';
             $user->active = true;
             $user->set_person($person);
             if (!$user->create()) {
                 midgardmvc_core::get_instance()->log(__CLASS__, "Creating midgard_user for LDAP user failed: " . midgard_connection::get_instance()->get_error_string(), 'warning');
                 $transaction->rollback();
                 midgardmvc_core::get_instance()->authorization->leave_sudo();
                 return false;
             }
         }
     }
     midgardmvc_core::get_instance()->authorization->leave_sudo();
     if (!$transaction->commit()) {
         return false;
     }
     return true;
 }
示例#6
0
 /**
  * Gathers roles that belong to a package
  * @param guid of a package
  * @return array
  */
 public function get_roles($package_guid, $filter = null)
 {
     $retval = array();
     $storage = new midgard_query_storage('com_meego_package_role');
     $q = new midgard_query_select($storage);
     $qc = new midgard_query_constraint_group('AND');
     $qc->add_constraint(new midgard_query_constraint(new midgard_query_property('package'), '=', new midgard_query_value($package_guid)));
     if ($filter) {
         $qc->add_constraint(new midgard_query_constraint(new midgard_query_property('role'), '=', new midgard_query_value($filter)));
     } else {
         $qc->add_constraint(new midgard_query_constraint(new midgard_query_property('id'), '>', new midgard_query_value(0)));
     }
     $q->set_constraint($qc);
     $q->execute();
     $roles = $q->list_objects();
     if ($roles) {
         foreach ($roles as $role) {
             if (!array_key_exists($role->role, $retval)) {
                 $retval[$role->role] = array();
                 $retval[$role->role]['users'] = array();
             }
             if (isset($role->user)) {
                 $user = com_meego_packages_utils::get_user_by_guid($role->user);
                 if (!array_key_exists($user->login, $retval[$role->role]['users'])) {
                     $retval[$role->role]['users'][$user->login] = array();
                 }
                 $retval[$role->role]['users'][$user->login]['login'] = $user->login;
                 $retval[$role->role]['users'][$user->login]['profile'] = $this->mvc->configuration->user_profile_prefix . $user->login;
             }
             if (count($retval[$role->role]['users']) > 1) {
                 $retval[$role->role]['title'] = $this->mvc->i18n->get('label_roles_' . $role->role);
             } else {
                 $retval[$role->role]['title'] = $this->mvc->i18n->get('label_role_' . $role->role);
             }
         }
     }
     return $retval;
 }
示例#7
0
 /**
  * The main method to return list of packages
  *
  * @param array HTTP GET arguments
  */
 public function get_data(array $args)
 {
     $constraints = array();
     $query = $this->request->get_query();
     $storage = new midgard_query_storage('com_meego_package_details');
     $q = new midgard_query_select($storage);
     $qc = new midgard_query_constraint_group('OR');
     // filter all hidden packages
     $qc->add_constraint(new midgard_query_constraint(new midgard_query_property('packagehidden'), '=', new midgard_query_value(1)));
     // filter packages by their names
     foreach ($this->mvc->configuration->sql_package_filters as $filter) {
         $qc->add_constraint(new midgard_query_constraint(new midgard_query_property('packagename'), 'LIKE', new midgard_query_value($filter)));
         $qc->add_constraint(new midgard_query_constraint(new midgard_query_property('packagetitle'), 'LIKE', new midgard_query_value($filter)));
     }
     $q->set_constraint($qc);
     $q->execute();
     $filtered = array();
     foreach ($q->list_objects() as $package) {
         $filtered[] = $package->packageid;
     }
     if (count($filtered)) {
         $constraints[] = new midgard_query_constraint(new midgard_query_property('packageid'), 'NOT IN', new midgard_query_value($filtered));
     }
     if (count($query)) {
         if (array_key_exists('search', $query) && strlen($query['search'])) {
             $cnstr1 = new midgard_query_constraint(new midgard_query_property('packagename'), 'LIKE', new midgard_query_value('%' . $query['search'] . '%'));
             $cnstr2 = new midgard_query_constraint(new midgard_query_property('packagetitle'), 'LIKE', new midgard_query_value('%' . $query['search'] . '%'));
             $group_constraint = new midgard_query_constraint_group("OR", $cnstr1, $cnstr2);
             $constraints[] = $group_constraint;
         }
         if (array_key_exists('categories', $query) && strlen($query['categories'])) {
             $constraints[] = new midgard_query_constraint(new midgard_query_property('basecategory'), 'IN', new midgard_query_value(explode('x', $query['categories'])));
         }
         if (array_key_exists('license', $query) && strlen($query['license'])) {
             $constraints[] = new midgard_query_constraint(new midgard_query_property('packagelicenseid'), 'IN', new midgard_query_value(explode(',', $query['license'])));
         }
         if (array_key_exists('distribution', $query) && strlen($query['distribution'])) {
             $constraints[] = new midgard_query_constraint(new midgard_query_property('repoosversionid'), 'IN', new midgard_query_value(explode(',', $query['distribution'])));
         }
         if (array_key_exists('dependency', $query) && strlen($query['dependency'])) {
             $constraints[] = new midgard_query_constraint(new midgard_query_property('repoosuxid'), 'IN', new midgard_query_value(explode(',', $query['dependency'])));
         }
         if (array_key_exists('sortmode', $query) && strlen($query['sortmode'])) {
             switch ($query['sortmode']) {
                 case 'new':
                     $q->add_order(new midgard_query_property('packagecreated'), SORT_DESC);
                     break;
                 case 'high':
                     $q->add_order(new midgard_query_property('statscachedratingvalue'), SORT_DESC);
                     //sort by name too
                     $q->add_order(new midgard_query_property('packagetitle'), SORT_ASC);
                     break;
                 case 'down':
                     //sort by name too
                     $q->add_order(new midgard_query_property('statscachedratingvalue'), SORT_ASC);
                 case 'alpha':
                 default:
                     $q->add_order(new midgard_query_property('packagetitle'), SORT_ASC);
                     break;
             }
         }
     }
     if (isset($args['id'])) {
         $constraints[] = new midgard_query_constraint(new midgard_query_property('packageid'), '=', new midgard_query_value($args['id']));
     }
     $qc = null;
     if (count($constraints) > 1) {
         $qc = new midgard_query_constraint_group('AND');
         foreach ($constraints as $constraint) {
             $qc->add_constraint($constraint);
         }
     } else {
         if (isset($constraints[0])) {
             $qc = $constraints[0];
         }
     }
     if (is_object($qc)) {
         $q->set_constraint($qc);
     }
     // 1st execute to get the total number of records
     // required by OCS
     $q->execute();
     $total = $q->get_results_count();
     if (array_key_exists('pagesize', $query) && strlen($query['pagesize'])) {
         $this->pagesize = $query['pagesize'];
     }
     $q->set_limit($this->pagesize);
     $page = 0;
     if (array_key_exists('page', $query) && strlen($query['page'])) {
         $page = $query['page'];
     }
     $offset = $page * $this->pagesize;
     $q->set_offset($offset);
     if ($offset > $total) {
         $offset = $total - $this->pagesize;
     }
     // 2nd execute to limit pagesize
     $q->execute();
     $ocs = new com_meego_ocs_OCSWriter();
     if ($total > 0) {
         $packageids = array();
         $localpackages = array();
         $packages = $q->list_objects();
         $done = array();
         foreach ($packages as $package) {
             if (in_array($package->packageid, $packageids)) {
                 // mimic distinct (Midgard Ratatoskr does not support it on SQL level)
                 --$total;
                 continue;
             }
             // set a special flag if the package is from a testing repository
             $package->testing = false;
             foreach ($this->mvc->configuration->top_projects as $top_project) {
                 if ($top_project['staging'] == $package->repoprojectname) {
                     $package->testing = true;
                 }
             }
             $package->comments_count = 0;
             // get number of comments
             $comments_qs = new midgard_query_storage('com_meego_comments_comment');
             $comments_q = new midgard_query_select($comments_qs);
             $comments_q->set_constraint(new midgard_query_constraint(new midgard_query_property('to'), '=', new midgard_query_value($package->packageguid)));
             $comments_q->execute();
             $package->comments_count = $comments_q->get_results_count();
             // get attachments
             $origpackage = new com_meego_package($package->packageguid);
             $package->attachments = $origpackage->list_attachments();
             unset($origpackage);
             // get the voters (who rated the package)
             $package->ratings = '';
             $ratings = com_meego_packages_controllers_application::prepare_ratings($package->packagename, true);
             if (count($ratings['ratings'])) {
                 foreach ($ratings['ratings'] as $rating) {
                     $ratings_[] = array('user' => $rating->user, 'version' => $rating->version, 'rate' => $rating->rating, 'date' => $rating->date);
                 }
                 $package->ratings = serialize($ratings_);
             }
             // some attributes returned only if an exact package is queried
             if (isset($args['id'])) {
                 // generate the URL of the package instance
                 $path = midgardmvc_core::get_instance()->dispatcher->generate_url('package_instance', array('package' => $package->packagename, 'version' => $package->packageversion, 'project' => $package->repoprojectname, 'repository' => $package->reponame, 'arch' => $package->repoarch), '/');
                 $package->commentsurl = com_meego_ocs_controllers_providers::generate_url($path);
                 // get the roles
                 $package->roles = null;
                 $roles = com_meego_packages_controllers_application::get_roles($package->packageguid);
                 if (count($roles)) {
                     $package->roles = serialize($roles);
                 }
                 // get the forms
                 $package->qa = null;
                 $forms = com_meego_packages_utils::get_stripped_forms_for_package($package);
                 if (count($forms)) {
                     $package->qa = serialize($forms);
                 }
             }
             if ($package->packagename != '') {
                 //get history
                 $args = array('os' => $package->repoos, 'version' => $package->repoosversion, 'ux' => $package->repoosux, 'packagename' => $package->packagename);
                 if (!isset($done[$package->packagename])) {
                     $packagehistory = com_meego_packages_controllers_application::get_lightweight_history($args);
                     if (count($packagehistory)) {
                         $package->history = serialize($packagehistory);
                         $done[$package->packagename] = $package->history;
                         //die;
                     }
                 } else {
                     // just copy the history
                     $package->history = $done[$package->packagename];
                 }
             }
             $localpackages[] = $package;
             $packageids[] = $package->packageid;
         }
         // write the xml content
         $ocs->writeMeta($total, $this->pagesize);
         $ocs->writeContent(array_values($localpackages));
         unset($done, $packageids, $localpackages);
     } else {
         // item not found
         $ocs->writeMeta($total, $this->pagesize, 'content not found', 'failed', 101);
         $ocs->writeEmptyData();
     }
     $ocs->endDocument();
     self::output_xml($ocs);
 }