Ejemplo n.º 1
0
/**
 * Compiles information to add or edit books
 * @param integer bid The unique id of the record to edit (0 if new)
 * @param array option the current options
 */
function editBook($option, $bid)
{
    global $mosConfig_absolute_path;
    global $database, $my, $mosConfig_live_site, $booklibrary_configuration;
    $book = new mosBookLibrary($database);
    // load the row from the db table
    $book->load(intval($bid));
    $numeric_bookids = array();
    if (empty($book->bookid) && $booklibrary_configuration['bookid']['auto-increment']['boolean'] == 1) {
        $database->setQuery("select bookid from #__booklibrary order by bookid");
        $bookids = $database->loadObjectList();
        foreach ($bookids as $bookid) {
            if (!is_numeric($bookid->bookid)) {
                echo "<script> alert('You have no numeric BookId. Please set option  " . _BOOKLIBRARY_ADMIN_CONFIG_BOOKID_AUTO_INCREMENT . " to \\'No\\' or change all BookID to numeric '); window.history.go(-1); </script>\n";
                exit;
            }
            $numeric_bookids[] = intval($bookid->bookid);
        }
        if (count($bookids) > 0) {
            sort($numeric_bookids);
            $book->bookid = $numeric_bookids[count($numeric_bookids) - 1] + 1;
        } else {
            $book->bookid = 1;
        }
    }
    // get list of categories
    $categories[] = mosHTML::makeOption('0', _BOOKLIBRARY_LABEL_SELECT_CATEGORIES);
    $query = "SELECT  id ,name, parent_id as parent" . "\n FROM #__booklibrary_main_categories" . "\n WHERE section='com_booklibrary'" . "\n AND published > 0" . "\n ORDER BY parent_id, ordering";
    $database->setQuery($query);
    $rows = $database->loadObjectList();
    // establish the hierarchy of the categories
    $children = array();
    // first pass - collect children
    foreach ($rows as $v) {
        $pt = $v->parent;
        $list = @$children[$pt] ? $children[$pt] : array();
        array_push($list, $v);
        $children[$pt] = $list;
    }
    // second pass - get an indent list of the items
    $list = bookLibraryTreeRecurse(0, '', array(), $children);
    foreach ($list as $i => $item) {
        $item->text = $item->treename;
        $item->value = $item->id;
        $list[$i] = $item;
    }
    $categories = array_merge($categories, $list);
    if (count($categories) <= 1) {
        mosRedirect("index.php?option=com_booklibrary&section=categories", _BOOKLIBRARY_ADMIN_IMPEXP_ADD);
    }
    $query = "select catid from #__booklibrary_categories where bookid='" . $book->id . "'";
    $database->setQuery($query);
    //$cat_idlist = $database->loadResultArray();
    if (version_compare(JVERSION, '3.0', 'lt')) {
        $cat_idlist = $database->loadResultArray();
    } else {
        $cat_idlist = $database->loadColumn();
    }
    if (empty($cat_idlist)) {
        $cat_idlist[0] = '0';
    }
    $clist = mosHTML::selectList($categories, 'catid[]', 'class="inputbox"', 'value', 'text', $cat_idlist);
    // get list of WS
    $retVal = mosBooklibraryWS::getArray();
    $ws = null;
    for ($i = 0, $n = count($retVal); $i < $n; $i++) {
        $help = $retVal[$i];
        $ws[] = mosHTML::makeOption($help[0], $help[1]);
    }
    if ($bid == 0) {
        $wslist = mosHTML::selectList($ws, 'informationFrom', 'class="inputbox" size="1"', 'value', 'text', intval($booklibrary_configuration['editbook']['default']['host']));
    } else {
        $wslist = mosHTML::selectList($ws, 'informationFrom', 'class="inputbox" size="1"', 'value', 'text', intval($book->informationFrom));
    }
    //get language List
    $retVal1 = mosBooklibraryOthers::getLanguageArray();
    $lang = null;
    for ($i = 0, $n = count($retVal1); $i < $n; $i++) {
        $help = $retVal1[$i];
        $lang[] = mosHTML::makeOption($help[0], $help[1]);
    }
    if ($bid == 0) {
        $langlist = mosHTML::selectList($lang, 'language', 'class="inputbox" size="1"', 'value', 'text', $booklibrary_configuration['editbook']['default']['lang']);
        $langlistshow = mosHTML::selectList($lang, 'langshow', 'class="inputbox" size="1"', 'value', 'text', '1');
    } else {
        $langlist = mosHTML::selectList($lang, 'language', 'class="inputbox" size="1"', 'value', 'text', $book->language);
        $langlistshow = mosHTML::selectList($lang, 'langshow', 'class="inputbox" size="1"', 'value', 'text', $book->langshow);
    }
    //get Rating
    $retVal2 = mosBooklibraryOthers::getRatingArray();
    $rating = null;
    for ($i = 0, $n = count($retVal2); $i < $n; $i++) {
        $help = $retVal2[$i];
        $rating[] = mosHTML::makeOption($help[0], $help[1]);
    }
    $ratinglist = mosHTML::selectList($rating, 'rating', 'class="inputbox" size="1"', 'value', 'text', $book->rating);
    if (!empty($book->id)) {
        //check ebook file
        $db = JFactory::getDBO();
        $db->setQuery("SELECT * FROM #__booklibrary_files WHERE fk_book_id=" . $book->id);
        $files = $db->loadObjectList();
    } else {
        $files = array();
    }
    if ($book->checked_out && $book->checked_out != $my->id) {
        mosRedirect("index.php?option={$option}", _BOOKLIBRARY_IS_EDITED);
    }
    // fail if checked out not by 'me'
    if ($book->checked_out && $book->checked_out != $my->id) {
        mosRedirect("index.php?option={$option}", _BOOKLIBRARY_IS_EDITED);
    }
    if ($bid) {
        $book->checkout($my->id);
    } else {
        // initialise new record
        $book->published = 0;
        $book->approved = 0;
    }
    if ($book->owneremail == '' || $bid == 0) {
        $book->owneremail = $my->email;
    }
    $database->setQuery("SELECT username AS owner FROM #__users" . "\nWHERE email='{$book->owneremail}'");
    $book->owner = $database->loadResult();
    //*****************************   begin for reviews **************************/
    $database->setQuery("select a.*, b.name from #__booklibrary_review a, #__users b" . " WHERE a.fk_userid = b.id and a.fk_bookid=" . $bid . " ORDER BY date ;");
    $reviews1 = $database->loadObjectList();
    //take review for anonymous users
    $database->setQuery("select a.*, 'anonymous' as name from #__booklibrary_review as a  " . " WHERE a.fk_userid = 0 and a.fk_bookid = " . $bid . " ORDER BY date ;");
    $reviews2 = $database->loadObjectList();
    $reviews = array_merge($reviews1, $reviews2);
    //**********************   end for reviews   *****************************/
    HTML_booklibrary::editBook($option, $book, $clist, $wslist, $langlist, $langlistshow, $ratinglist, $delete_ebook, $reviews, $files);
}