Εκδότης - API Αναφορά
Αναφορά εκδότη API
Ενότητα με τίτλο «Αναφορά εκδότη API»Πλήρης αναφορά για τις κλάσεις, τις μεθόδους, τις συναρτήσεις και τα τελικά σημεία API λειτουργικών μονάδων Publisher.
Δομή ενότητας
Ενότητα με τίτλο «Δομή ενότητας»# Οργάνωση τάξης
Ενότητα με τίτλο «# Οργάνωση τάξης»Publisher Module Classes:
├── Item / ItemHandler│ ├── Get articles│ ├── Create articles│ ├── Update articles│ └── Delete articles│├── Category / CategoryHandler│ ├── Get categories│ ├── Create categories│ ├── Update categories│ └── Delete categories│├── Comment / CommentHandler│ ├── Get comments│ ├── Create comments│ ├── Moderate comments│ └── Delete comments│└── Helper ├── Utility functions ├── Format functions └── Permission checksΚατηγορία αντικειμένου
Ενότητα με τίτλο «Κατηγορία αντικειμένου»# Επισκόπηση
Ενότητα με τίτλο «# Επισκόπηση»Η κλάση Item αντιπροσωπεύει ένα μόνο article/item στον Publisher.
Χώρος ονομάτων: XoopsModules\Publisher\
Αρχείο: modules/publisher/class/Item.php
# Κατασκευαστής
Ενότητα με τίτλο «# Κατασκευαστής»// Create new item$item = new Item();
// Get existing item$itemHandler = xoops_getModuleHandler('Item', 'publisher');$item = $itemHandler->get($itemId);# Ιδιότητες & Μέθοδοι
Ενότητα με τίτλο «# Ιδιότητες & Μέθοδοι»# Αποκτήστε ιδιότητες
Ενότητα με τίτλο «# Αποκτήστε ιδιότητες»// Get article ID$itemId = $item->getVar('itemid');$itemId = $item->id();
// Get title$title = $item->getVar('title');$title = $item->title();
// Get description$description = $item->getVar('description');$description = $item->description();
// Get body/content$body = $item->getVar('body');$body = $item->body();
// Get subtitle$subtitle = $item->getVar('subtitle');$subtitle = $item->subtitle();
// Get author$authorId = $item->getVar('uid');$authorId = $item->authorId();
// Get author name$authorName = $item->getVar('uname');$authorName = $item->uname();
// Get category$categoryId = $item->getVar('categoryid');$categoryId = $item->categoryId();
// Get status$status = $item->getVar('status');$status = $item->status();
// Get published date$date = $item->getVar('datesub');$date = $item->date();
// Get modified date$modified = $item->getVar('datemod');$modified = $item->modified();
// Get view count$views = $item->getVar('counter');$views = $item->views();
// Get image$image = $item->getVar('image');$image = $item->image();
// Get featured status$featured = $item->getVar('featured');# Ορισμός ιδιοτήτων
Ενότητα με τίτλο «# Ορισμός ιδιοτήτων»// Set title$item->setVar('title', 'New Article Title');
// Set body$item->setVar('body', '<p>Article content here</p>');
// Set description$item->setVar('description', 'Short description');
// Set category$item->setVar('categoryid', 5);
// Set status (0=draft, 1=published, etc)$item->setVar('status', 1);
// Set featured$item->setVar('featured', 1);
// Set image$item->setVar('image', 'path/to/image.jpg');# Μέθοδοι
Ενότητα με τίτλο «# Μέθοδοι»// Get formatted date$formatted = $item->date('Y-m-d H:i:s');$formatted = $item->date('l, F j, Y');
// Get item URL$url = $item->url();
// Get category URL$catUrl = $item->categoryUrl();
// Check if published$isPublished = $item->isPublished();
// Get edit URL$editUrl = $item->editUrl();
// Get delete URL$deleteUrl = $item->deleteUrl();
// Get excerpt/summary$summary = $item->getSummary(100);$summary = $item->description();
// Get all tags$tags = $item->getTags();
// Get comments$comments = $item->getComments();$commentCount = $item->getCommentCount();
// Get rating$rating = $item->getRating();
// Get rating count$ratingCount = $item->getRatingCount();Κατηγορία χειριστή αντικειμένων
Ενότητα με τίτλο «Κατηγορία χειριστή αντικειμένων»# Επισκόπηση
Ενότητα με τίτλο «# Επισκόπηση»Το ItemHandler διαχειρίζεται CRUD λειτουργίες για άρθρα.
Αρχείο: modules/publisher/class/ItemHandler.php
# Ανάκτηση αντικειμένων
Ενότητα με τίτλο «# Ανάκτηση αντικειμένων»// Get single item by ID$itemHandler = xoops_getModuleHandler('Item', 'publisher');$item = $itemHandler->get($itemId);
// Get all items$items = $itemHandler->getAll();
// Get items with conditions$criteria = new CriteriaCompo();$criteria->add(new Criteria('status', 1)); // Published only$criteria->add(new Criteria('categoryid', 5)); // Specific category$criteria->setLimit(10);$criteria->setStart(0);$items = $itemHandler->getObjects($criteria);
// Get items by category$items = $itemHandler->getByCategory($categoryId, $limit = 10);
// Get recent items$items = $itemHandler->getRecent($limit = 10);
// Get featured items$items = $itemHandler->getFeatured($limit = 5);
// Count items$total = $itemHandler->getCount($criteria);# Δημιουργία αντικειμένου
Ενότητα με τίτλο «# Δημιουργία αντικειμένου»// Create new item$item = $itemHandler->create();
// Set properties$item->setVar('title', 'Article Title');$item->setVar('body', '<p>Content</p>');$item->setVar('description', 'Short desc');$item->setVar('categoryid', 1);$item->setVar('uid', $userId);$item->setVar('status', 0); // Draft$item->setVar('datesub', time());
// Saveif ($itemHandler->insert($item)) { $itemId = $item->getVar('itemid'); echo "Article created: " . $itemId;} else { echo "Error: " . implode(', ', $item->getErrors());}# Ενημέρωση στοιχείου
Ενότητα με τίτλο «# Ενημέρωση στοιχείου»// Get item$item = $itemHandler->get($itemId);
// Modify$item->setVar('title', 'Updated Title');$item->setVar('body', '<p>Updated content</p>');$item->setVar('status', 1); // Publish
// Saveif ($itemHandler->insert($item)) { echo "Item updated";} else { echo "Error: " . implode(', ', $item->getErrors());}# Διαγραφή αντικειμένου
Ενότητα με τίτλο «# Διαγραφή αντικειμένου»// Get item$item = $itemHandler->get($itemId);
// Deleteif ($itemHandler->delete($item)) { echo "Item deleted";} else { echo "Error deleting item";}
// Delete by ID$itemHandler->deleteByPrimary($itemId);Κατηγορία Κατηγορίας
Ενότητα με τίτλο «Κατηγορία Κατηγορίας»# Επισκόπηση
Ενότητα με τίτλο «# Επισκόπηση»Η κλάση Category αντιπροσωπεύει μια κατηγορία ή μια ενότητα.
Αρχείο: modules/publisher/class/Category.php
# Μέθοδοι
Ενότητα με τίτλο «# Μέθοδοι»// Get category ID$catId = $category->getVar('categoryid');$catId = $category->id();
// Get name$name = $category->getVar('name');$name = $category->name();
// Get description$desc = $category->getVar('description');$desc = $category->description();
// Get image$image = $category->getVar('image');$image = $category->image();
// Get parent category$parentId = $category->getVar('parentid');$parentId = $category->parentId();
// Get status$status = $category->getVar('status');
// Get URL$url = $category->url();
// Get item count$count = $category->itemCount();
// Get subcategories$subs = $category->getSubCategories();
// Get parent category object$parent = $category->getParent();Κατηγορία Handler Class
Ενότητα με τίτλο «Κατηγορία Handler Class»# Επισκόπηση
Ενότητα με τίτλο «# Επισκόπηση»Το CategoryHandler διαχειρίζεται λειτουργίες της κατηγορίας CRUD.
Αρχείο: modules/publisher/class/CategoryHandler.php
# Ανάκτηση κατηγοριών
Ενότητα με τίτλο «# Ανάκτηση κατηγοριών»// Get single category$catHandler = xoops_getModuleHandler('Category', 'publisher');$category = $catHandler->get($categoryId);
// Get all categories$categories = $catHandler->getAll();
// Get root categories (no parent)$roots = $catHandler->getRoots();
// Get subcategories$subs = $catHandler->getByParent($parentId);
// Get categories with criteria$criteria = new CriteriaCompo();$criteria->add(new Criteria('status', 1));$categories = $catHandler->getObjects($criteria);# Δημιουργία κατηγορίας
Ενότητα με τίτλο «# Δημιουργία κατηγορίας»// Create new$category = $catHandler->create();
// Set values$category->setVar('name', 'News');$category->setVar('description', 'News items');$category->setVar('parentid', 0); // Root level$category->setVar('status', 1);
// Saveif ($catHandler->insert($category)) { $catId = $category->getVar('categoryid');} else { echo "Error";}# Ενημέρωση κατηγορίας
Ενότητα με τίτλο «# Ενημέρωση κατηγορίας»// Get category$category = $catHandler->get($categoryId);
// Modify$category->setVar('name', 'Updated Name');
// Save$catHandler->insert($category);# Διαγραφή κατηγορίας
Ενότητα με τίτλο «# Διαγραφή κατηγορίας»// Get category$category = $catHandler->get($categoryId);
// Delete$catHandler->delete($category);Λειτουργίες βοηθού
Ενότητα με τίτλο «Λειτουργίες βοηθού»# Βοηθητικές Λειτουργίες
Ενότητα με τίτλο «# Βοηθητικές Λειτουργίες»Η κλάση Helper παρέχει βοηθητικές λειτουργίες:
Αρχείο: modules/publisher/class/Helper.php
// Get helper instance$helper = \XoopsModules\Publisher\Helper::getInstance();
// Get module instance$module = $helper->getModule();
// Get handler$itemHandler = $helper->getHandler('Item');$catHandler = $helper->getHandler('Category');
// Get config value$editorName = $helper->getConfig('editor');$itemsPerPage = $helper->getConfig('items_per_page');
// Check permission$canView = $helper->hasPermission('view', $categoryId);$canEdit = $helper->hasPermission('edit', $itemId);$canDelete = $helper->hasPermission('delete', $itemId);$canApprove = $helper->hasPermission('approve');
// Get URL$indexUrl = $helper->url('index.php');$itemUrl = $helper->url('index.php?op=showitem&itemid=' . $itemId);
// Get base path$basePath = $helper->getPath();$templatePath = $helper->getPath('templates');# Λειτουργίες Μορφοποίησης
Ενότητα με τίτλο «# Λειτουργίες Μορφοποίησης»// Format date$formatted = $helper->formatDate($timestamp, 'Y-m-d');
// Truncate text$excerpt = $helper->truncate($text, $length = 100);
// Sanitize input$clean = $helper->sanitize($input);
// Prepare output$output = $helper->prepare($data);
// Get breadcrumb$breadcrumb = $helper->getBreadcrumb($itemId);JavaScript API
Ενότητα με τίτλο «JavaScript API»# Λειτουργίες JavaScript Frontend
Ενότητα με τίτλο «# Λειτουργίες JavaScript Frontend»Ο εκδότης περιλαμβάνει JavaScript API για αλληλεπιδράσεις διεπαφής:
// Include Publisher JS library<script src="/modules/publisher/assets/js/publisher.js"></script>
// Check if Publisher object existsif (typeof Publisher !== 'undefined') { // Use Publisher API}
// Get article datavar item = Publisher.getItem(itemId);console.log(item.title);console.log(item.url);
// Get category datavar category = Publisher.getCategory(categoryId);console.log(category.name);
// Submit ratingPublisher.submitRating(itemId, rating, function(response) { console.log('Rating saved');});
// Load more articlesPublisher.loadMore(categoryId, page, limit, function(articles) { // Handle loaded articles});
// Search articlesPublisher.search(query, function(results) { // Handle search results});# Τελικά σημεία του Άγιαξ
Ενότητα με τίτλο «# Τελικά σημεία του Άγιαξ»Ο Publisher παρέχει AJAX τελικά σημεία για αλληλεπιδράσεις διεπαφής:
// Get article via AJAXfetch('/modules/publisher/ajax.php?op=getItem&itemid=' + itemId) .then(response => response.json()) .then(data => console.log(data));
// Submit comment via AJAXfetch('/modules/publisher/ajax.php', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'op=addComment&itemid=' + itemId + '&text=' + comment}).then(response => response.json()).then(data => console.log(data));
// Get ratingsfetch('/modules/publisher/ajax.php?op=getRatings&itemid=' + itemId) .then(response => response.json()) .then(data => console.log(data));REST API (Εάν είναι ενεργοποιημένο)
Ενότητα με τίτλο «REST API (Εάν είναι ενεργοποιημένο)»# API Τελικά σημεία
Ενότητα με τίτλο «# API Τελικά σημεία»Εάν ο Εκδότης αποκαλύψει το REST API:
GET /modules/publisher/api/itemsGET /modules/publisher/api/items/{id}GET /modules/publisher/api/categoriesGET /modules/publisher/api/categories/{id}POST /modules/publisher/api/itemsPUT /modules/publisher/api/items/{id}DELETE /modules/publisher/api/items/{id}# Παράδειγμα API κλήσεις
Ενότητα με τίτλο «# Παράδειγμα API κλήσεις»// Get items via REST$url = 'http://example.com/modules/publisher/api/items';$response = file_get_contents($url);$items = json_decode($response, true);
// Get single item$url = 'http://example.com/modules/publisher/api/items/1';$response = file_get_contents($url);$item = json_decode($response, true);
// Create item$url = 'http://example.com/modules/publisher/api/items';$data = array( 'title' => 'New Article', 'body' => 'Content here', 'categoryid' => 1);$options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/json', 'content' => json_encode($data) ));$response = file_get_contents($url, false, stream_context_create($options));Σχήμα βάσης δεδομένων
Ενότητα με τίτλο «Σχήμα βάσης δεδομένων»# Πίνακες
Ενότητα με τίτλο «# Πίνακες»# εκδοτών_κατηγορίες
Ενότητα με τίτλο «# εκδοτών_κατηγορίες»- categoryid (PK)- name- description- image- parentid (FK)- status- created- modified# εκδότης_στοιχεία
Ενότητα με τίτλο «# εκδότης_στοιχεία»- itemid (PK)- categoryid (FK)- uid (FK to users)- title- subtitle- description- body- image- status- featured- datesub- datemod- counter (views)# εκδότης_σχόλια
Ενότητα με τίτλο «# εκδότης_σχόλια»- commentid (PK)- itemid (FK)- uid (FK)- comment- datesub- approved# εκδότης_αρχεία
Ενότητα με τίτλο «# εκδότης_αρχεία»- fileid (PK)- itemid (FK)- filename- description- uploadedΕκδηλώσεις & Άγκιστρα
Ενότητα με τίτλο «Εκδηλώσεις & Άγκιστρα»# Εκδηλώσεις εκδότη
Ενότητα με τίτλο «# Εκδηλώσεις εκδότη»// Item created event$modHandler = xoops_getHandler('module');$modHandler->activateModule('publisher');$publisher = xoops_getModuleHandler('Item', 'publisher');xoops_events()->trigger( 'publisher.item.created', array('item' => $item));
// Item updatedxoops_events()->trigger( 'publisher.item.updated', array('item' => $item));
// Item deletedxoops_events()->trigger( 'publisher.item.deleted', array('itemid' => $itemId));
// Article commentedxoops_events()->trigger( 'publisher.comment.added', array('comment' => $comment));# Ακούστε Εκδηλώσεις
Ενότητα με τίτλο «# Ακούστε Εκδηλώσεις»// Register event listenerxoops_events()->attach( 'publisher.item.created', array($myClass, 'onItemCreated'));
// Or in pluginpublic function onItemCreated($item) { // Handle item creation}Παραδείγματα κώδικα
Ενότητα με τίτλο «Παραδείγματα κώδικα»# Λάβετε πρόσφατα άρθρα
Ενότητα με τίτλο «# Λάβετε πρόσφατα άρθρα»<?php// Get recent published articles$itemHandler = xoops_getModuleHandler('Item', 'publisher');$criteria = new CriteriaCompo();$criteria->add(new Criteria('status', 1)); // Published$criteria->setSort('datesub');$criteria->setOrder('DESC');$criteria->setLimit(5);
$items = $itemHandler->getObjects($criteria);
foreach ($items as $item) { echo $item->title() . "\n"; echo $item->date('Y-m-d') . "\n"; echo $item->description() . "\n"; echo "<a href='" . $item->url() . "'>Read More</a>\n\n";}?># Δημιουργία άρθρου μέσω προγραμματισμού
Ενότητα με τίτλο «# Δημιουργία άρθρου μέσω προγραμματισμού»<?php// Create article$itemHandler = xoops_getModuleHandler('Item', 'publisher');$item = $itemHandler->create();
$item->setVar('title', 'Programmatic Article');$item->setVar('description', 'Created via API');$item->setVar('body', '<p>Full content here</p>');$item->setVar('categoryid', 1);$item->setVar('uid', 1);$item->setVar('status', 1); // Published$item->setVar('datesub', time());
if ($itemHandler->insert($item)) { echo "Article created: " . $item->getVar('itemid');} else { echo "Error: " . implode(', ', $item->getErrors());}?># Λάβετε άρθρα ανά κατηγορία
Ενότητα με τίτλο «# Λάβετε άρθρα ανά κατηγορία»<?php// Get category articles$catId = 5;$itemHandler = xoops_getModuleHandler('Item', 'publisher');$items = $itemHandler->getByCategory($catId, $limit = 10);
echo "Articles in category " . $catId . ":\n";foreach ($items as $item) { echo "- " . $item->title() . "\n";}?># Ενημέρωση κατάστασης άρθρου
Ενότητα με τίτλο «# Ενημέρωση κατάστασης άρθρου»<?php// Change article status$itemHandler = xoops_getModuleHandler('Item', 'publisher');$item = $itemHandler->get($itemId);
if ($item) { $item->setVar('status', 1); // Publish
if ($itemHandler->insert($item)) { echo "Article published"; } else { echo "Error publishing article"; }} else { echo "Article not found";}?># Λάβετε Δέντρο κατηγορίας
Ενότητα με τίτλο «# Λάβετε Δέντρο κατηγορίας»<?php// Build category tree$catHandler = xoops_getModuleHandler('Category', 'publisher');$roots = $catHandler->getRoots();
function displayTree($category, $level = 0) { echo str_repeat(" ", $level) . $category->name() . "\n";
$subs = $category->getSubCategories(); foreach ($subs as $sub) { displayTree($sub, $level + 1); }}
foreach ($roots as $root) { displayTree($root);}?>Χειρισμός σφαλμάτων
Ενότητα με τίτλο «Χειρισμός σφαλμάτων»# Χειρισμός σφαλμάτων
Ενότητα με τίτλο «# Χειρισμός σφαλμάτων»<?php// Try/catch error handlingtry { $itemHandler = xoops_getModuleHandler('Item', 'publisher'); $item = $itemHandler->get($itemId);
if (!$item) { throw new Exception('Item not found'); }
$item->setVar('title', 'New Title');
if (!$itemHandler->insert($item)) { throw new Exception('Failed to save item'); }} catch (Exception $e) { error_log('Publisher Error: ' . $e->getMessage()); // Handle error}?># Λάβετε μηνύματα σφάλματος
Ενότητα με τίτλο «# Λάβετε μηνύματα σφάλματος»<?php// Get error messages from object$item = $itemHandler->create();// ... set variables ...
if (!$itemHandler->insert($item)) { $errors = $item->getErrors(); foreach ($errors as $error) { echo "Error: " . $error . "\n"; }}?>Σχετική τεκμηρίωση
Ενότητα με τίτλο «Σχετική τεκμηρίωση»- Γάντζοι και εκδηλώσεις
- Προσαρμοσμένα πρότυπα
- Ανάλυση ενότητας εκδότη
- Πρότυπα και μπλοκ στον Publisher
- Δημιουργία άρθρου
- Διαχείριση Κατηγορίας