/** * @return int * @param Category $o_category * @desc Save the supplied Category to the database, and return the id */ function Save($o_category) { # check parameters if (!$o_category instanceof Category) { die('Unable to save category'); } # build query # if no id, it's a new category; otherwise update the category if ($o_category->GetId()) { $s_sql = 'UPDATE ' . $this->GetSettings()->GetTable('Category') . ' SET ' . 'parent = ' . Sql::ProtectNumeric($o_category->GetParentId(), true) . ', ' . "name = " . Sql::ProtectString($this->GetDataConnection(), $o_category->GetName()) . ", " . "code = " . Sql::ProtectString($this->GetDataConnection(), $o_category->GetUrl()) . ", " . 'sort_override = ' . Sql::ProtectNumeric($o_category->GetSortOverride()) . ', ' . 'date_changed = ' . gmdate('U') . ' ' . 'WHERE id = ' . Sql::ProtectNumeric($o_category->GetId()); # run query $this->GetDataConnection()->query($s_sql); } else { $s_sql = 'INSERT INTO ' . $this->GetSettings()->GetTable('Category') . ' SET ' . 'parent = ' . Sql::ProtectNumeric($o_category->GetParentId(), true) . ', ' . "name = " . Sql::ProtectString($this->GetDataConnection(), $o_category->GetName()) . ", " . "code = " . Sql::ProtectString($this->GetDataConnection(), $o_category->GetUrl()) . ", " . 'sort_override = ' . Sql::ProtectNumeric($o_category->GetSortOverride()) . ', ' . 'date_added = ' . gmdate('U') . ', ' . 'date_changed = ' . gmdate('U'); # run query $o_result = $this->GetDataConnection()->query($s_sql); # get autonumber $o_category->SetId($this->GetDataConnection()->insertID()); } return $o_category->GetId(); }
public static function Add($conn, $params) { //peredelat Add nehvatilo vremeni ... $q = "SELECT id FROM books WHERE books.name = '{$params['book_name']}'"; $res = $conn->query($q); $zzz = $res->fetch_row(); if (count($zzz)) { echo "Book already exist in database"; return; } if ($params['book_name'] == "") { echo "Empty book name"; return; } if (Category::GetId($conn, $params['category']) < 0) { $q = "INSERT INTO Categories (name) VALUES ('{$params['category']}')"; $conn->query($q); } if (Publisher::GetId($conn, $params['publisher_name']) < 0) { $q = "INSERT INTO Publishers (name) VALUES ('{$params['publisher_name']}')"; $conn->query($q); } if (Author::GetId($conn, $params['author_name'], $params['author_surname'], $params['author_birthdate']) < 0) { $q = "INSERT INTO Authors (name, surname, birthdate) " . "VALUES ('{$params['author_name']}', '{$params['author_surname']}', '{$params['author_birthdate']}')"; $conn->query($q); } $q = "INSERT INTO Books (name, pages, date, category_id, publisher_id) " . "VALUES ('{$params['book_name']}', '{$params['pages']}', '{$params['date']}', " . "(SELECT id FROM Categories WHERE Categories.name = '{$params['category']}'), " . "(SELECT id FROM Publishers WHERE Publishers.name = '{$params['publisher_name']}'))"; //echo $q; $conn->query($q); $q = "INSERT INTO BooksAuthors (book_id, author_id) VALUES (" . "(SELECT id FROM Books WHERE Books.name = '{$params['book_name']}'), " . "(SELECT id FROM Authors WHERE Authors.name = '{$params['author_name']}' AND " . "Authors.surname = '{$params['author_surname']}' AND " . "Authors.birthdate = '{$params['author_birthdate']}'))"; //echo $q; $conn->query($q); echo "Book was added in the database "; //echo Category::GetId($conn, $params['category']); //echo Publisher::GetId($conn, $params['publisher_name']); //echo Author::GetId($conn, $params['author_name'], $params['author_surname'], $params['author_birthdate']); }
private function CreateItem(Category $category) { $o_link = new XhtmlElement('a', Html::Encode($category->GetName())); $o_link->AddAttribute('href', 'categoryedit.php?item=' . $category->GetId()); return new XhtmlElement('li', $o_link); }
/** * @return Category[] * @param Category $o_parent_category * @param int $i_number_of_levels * @desc Get an array of Categories which are children of the provided category */ function GetChildren($o_parent_category, $i_number_of_levels = 1) { $b_start_flag = false; $b_finished_flag = false; $b_collect_flag = false; $a_child_categories = array(); if (is_array($this->a_items) and $o_parent_category instanceof Category) { foreach ($this->a_items as $o_category) { # finish point is another category at same level as root if ($b_start_flag and $o_category->GetHierarchyLevel() <= $o_parent_category->GetHierarchyLevel() and $o_category->GetId() != $o_parent_category->GetId()) { $b_finished_flag = true; } # don't collect too deep if ($o_category->GetHierarchyLevel() > $o_parent_category->GetHierarchyLevel() + $i_number_of_levels) { $b_collect_flag = false; } # identify category to collect if ($o_category->GetHierarchyLevel() > $o_parent_category->GetHierarchyLevel() and $o_category->GetHierarchyLevel() <= $o_parent_category->GetHierarchyLevel() + $i_number_of_levels) { $b_collect_flag = true; } # collect category if ($b_start_flag and $b_collect_flag and !$b_finished_flag) { $a_child_categories[] = $o_category; } # find category to start at if ($o_category->GetId() == $o_parent_category->GetId()) { $b_start_flag = true; } } } return $a_child_categories; }