private function process()
 {
     $this->commonProcess();
     $config = services::getService('config');
     $lang = services::getService('lang');
     $params = services::getService('pageParams');
     $mail = services::getService('mail');
     $categories = services::getService('cats');
     $pool = new pools();
     if ($pool->get($params->getParam('pool_id'))) {
         // become member
         if ($params->getParam('action') == 'become_member') {
             if ($params->getParam('become_member_submit') || $pool->is_public) {
                 $new_membership = new poolsUser();
                 $new_membership->user_id = $this->user->id;
                 $new_membership->pool_id = $pool->id;
                 if (!$pool->is_public) {
                     $new_membership->wait = 1;
                 } else {
                     $new_membership->res_to_free = 1;
                     $new_membership->wait = 0;
                 }
                 if ($params->getParam('become_member_comments')) {
                     $new_membership->comments = $params->getParam('become_member_comments');
                 }
                 $new_membership->insert();
                 // compose E-Mails
                 if (!$pool->is_public) {
                     $tos = $pool->getAdmins();
                     foreach ($tos as $to) {
                         $mail->send('new_member', $to, $pool, $this->user, $new_membership->comments);
                     }
                 } else {
                     $this->switchPage('mysite&new_pool=' . $pool->id);
                 }
             } else {
                 $this->become_member_form = true;
             }
         }
         // are you waiting?
         if ($pool->isWaiting($this->user->id)) {
             $this->user_is_waiting = true;
         }
         // loose membership
         if ($params->getParam('action') == 'no_member') {
             // the last admin can't leave the pool
             if (!$pool->isLastAdmin($this->user->id)) {
                 $pool->removeMember($this->user->id);
                 $this->addMsg('msg_leave_pool');
             } else {
                 $this->addMsg('msg_leave_pool_last_admin');
             }
         }
         // build tabledata
         if ($pool->isMember($this->user->id)) {
             $showcats = $categories->getChildren(0);
             foreach ($showcats as $cat_id => $cat_name) {
                 $show_res = new resFetcher();
                 $show_res->_cat = $cat_id;
                 $show_res->_pools = array($pool->id);
                 $rescounter = $show_res->count();
                 if ($rescounter > 0) {
                     $this->cats[$cat_id] = array("name" => $cat_name, "count" => $rescounter);
                 }
             }
             $allcount = 0;
             foreach ($this->cats as $cat) {
                 $allcount += $cat['count'];
             }
             $this->cats[0] = array("name" => $lang->getMsg('cat_all'), "count" => $allcount);
         } else {
             if (!$pool->isMember($this->user->id, true)) {
                 $this->not_member = true;
             }
             $res_counter = new poolsResources();
             $res_counter->pool_id = $pool->id;
             $this->res_counter = 0;
             $res_counter->find();
             while ($res_counter->fetch()) {
                 ++$this->res_counter;
             }
         }
         // build forum-tabledata
         if ($pool->isMember($this->user->id)) {
             // new/change entry
             if ($params->getParam('action') == 'new_entry') {
                 // formular
                 $form = new form('forum_entry');
                 $form->addElement('text', 'entryheader', $lang->getMsg('showpool_forum_headline'), array('size' => 30, 'maxlength' => 100));
                 $form->addElement('textarea', 'entrytext', $lang->getMsg('home_news_text'), array('rows' => 15, 'cols' => 70));
                 $form->addElement('hidden', 'action', 'new_entry');
                 $form->addElement('hidden', 'pool_id', $pool->id);
                 $form->addElement('hidden', 'thread', $thread->id);
                 $form->addElement('submit', 'entrysubmit', $lang->getMsg('home_news_submit'));
                 $form->addRule('entryheader', $lang->getMsg('showpool_forum_headline_required'), 'required');
                 $form->addRule('entrytext', $lang->getMsg('showpool_forum_text_required'), 'required');
                 // write entry
                 if ($form->validate()) {
                     $new_thread = new forumThreads();
                     $new_thread->pool_id = $pool->id;
                     $new_thread->title = $form->exportValue('entryheader');
                     $new_thread->act_date = time();
                     $new_thread->insert();
                     $new_thread->find(true);
                     $new_entry = new forumEntries();
                     $new_entry->thread_id = $new_thread->id;
                     $new_entry->text = $form->exportValue('entrytext');
                     $new_entry->user_id = $this->user->id;
                     $new_entry->date = $new_thread->act_date;
                     $new_entry->insert();
                     $this->switchPage('showpool&pool_id=' . $pool->id . '&msg=msg_forum_entry_made');
                 }
                 $this->form = $form;
             }
             $showthreads = new forumThreads();
             $showthreads->pool_id = $pool->id;
             $showthreads->find();
             while ($showthreads->fetch()) {
                 $showthreads->fetchLastEntry();
                 $showthreads->last_entry->fetchUser();
                 $this->threads[] = array("id" => $showthreads->id, "title" => $showthreads->title, "act_date" => date('j. n. y, H:i', $showthreads->last_entry->date) . ' ' . $lang->getMsg('showpool_forum_lastentry_by') . ' ' . '<a href="./index.php?page=showmember&pool_id=' . $pool->id . '&showmember=' . $showthreads->last_entry->user->id . '">' . $showthreads->last_entry->user->name . '</a>');
             }
         }
         // build userlist
         if ($pool->id != 1) {
             // assotiativ array with object and detail-flag
             if ($pool->isMember($this->user->id)) {
                 $members = array();
                 $pool_users = new poolsUser();
                 $pool_users->pool_id = $pool->id;
                 $pool_users->wait = 0;
                 if ($pool_users->find()) {
                     $detail_id = $params->getParam('showmember');
                     $count = 1;
                     while ($pool_users->fetch()) {
                         if ($pool_users->user_id != $this->user->id) {
                             $pool_users->fetchUser();
                             $member = array("obj" => $pool_users->user, "detail" => $pool_users->user->id == $detail_id, "count" => $count, "admin" => $pool->isAdmin($pool_users->user->id));
                             if ($pool_users->user->name != "") {
                                 $members[] = $member;
                             }
                             ++$count;
                         }
                     }
                     $this->members = $members;
                 }
             }
         }
         $this->pool = $pool;
     } else {
         $this->switchPage('mysite');
     }
 }
 private function process()
 {
     $this->commonProcess();
     $config = services::getService('config');
     $lang = services::getService('lang');
     $params = services::getService('pageParams');
     $categories = services::getService('cats');
     if ($params->getParam('res_id')) {
         $this->header = $lang->getMsg('resdata_header_change');
         $this->is_change = true;
     } else {
         $this->header = $lang->getMsg('resdata_header_new');
         $this->is_change = false;
     }
     $aItems = $categories->getThisAndBelow(1);
     $sJavaScriptArray = sprintf('new Array(%s)', implode(',', array_keys($aItems)));
     $this->sJavaScriptArray = $sJavaScriptArray;
     if ($_POST['resdata_isbn_submit'] && is_isbn($_POST['resdata_isbn'])) {
         foreach ($_POST as $key => $value) {
             if ($key != 'page' && $key != 'name' && $key != 'description' && $key != 'resdata_authors') {
                 $get .= $key . '=' . $value . '&';
             }
         }
         $this->switchPage('resdata&' . $get);
     }
     // Instantiate the HTML_QuickForm object
     $this->form = new formResData('ResDataForm', $params->getParam($res_id), $this->user);
     $this->new_res_link = false;
     // Try to validate a form
     if ($this->form->validate() && $this->form->exportValue('submit')) {
         $newres = new resources();
         if ($params->getParam('res_id')) {
             $newres->get($params->getParam('res_id'));
         }
         $newres->name = $this->form->exportValue('name');
         $newres->description = $this->form->exportValue('description');
         $newres->cat = $this->form->exportValue('resdata_cat');
         $newres->type = in_array($newres->cat, array_keys($aItems)) ? $this->form->exportValue('type') : 0;
         // insert/update in res-maintable
         if ($params->getParam('res_id')) {
             $newres->update();
         } else {
             $newres->since = time();
             $newres->user_id = $this->user->id;
             $newres->insert();
         }
         $newres->find(true);
         // insert/update in attributes-tables
         $attr_string = new attributesString();
         $attr_string->res_id = $newres->id;
         $attr_string->delete();
         $attr_select = new attributesSelect();
         $attr_select->res_id = $newres->id;
         $attr_select->delete();
         if ($params->getParam('cat')) {
             $cat = $params->getParam('cat');
             $attributes = new attributes();
             $attributes->category_id = $cat;
             if ($attributes->find()) {
                 while ($attributes->fetch()) {
                     if ($attributes->type == "string") {
                         $attr_string = new attributesString();
                         $attr_string->res_id = $newres->id;
                         $attr_string->attribute_id = $attributes->id;
                         // insert
                         $value = $this->form->exportValue('resdata_' . $attributes->name);
                         if ($value != "") {
                             $attr_string->value = $value;
                             $attr_string->insert();
                         }
                     }
                     if ($attributes->type == "select") {
                         $values = $this->form->getElementValue('resdata_' . $attributes->name);
                         if (!is_array($values)) {
                             $values = array($values);
                         }
                         foreach ($values as $value) {
                             if ($value[0] != 0) {
                                 $attr_string = new attributesSelect();
                                 $attr_string->res_id = $newres->id;
                                 $attr_string->attribute_id = $attributes->id;
                                 // insert
                                 $attr_string->value = $value[0];
                                 $attr_string->insert();
                             }
                         }
                     }
                 }
             }
         }
         // add res to pools if given
         $respools = new poolsResources();
         $respools->res_id = $newres->id;
         $respools->delete();
         $res_pools = $this->form->exportValue('free_pools');
         if (is_array($res_pools)) {
             foreach ($res_pools as $pool_id => $bool) {
                 if ($bool) {
                     $newres->addPool($pool_id);
                 }
             }
         }
         if ($params->getParam('res_id')) {
             $params->addParam('msg', 'msg_data_change_success', 'page');
             $this->switchPage('resmanager&function=all');
         } else {
             $this->form->removeElement('resdata_isbn_submit');
             $this->form->freezeForm();
             $this->addMsg('msg_res_insert_success');
             $this->new_res_link = true;
         }
     }
     if ($_POST['resdata_isbn_submit']) {
         $this->form->addRule('name', $lang->getMsg('resdata_form_namenecessary'), 'required');
         $this->form->addRule('resdata_cat', $lang->getMsg('resdata_form_catnecessary'), 'required');
     }
 }
 private function process()
 {
     $this->commonProcess();
     $config = services::getService('config');
     $lang = services::getService('lang');
     $params = services::getService('pageParams');
     // for redirection
     if ($params->getParam('refer') == 'mysite') {
         $this->refer = 'mysite';
     } else {
         $this->refer = 'showpool';
     }
     // res_free_page
     $userres = new resFetcher();
     $userres->_order = "name";
     $userres->_user = $this->user->id;
     $userres->search();
     $this->userres = $userres->getAsArray();
     $user_new_pool = new pools();
     $user_new_pool->id = $params->getParam('pool_id');
     if ($user_new_pool->find(true)) {
         $this->user_new_pool = $user_new_pool;
         $user_new_pools = new poolsUser();
         $user_new_pools->pool_id = $user_new_pool->id;
         $user_new_pools->user_id = $this->user->id;
         $user_new_pools->find(true);
         if ($user_new_pools->res_to_free == 1) {
             $this->new_pool = true;
         }
         if ($params->getParam('no_free_submit')) {
             $user_new_pools->res_to_free = 0;
             $user_new_pools->update();
             // delete res
             foreach ($this->userres as $res) {
                 $del_res = new poolsResources();
                 $del_res->pool_id = $params->getParam('pool_id');
                 $del_res->res_id = $res->id;
                 $del_res->find();
                 $del_res->delete();
                 $freed = true;
             }
             $this->switchPage($this->refer . '&pool_id=' . $user_new_pools->pool_id . '&msg=msg_freeres_alldeleted');
         } else {
             if ($params->getParam('res_free_submit')) {
                 // delete res
                 foreach ($this->userres as $res) {
                     $del_res = new poolsResources();
                     $del_res->pool_id = $params->getParam('pool_id');
                     $del_res->res_id = $res->id;
                     $del_res->find();
                     $del_res->delete();
                     $freed = true;
                 }
                 foreach ($_POST as $res_id => $doesnmatter) {
                     $free_res = new poolsResources();
                     $free_res->res_id = $res_id;
                     $free_res->pool_id = $params->getParam('pool_id');
                     // insert res
                     if ($doesnmatter == "check") {
                         if (!$free_res->find()) {
                             $free_res->insert();
                             $freed = true;
                         }
                     }
                 }
                 // set $msg
                 if ($freed) {
                     $this->switchPage($this->refer . '&pool_id=' . $user_new_pools->pool_id . '&msg=msg_free_res_success');
                 }
                 $user_new_pools->res_to_free = 0;
                 $user_new_pools->update();
             } else {
                 if (is_array($this->userres)) {
                     $this->free_res = true;
                 } else {
                     $user_new_pools->res_to_free = 0;
                     $user_new_pools->update();
                     // switch page with msg
                     $this->switchPage('showpool&pool_id=' . $user_new_pools->pool_id . '&msg=msg_freeres_nores');
                 }
             }
         }
     }
 }
 function formResData($name, $res_id = 0, $user)
 {
     $lang = services::getService('lang');
     $params = services::getService('pageParams');
     $cats = services::getService('cats');
     $this->form($name);
     if ($params->getParam('res_id')) {
         $this->addElement('hidden', 'res_id', $params->getParam('res_id'));
         // Categories
         $this->addElement('select', 'resdata_cat', $lang->getMsg('resdata_form_category'), $cats->getAll(), array('onchange' => 'resdata_cat_change(this.value, document.ResDataForm.res_id.value)'));
     } else {
         $this->addElement('select', 'resdata_cat', $lang->getMsg('resdata_form_category'), $cats->getAll(), array('onchange' => 'resdata_cat_change_new(this.value)'));
     }
     $this->addElement('text', 'name', $lang->getMsg('resdata_form_name'), array('size' => 40, 'maxlength' => 255));
     $this->addElement('textarea', 'description', $lang->getMsg('resdata_form_description'), array('rows' => 7, 'cols' => 40));
     // fetch additional attributes e.g. form-fields for chosen category
     if ($params->getParam('cat')) {
         $cat = $params->getParam('cat');
         $this->addElement('hidden', 'cat', $cat);
         $attributes = new attributes();
         $attributes->category_id = $cat;
         if ($attributes->find()) {
             while ($attributes->fetch()) {
                 if ($attributes->type == "string") {
                     if ($attributes->name == "isbn") {
                         $this->addElement('text', 'resdata_' . $attributes->name, $lang->getMsg('resdata_form_' . $attributes->name), array('size' => 30, 'maxlength' => 255));
                         $this->addElement('submit', 'resdata_' . $attributes->name . '_submit', $lang->getMsg('resdata_form_' . $attributes->name . '_submit'), "asd");
                     } else {
                         $this->addElement('text', 'resdata_' . $attributes->name, $lang->getMsg('resdata_form_' . $attributes->name), array('size' => 30, 'maxlength' => 255));
                     }
                 }
                 if ($attributes->type == "select") {
                     $keys = new attributesSelectKeys();
                     $keys->attribute_id = $attributes->id;
                     $keys->find();
                     $options = array();
                     $options[0] = "----";
                     while ($keys->fetch()) {
                         $options[$keys->key] = $lang->getMsg('resdata_form_select_' . $keys->value);
                     }
                     sort($options);
                     $select_elements = array();
                     for ($i = 1; $attributes->amount >= $i; ++$i) {
                         $select_elements[] =& HTML_QuickForm::createElement('select', $i, null, $options);
                     }
                     $this->addGroup($select_elements, 'resdata_' . $attributes->name, $lang->getMsg('resdata_form_' . $attributes->name), ' ');
                 }
             }
         }
     }
     $this->addElement('select', 'type', $lang->getMsg('resdata_form_type'), array("2" => $lang->getMsg('resdata_form_type_borrow'), "1" => $lang->getMsg('resdata_form_type_give')));
     $free_pool = new poolsUser();
     $free_pool->user_id = $user->id;
     $free_pool->wait = 0;
     $free_pool->find();
     $free_pools = array();
     while ($free_pool->fetch()) {
         $free_pool->fetchPool();
         $free_pools[] =& HTML_QuickForm::createElement('checkbox', $free_pool->pool->id, null, ' ' . $free_pool->pool->name, $free_pool->pool->id);
     }
     $this->addGroup($free_pools, 'free_pools', $lang->getMsg('resdata_form_pools'), '<br>');
     $this->addElement('submit', 'submit', $lang->getMsg('resdata_form_submit'));
     // rules
     if (!$_POST['resdata_isbn_submit']) {
         $this->addRule('name', $lang->getMsg('resdata_form_namenecessary'), 'required');
         $this->addRule('resdata_cat', $lang->getMsg('resdata_form_catnecessary'), 'required');
     }
     // presets
     if ($params->getParam('resdata_isbn_submit') && is_isbn($params->getParam('resdata_isbn'))) {
         $lastletter = 0;
         $isbnfound = 0;
         $firstletter = substr($params->getParam('resdata_isbn'), 0, 1);
         if ($firstletter == 0 || $firstletter == 1) {
             $urlendings = array('com', 'co.uk', 'de', 'jp', 'fr', 'ca');
         } else {
             if ($firstletter == 2) {
                 $urlendings = array('fr', 'de', 'com', 'co.uk', 'jp', 'ca');
             } else {
                 if ($firstletter == 3) {
                     $urlendings = array('de', 'com', 'co.uk', 'jp', 'fr', 'ca');
                 } else {
                     if ($firstletter == 4) {
                         $urlendings = array('jp', 'de', 'com', 'co.uk', 'fr', 'ca');
                     } else {
                         $urlendings = array('com', 'co.uk', 'de', 'fr', 'ca', 'jp');
                     }
                 }
             }
         }
         $lastletters = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 'x');
         foreach ($urlendings as $urlending) {
             foreach ($lastletters as $lastletter) {
                 if ($isbnfound == 0) {
                     $url = "http://ecs.amazonaws." . $urlending . "/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=1WFW5KS6EFZ0M8690ZR2&Operation=ItemLookup&ItemId=" . substr($params->getParam('resdata_isbn'), 0, 9) . $lastletter . "&ResponseGroup=Medium&type=lite&f=xml";
                     $xml = file_get_contents($url);
                     if (!strpos($xml, 'is not a valid value for ItemId')) {
                         $isbnfound = true;
                     }
                 }
             }
         }
         $first = strpos($xml, '<Item>') + 6;
         $sec = strpos($xml, '</Item>');
         $newxml = substr($xml, $first, $sec - $first);
         $newxml = unhtmlentities($newxml);
         // title
         $first = strpos($newxml, '<Title>') + 7;
         $sec = strpos($newxml, '</Title>');
         $title = substr($newxml, $first, $sec - $first);
         // binding
         $first = strpos($newxml, '<Binding>') + 9;
         $sec = strpos($newxml, '</Binding>');
         $binding = substr($newxml, $first, $sec - $first);
         // publisher
         $first = strpos($newxml, '<Label>') + 7;
         $sec = strpos($newxml, '</Label>');
         $publisher = substr($newxml, $first, $sec - $first);
         // publication date
         $first = strpos($newxml, '<PublicationDate>') + 17;
         $sec = strpos($newxml, '</PublicationDate>');
         $publicationdate = substr($newxml, $first, $sec - $first);
         // number of pages
         $first = strpos($newxml, '<NumberOfPages>') + 15;
         $sec = strpos($newxml, '</NumberOfPages>');
         $numberofpages = substr($newxml, $first, $sec - $first);
         // author(s)
         $authors = array();
         while (strstr($newxml, '<Author>')) {
             $first = strpos($newxml, '<Author>') + 8;
             $sec = strpos($newxml, '</Author>');
             $authors[] = substr($newxml, $first, $sec - $first);
             $newxml = substr($newxml, $sec + 9);
         }
         foreach ($authors as $key => $author) {
             if ($key != 0) {
                 $authors_field .= "; ";
             }
             $authors_field .= $this->changeAuthor($author);
         }
         // description
         $first = strpos($newxml, '<EditorialReviews><EditorialReview><Source>Aus der Amazon.de-Redaktion</Source><Content>') + 88;
         $sec = strpos($newxml, '</Content></EditorialReview></EditorialReviews>');
         $description = substr($newxml, $first, $sec - $first);
         $description = substr($description, 0, strrpos($description, '--'));
         // name
         if ($authors_field) {
             $name = $authors_field . ": " . $title;
         } else {
             $name = $title;
         }
         $this->setDefaults(array('resdata_title' => $title, 'name' => $name, 'description' => $description, 'resdata_authors' => $authors_field, 'resdata_binding' => $binding, 'resdata_number_of_pages' => $numberofpages, 'resdata_publication_date' => $publicationdate, 'resdata_publisher' => $publisher, 'resdata_isbn' => $params->getParam('resdata_isbn')));
     }
     if ($params->getParam('res_id')) {
         $free_pool = new poolsResources();
         $free_pool->res_id = $params->getParam('res_id');
         $free_pool->find();
         $free_pools_presets = array();
         while ($free_pool->fetch()) {
             $free_pools_presets[$free_pool->pool_id] = 1;
         }
         $changeform_res = new resources();
         $changeform_res->get($params->getParam('res_id'));
         $this->setDefaults(array('name' => $changeform_res->name, 'type' => $changeform_res->type, 'description' => $changeform_res->description, 'resdata_cat' => $changeform_res->cat, 'free_pools' => $free_pools_presets, 'public' => $changeform_res->is_public));
         // presets for additional fields
         if ($params->getParam('cat')) {
             $cat = $params->getParam('cat');
             $this->setDefaults(array('resdata_cat' => $cat));
             $attributes = new attributes();
             $attributes->category_id = $cat;
             if ($attributes->find()) {
                 while ($attributes->fetch()) {
                     if ($attributes->type == "string") {
                         $attr_string_presets = new attributesString();
                         $attr_string_presets->res_id = $params->getParam('res_id');
                         $attr_string_presets->attribute_id = $attributes->id;
                         $attr_string_presets->find(true);
                         $this->setDefaults(array('resdata_' . $attributes->name => $attr_string_presets->value));
                     }
                     if ($attributes->type == "select") {
                         $attr_select_presets = new attributesSelect();
                         $attr_select_presets->res_id = $params->getParam('res_id');
                         $attr_select_presets->attribute_id = $attributes->id;
                         $attr_select_presets->find();
                         $i = 1;
                         while ($attr_select_presets->fetch()) {
                             $this->setDefaults(array("resdata_" . $attributes->name . "[" . $i . "]" => $attr_select_presets->value));
                             ++$i;
                         }
                     }
                 }
             }
         }
     } else {
         if (isset($pool)) {
             $free_pools_presets[$pool->id] = 1;
             $this->setDefaults(array('type' => 2, 'public' => 0, 'free_pools' => $free_pools_presets));
         }
     }
     if ($params->getParam('cat')) {
         $this->setDefaults(array('resdata_cat' => $params->getParam('cat')));
     }
 }
 function addPool($pool_id)
 {
     // add to relations-table
     $new_membership = new poolsResources();
     $new_membership->res_id = $this->id;
     $new_membership->pool_id = $pool_id;
     if (!$new_membership->insert()) {
         $new_membership->update();
     }
 }
 function addRes($res_id)
 {
     $add_res = new poolsResources();
     $add_res->res_id = $res_id;
     $add_res->pool_id = $this->id;
     $add_res->insert();
 }