Пример #1
0
 function saveLendRequest($option, $bids)
 {
     global $mainframe, $database, $my, $Itemid, $acl, $mosConfig_live_site;
     global $booklibrary_configuration, $mosConfig_mailfrom, $doc;
     //print_r($mosConfig_mailfrom);
     PHP_booklibrary::addTitleAndMetaTags();
     $doc->addStyleSheet($mosConfig_live_site . '/components/com_booklibrary/includes/booklibrary.css');
     $data = JFactory::getDBO();
     if (!$GLOBALS['lendstatus_show'] || !checkAccessBL($GLOBALS['lendrequest_registrationlevel'], 'RECURSE', userGID_BL($my->id), $acl)) {
         echo _BOOKLIBRARY_NOT_AUTHORIZED;
         return;
     }
     $help = array();
     foreach ($bids as $bid) {
         $lend_request = new mosBookLibrary_lend_request($database);
         if (!$lend_request->bind($_POST)) {
             echo "<script> alert('" . addslashes($lend_request->getError()) . "'); window.history.go(-1); </script>\n";
             exit;
         }
         //-----------------
         $date_format = $booklibrary_configuration['date_format'];
         if (phpversion() >= '5.3.0') {
             $date_format = str_replace('%', '', $date_format);
             $d_from = DateTime::createFromFormat($date_format, $_POST['lend_from']);
             $d_until = DateTime::createFromFormat($date_format, $_POST['lend_until']);
             if ($d_from === FALSE or $d_until === FALSE) {
                 echo "<script> alert('Bad date format'); window.history.go(-1); </script>\n";
                 exit;
             }
             $lend_request->lend_from = $d_from->format('Y-m-d');
             $lend_request->lend_until = $d_until->format('Y-m-d');
         } else {
             $lend_request->lend_from = data_transformer($_POST['lend_from'], 'to');
             $lend_request->lend_until = data_transformer($_POST['lend_until'], 'to');
         }
         //-----------------
         $lend_request->lend_request = date("Y-m-d H:i:s");
         $lend_request->fk_bookid = intval($bid);
         $query = "SELECT id FROM #__users WHERE email='" . $lend_request->user_email . "'";
         $data->setquery($query);
         $user_id = $data->loadResult();
         if (isset($user_id) || $user_id != '0') {
             $lend_request->fk_userid = intval($user_id);
         }
         $query = "SELECT * FROM #__booklibrary where id= " . $lend_request->fk_bookid;
         $data->setQuery($query);
         $bookid = $data->loadObjectList();
         $query = "SELECT * FROM #__booklibrary_lend where fk_bookid= " . $bookid[0]->id . " AND lend_return IS NULL";
         $data->setQuery($query);
         $rents = $data->loadObjectList();
         if (isset($rents[0])) {
             for ($e = 0, $m = count($rents); $e < $m; $e++) {
                 $rents[$e]->lend_from = substr($rents[$e]->lend_from, 0, 10);
                 $rents[$e]->lend_until = substr($rents[$e]->lend_until, 0, 10);
                 //cheking the rent
                 if ($lend_request->lend_from >= $rents[$e]->lend_from && $lend_request->lend_from <= $rents[$e]->lend_until || $lend_request->lend_until >= $rents[$e]->lend_from && $lend_request->lend_until <= $rents[$e]->lend_until || $lend_request->lend_from <= $rents[$e]->lend_from && $lend_request->lend_until >= $rents[$e]->lend_until) {
                     echo "<script> alert('Sorry this object is already rent out from " . $rents[$e]->lend_from . " to " . $rents[$e]->lend_until . "'); window.history.go(-1); </script>\n";
                     exit;
                 }
             }
         }
         if (!$lend_request->check()) {
             echo "<script> alert('" . addslashes($lend_request->getError()) . "'); window.history.go(-1); </script>\n";
             exit;
         }
         if (!$lend_request->store()) {
             echo "<script> alert('" . addslashes($lend_request->getError()) . "'); window.history.go(-1); </script>\n";
             exit;
         }
         $lend_request->checkin();
         array_push($help, $lend_request);
     }
     $currentcat = NULL;
     // Parameters
     //$menu = new JTableMenu( $database );//for 1.6
     //   $menu = new mosMenu( $database );
     //   $menu_name = set_header_name_bl($menu, $Itemid);
     //
     //   $menu->load( $Itemid );
     //   $params = new mosParameters( $menu->params );
     if (version_compare(JVERSION, '3.0', 'lt')) {
         $menu = new JTableMenu($database);
         // for 1.6
         $menu->load($Itemid);
         $params = new mosParameters($menu->params);
     } else {
         $menu = new JTableMenu($database);
         $menu->load($Itemid);
         $params = new JRegistry();
         $params->loadString($menu->params);
     }
     $menu_name = set_header_name_bl($menu, $Itemid);
     $params->def('header', $menu_name);
     //for 1.6
     $params->def('pageclass_sfx', '');
     //
     $params->def('show_search', '1');
     $params->def('back_button', $mainframe->getCfg('back_button'));
     $currentcat = new stdClass();
     $currentcat->descrip = _BOOKLIBRARY_LABEL_LEND_REQUEST_THANKS;
     // page image
     $currentcat->img = $mosConfig_live_site . "/components/com_booklibrary/images/book.png";
     $currentcat->header = '';
     if (@$currentcat->name != '') {
         $currentcat->header = $currentcat->name;
     } else {
         $currentcat->header = $params->get('header');
     }
     $currentcat->header = $currentcat->header;
     // used to show table rows in alternating colours
     $tabclass = array('sectiontableentry1', 'sectiontableentry2');
     //********************   begin add send mail for admin   ********
     if ($GLOBALS['lendrequest_email_show']) {
         $params->def('show_email', 1);
         if (checkAccessBL($GLOBALS['lendrequest_email_registrationlevel'], 'RECURSE', userGID_BL($my->id), $acl)) {
             $params->def('show_input_email', 1);
         }
     }
     if ($params->get('show_input_email')) {
         if (trim($GLOBALS['lendrequest_email_address']) != "") {
             $mail_to = explode(",", $GLOBALS['lendrequest_email_address']);
         }
         $userid = $my->id;
         //select user (added lend request)
         $zapros = "SELECT name, email FROM #__users WHERE id=" . $userid . ";";
         $database->setQuery($zapros);
         $item_user = $database->loadObjectList();
         echo $database->getErrorMsg();
         for ($i = 0; $i < count($bids); $i++) {
             $zapros = "SELECT id, bookid, isbn,title,owneremail FROM #__booklibrary WHERE id=" . intval($bids[$i]) . ";";
             $database->setQuery($zapros);
             $item_book = $database->loadObjectList();
             echo $database->getErrorMsg();
             if (trim($item_book[0]->owneremail) != '') {
                 $mail_to[] = $item_book[0]->owneremail;
             }
         }
         $query = "SELECT * FROM #__booklibrary WHERE id='" . $_REQUEST['bookid'] . "'";
         $database->setQuery($query);
         $book_name = $database->loadAssoc();
         if ($_REQUEST['user_name'] != "") {
             $name = $_REQUEST['user_name'];
         } else {
             $name = "anonymous";
         }
         if (count($mail_to) > 0) {
             $username = isset($item_user[0]->name) ? $item_user[0]->name : "anonymous";
         }
         $message = _BOOKLIBRARY_EMAIL_NOTIFICATION_LEND_REQUEST;
         $message = str_replace("{username}", $name, $message);
         $message = str_replace("{book_title}", $book_name['title'], $message);
         if ($userid == 0) {
             mosMail($mosConfig_mailfrom, 'anonymous', $mail_to, 'New rent request added!', $message, true);
         } else {
             mosMail($mosConfig_mailfrom, $item_user[0]->name, $mail_to, 'New rent request added!', $message, true);
         }
     }
     //********************   end add send mail for admin   ****************
     if ($option == 'com_comprofiler') {
         $link_for_mosRedirect = "index.php?option=" . $option . "&tab=getmybooksTab&Itemid=" . $Itemid;
     } else {
         $link_for_mosRedirect = "index.php?option=" . $option;
     }
     $HTML_booklibrary = new HTML_booklibrary();
     $HTML_booklibrary->showLendRequestThanks($params, $currentcat);
 }