Готовый пример плагина

Материал из Документация NGCMS

Перейти к: навигация, поиск

Сейчас расмотри на примере простого плагин. Он будет делать случайную выборку из таблицы с новостями. Он будет состоять из:

  • -random_news
    • random_news.php
    • config.php
    • version
    • -tpl
      • random_news.tpl
      • entries.tpl

Теперь поподробнее расмотрим каждый файл по отдельности. Писать плагин лучше начинать с файла version, по тому что так удобнее.

version

;
; Version description file for plugin @@ Next Generation CMS
;
 
ID: random_news
Name: Случайная выборка новостей
Version: 0.01
Acts: index
File: random_news.php
Config: config.php
Type: plugin
Description: В случайном порядке показывает новости
Author: Anonimys
Author_URI: http://ngcms.ru/
Title: Случайные новости
Information: Случайно показывает новости
Preinstall: no

random_news.php

if (!defined('NGCMS')) die ('HAL');
 
add_act('index', 'random_news');
 
function random_news() {
	global $template, $mysql, $tpl;
 
	$num = intval(extra_get_param('random_news','number'));
		if (($num < 1) || ($num > 50)) {$num = 10;}	
 
	$tpath = locatePluginTemplates(array('random_news', 'entries'), 'random_news');
 
	foreach ($mysql->select("select * from ".prefix."_news order by RAND() limit ".$num) as $row) {
		$tvars['vars'] = array(
			'link'		=>	GetLink('full', $row),
			'views'		=>	$row['views'],
			'title'		=>	$row['title']
		);
		$tpl -> template('entries', $tpath['entries']);
		$tpl -> vars('entries', $tvars);
		$v .= $tpl -> show('entries');
	}
		$tvars['vars'] = array ( 'entries' => $v);
		$tpl -> template('random_news', $tpath['random_news']);
		$tpl -> vars('random_news', $tvars);
		$output .= $tpl -> show('random_news');
		$template['vars']['rendom_news'] = $output;
}

В шаблоне random_news.tpl будет доступна переменная {entries}. Для редоктирования перменной {entries} используется шаблон entries.tpl В entries.tpl доступны следующие переменые:

  • {link} - ссылка на новость
  • {views} - количество просмотров этой новости
  • {title} - название этой новости

config.php

// Protect against hack attempts
if (!defined('NGCMS')) die ('HAL');
//
// Configuration file for plugin
//
// Preload config file
plugins_load_config();
// Fill configuration parameters
$cfg = array();
$cfgX = array();
array_push($cfgX, array('name' => 'number', 'title' => 'Количество случайных новостей', 'descr' => 'По умолчанию стоит: 10', 'type' => 'input', 'value' => intval(extra_get_param($plugin,'number'))));
array_push($cfg,  array('mode' => 'group', 'title' => 'Настройки плагина', 'entries' => $cfgX));
 
// RUN 
if ($_REQUEST['action'] == 'commit') {
       // If submit requested, do config save
       commit_plugin_config_changes($plugin, $cfg);
       print_commit_complete($plugin);
} else {
       generate_config_page($plugin, $cfg);
}

В конфиге уже задается количество выводимых новостей!