ผู้จัดพิมพ์ - API ข้อมูลอ้างอิง
ผู้จัดพิมพ์ API ข้อมูลอ้างอิง
หัวข้อที่มีชื่อว่า “ผู้จัดพิมพ์ API ข้อมูลอ้างอิง”การอ้างอิงที่สมบูรณ์สำหรับคลาสโมดูล Publisher วิธีการ ฟังก์ชัน และจุดสิ้นสุด API
โครงสร้างโมดูล
หัวข้อที่มีชื่อว่า “โครงสร้างโมดูล”การจัดชั้นเรียน
หัวข้อที่มีชื่อว่า “การจัดชั้นเรียน”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 คลาสแสดงถึงบทความ/รายการเดียวใน 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
หัวข้อที่มีชื่อว่า “คลาส ItemHandler”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();คลาส CategoryHandler
หัวข้อที่มีชื่อว่า “คลาส CategoryHandler”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 ส่วนหน้า
หัวข้อที่มีชื่อว่า “ฟังก์ชัน JavaScript ส่วนหน้า”ผู้เผยแพร่มี 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});จุดสิ้นสุดอาแจ็กซ์
หัวข้อที่มีชื่อว่า “จุดสิ้นสุดอาแจ็กซ์”ผู้เผยแพร่จัดเตรียมปลายทาง 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ผู้เผยแพร่_items
หัวข้อที่มีชื่อว่า “ผู้เผยแพร่_items”- 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
- การสร้างบทความ
- การจัดการหมวดหมู่
แหล่งข้อมูล
หัวข้อที่มีชื่อว่า “แหล่งข้อมูล”#publisher #api #reference #code #classes #methods #xoops