PageRenderTime 82ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/kernel.templates.php

http://litepublisher.googlecode.com/
PHP | 1969 lines | 1952 code | 10 blank | 7 comment | 7 complexity | 16220a93ea9856fe47f9f1252efe081b MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Lite Publisher
  4. * Copyright (C) 2010, 2011, 2012, 2013 Vladimir Yushko http://litepublisher.com/
  5. * Dual licensed under the MIT (mit.txt)
  6. * and GPL (gpl.txt) licenses.
  7. **/
  8. //local.class.php
  9. class tlocal {
  10. public static $self;
  11. public $loaded;
  12. public $ini;
  13. public $section;
  14. public $searchsect;
  15. public static function i($section = '') {
  16. if (!isset(self::$self)) {
  17. self::$self= getinstance(__class__);
  18. self::$self->loadfile('default');
  19. }
  20. if ($section != '') self::$self->section = $section;
  21. return self::$self;
  22. }
  23. public static function admin($section = '') {
  24. $result = self::i($section);
  25. $result->check('admin');
  26. return $result;
  27. }
  28. public function __construct() {
  29. $this->ini = array();
  30. $this->loaded = array();
  31. $this->searchsect = array('common', 'default');
  32. }
  33. public static function get($section, $key) {
  34. return self::i()->ini[$section][$key];
  35. }
  36. public function __get($name) {
  37. if (isset($this->ini[$this->section][$name])) return $this->ini[$this->section][$name];
  38. foreach ($this->searchsect as $section) {
  39. if (isset($this->ini[$section][$name])) return $this->ini[$section][$name];
  40. }
  41. return '';
  42. }
  43. public function __isset($name) {
  44. if (isset($this->ini[$this->section][$name])) return true;
  45. foreach ($this->searchsect as $section) {
  46. if (isset($this->ini[$section][$name])) return true;
  47. }
  48. return false;
  49. }
  50. public function __call($name, $args) {
  51. return strtr ($this->__get($name), $args->data);
  52. }
  53. public function addsearch() {
  54. $this->joinsearch(func_get_args());
  55. }
  56. public function joinsearch(array $a) {
  57. foreach ($a as $sect) {
  58. $sect = trim(trim($sect), "\"',;:.");
  59. if (!in_array($sect, $this->searchsect)) $this->searchsect[] = $sect;
  60. }
  61. }
  62. public function firstsearch() {
  63. $a = array_reverse(func_get_args());
  64. foreach ($a as $sect) {
  65. $i = array_search($sect, $this->searchsect);
  66. if ($i !== false) array_splice($this->searchsect, $i, 1);
  67. array_unshift($this->searchsect, $sect);
  68. }
  69. }
  70. public static function date($date, $format = '') {
  71. if (empty($format)) $format = self::i()->getdateformat();
  72. return self::i()->translate(date($format, $date), 'datetime');
  73. }
  74. public function getdateformat() {
  75. $format = litepublisher::$options->dateformat;
  76. return $format != ''? $format : $this->ini['datetime']['dateformat'];
  77. }
  78. public function translate($s, $section = 'default') {
  79. return strtr($s, $this->ini[$section]);
  80. }
  81. public function check($name) {
  82. if ($name == '') $name = 'default';
  83. if (!in_array($name, $this->loaded)) $this->loadfile($name);
  84. }
  85. public function loadfile($name) {
  86. $this->loaded[] = $name;
  87. $filename = self::getcachedir() . $name;
  88. if (tfilestorage::loadvar($filename, $v) && is_array($v)) {
  89. $this->ini = $v + $this->ini ;
  90. if (isset($v['searchsect'])) $this->joinsearch($v['searchsect']);
  91. } else {
  92. $merger = tlocalmerger::i();
  93. $merger->parse($name);
  94. }
  95. }
  96. public static function usefile($name) {
  97. self::i()->check($name);
  98. return self::$self;
  99. }
  100. public static function inifile($class, $filename) {
  101. return self::inicache(litepublisher::$classes->getresourcedir($class) . litepublisher::$options->language . $filename);
  102. }
  103. public static function inicache($filename) {
  104. $self = self::i();
  105. if (!isset(ttheme::$inifiles[$filename])) {
  106. $ini = ttheme::cacheini($filename);
  107. if (is_array($ini)) {
  108. $self->ini = $ini + $self->ini ;
  109. if (isset($ini['searchsect'])) $self->joinsearch($ini['searchsect']);
  110. $keys = array_keys($ini);
  111. $self->section = array_shift($keys);
  112. $self->addsearch($self->section);
  113. }
  114. }
  115. return $self;
  116. }
  117. //backward
  118. public static function loadlang($name) {
  119. self::usefile($name);
  120. }
  121. public static function getcachedir() {
  122. return litepublisher::$paths->data . 'languages' . DIRECTORY_SEPARATOR;
  123. }
  124. public static function clearcache() {
  125. tfiler::delete(self::getcachedir(), false, false);
  126. self::i()->loaded = array();
  127. }
  128. }//class
  129. class tdateformater {
  130. public $date;
  131. public function __construct($date) { $this->date = $date; }
  132. public function __get($name) { return tlocal::translate(date($name, $this->date), 'datetime'); }
  133. }
  134. //views.class.php
  135. class tview extends titem_storage {
  136. public $sidebars;
  137. protected $themeinstance;
  138. public static function i($id = 1) {
  139. if ($id == 1) {
  140. $class = __class__;
  141. } else {
  142. $views = tviews::i();
  143. $class = $views->itemexists($id) ? $views->items[$id]['class'] : __class__;
  144. }
  145. return parent::iteminstance($class, $id);
  146. }
  147. public static function getinstancename() {
  148. return 'view';
  149. }
  150. public static function getview($instance) {
  151. $id = $instance->getidview();
  152. if (isset(self::$instances['view'][$id])) return self::$instances['view'][$id];
  153. $views = tviews::i();
  154. if (!$views->itemexists($id)) {
  155. $id = 1; //default, wich always exists
  156. $instance->setidview($id);
  157. }
  158. return self::i($id);
  159. }
  160. protected function create() {
  161. parent::create();
  162. $this->data = array(
  163. 'id' => 0,
  164. 'class' => get_class($this),
  165. 'name' => 'default',
  166. 'themename' => 'default',
  167. 'menuclass' => 'tmenus',
  168. 'hovermenu' => true,
  169. 'customsidebar' => false,
  170. 'disableajax' => false,
  171. 'custom' => array(),
  172. 'sidebars' => array()
  173. );
  174. $this->sidebars = &$this->data['sidebars'];
  175. $this->themeinstance = null;
  176. }
  177. public function __destruct() {
  178. unset($this->themeinstance);
  179. parent::__destruct();
  180. }
  181. public function getowner() {
  182. return tviews::i() ;
  183. }
  184. public function load() {
  185. if (parent::load()) {
  186. $this->sidebars = &$this->data['sidebars'];
  187. return true;
  188. }
  189. return false;
  190. }
  191. protected function get_theme_instance($name) {
  192. return ttheme::getinstance($name);
  193. }
  194. public function setthemename($name) {
  195. if ($name != $this->themename) {
  196. if (!ttheme::exists($name)) return $this->error(sprintf('Theme %s not exists', $name));
  197. $this->data['themename'] = $name;
  198. $this->themeinstance = $this->get_theme_instance($name);
  199. $this->data['custom'] = $this->themeinstance->templates['custom'];
  200. $this->save();
  201. tviews::i()->themechanged($this);
  202. }
  203. }
  204. public function gettheme() {
  205. if (isset($this->themeinstance)) return $this->themeinstance;
  206. if (ttheme::exists($this->themename)) {
  207. $this->themeinstance = $this->get_theme_instance($this->themename);
  208. if (count($this->data['custom']) == count($this->themeinstance->templates['custom'])) {
  209. $this->themeinstance->templates['custom'] = $this->data['custom'];
  210. } else {
  211. $this->data['custom'] = $this->themeinstance->templates['custom'];
  212. $this->save();
  213. }
  214. } else {
  215. $this->setthemename('default');
  216. }
  217. return $this->themeinstance;
  218. }
  219. public function setcustomsidebar($value) {
  220. if ($value != $this->customsidebar) {
  221. if ($this->id == 1) return false;
  222. if ($value) {
  223. $default = tview::i(1);
  224. $this->sidebars = $default->sidebars;
  225. } else {
  226. $this->sidebars = array();
  227. }
  228. $this->data['customsidebar'] = $value;
  229. $this->save();
  230. }
  231. }
  232. }//class
  233. class tviews extends titems_storage {
  234. public $defaults;
  235. public static function i() {
  236. return getinstance(__class__);
  237. }
  238. protected function create() {
  239. $this->dbversion = false;
  240. parent::create();
  241. $this->basename = 'views';
  242. $this->addevents('themechanged');
  243. $this->addmap('defaults', array());
  244. }
  245. public function add($name) {
  246. $this->lock();
  247. $id = ++$this->autoid;
  248. $view = litepublisher::$classes->newitem(tview::getinstancename(), 'tview', $id);
  249. $view->id = $id;
  250. $view->name = $name;
  251. $view->data['class'] = get_class($view);
  252. $this->items[$id] = &$view->data;
  253. $this->unlock();
  254. return $id;
  255. }
  256. public function addview(tview $view) {
  257. $this->lock();
  258. $id = ++$this->autoid;
  259. $view->id = $id;
  260. if ($view->name == '') $view->name = 'view_' . $id;
  261. $view->data['class'] = get_class($view);
  262. $this->items[$id] = &$view->data;
  263. $this->unlock();
  264. return $id;
  265. }
  266. public function delete($id) {
  267. if ($id == 1) return $this->error('You cant delete default view');
  268. foreach ($this->defaults as $name => $iddefault) {
  269. if ($id == $iddefault) $this->defaults[$name] = 1;
  270. }
  271. return parent::delete($id);
  272. }
  273. public function get($name) {
  274. foreach ($this->items as $id => $item) {
  275. if ($name == $item['name']) return tview::i($id);
  276. }
  277. return false;
  278. }
  279. public function widgetdeleted($idwidget) {
  280. $deleted = false;
  281. foreach ($this->items as &$viewitem) {
  282. unset($sidebar);
  283. foreach ($viewitem['sidebars'] as &$sidebar) {
  284. for ($i = count($sidebar) - 1; $i >= 0; $i--) {
  285. if ($idwidget == $sidebar[$i]['id']) {
  286. array_delete($sidebar, $i);
  287. $deleted = true;
  288. }
  289. }
  290. }
  291. }
  292. if ($deleted) $this->save();
  293. }
  294. }//class
  295. class tevents_itemplate extends tevents {
  296. protected function create() {
  297. parent::create();
  298. $this->data['idview'] = 1;
  299. }
  300. public function gethead() {}
  301. public function getkeywords() {}
  302. public function getdescription() {}
  303. public function getidview() {
  304. return $this->data['idview'];
  305. }
  306. public function setidview($id) {
  307. if ($id != $this->idview) {
  308. $this->data['idview'] = $id;
  309. $this->save();
  310. }
  311. }
  312. public function getview() {
  313. return tview::getview($this);
  314. }
  315. }//class
  316. class titems_itemplate extends titems {
  317. protected function create() {
  318. parent::create();
  319. $this->data['idview'] = 1;
  320. $this->data['keywords'] = '';
  321. $this->data['description'] = '';
  322. $this->data['head'] = '';
  323. }
  324. public function gethead() {
  325. return $this->data['head'];
  326. }
  327. public function getkeywords() {
  328. return $this->data['keywords'];
  329. }
  330. public function getdescription() {
  331. return $this->data['description'];
  332. }
  333. public function getidview() {
  334. return $this->data['idview'];
  335. }
  336. public function setidview($id) {
  337. if ($id != $this->data['idview']) {
  338. $this->data['idview'] = $id;
  339. $this->save();
  340. }
  341. }
  342. public function getview() {
  343. return tview::getview($this);
  344. }
  345. }//class
  346. //template.class.php
  347. class ttemplate extends tevents_storage {
  348. public $path;
  349. public $url;
  350. public $context;
  351. public $itemplate;
  352. public $view;
  353. public $ltoptions;
  354. public $hover;
  355. //public $footer;
  356. public static function i() {
  357. return getinstance(__class__);
  358. }
  359. protected function create() {
  360. //prevent recursion
  361. litepublisher::$classes->instances[__class__] = $this;
  362. parent::create();
  363. $this->basename = 'template' ;
  364. $this->addevents('beforecontent', 'aftercontent', 'onhead', 'onrequest', 'ontitle', 'ongetmenu');
  365. $this->path = litepublisher::$paths->themes . 'default' . DIRECTORY_SEPARATOR ;
  366. $this->url = litepublisher::$site->files . '/themes/default';
  367. $this->itemplate = false;
  368. $this->ltoptions = array(
  369. 'url' => litepublisher::$site->url,
  370. 'files' =>litepublisher::$site->files,
  371. 'idurl' => litepublisher::$urlmap->itemrequested['id'],
  372. 'jqueryui_version' => litepublisher::$site->jqueryui_version,
  373. 'lang' => litepublisher::$site->language,
  374. 'video_width' => litepublisher::$site->video_width,
  375. 'video_height' => litepublisher::$site->video_height,
  376. 'theme' => array(),
  377. );
  378. $this->hover = true;
  379. $this->data['heads'] = '';
  380. $this->data['js'] = '<script type="text/javascript" src="%s"></script>';
  381. $this->data['jsready'] = '<script type="text/javascript">$(document).ready(function() {%s});</script>';
  382. $this->data['jsload'] = '<script type="text/javascript">$.load_script(%s);</script>';
  383. $this->data['footer']= '<a href="http://litepublisher.com/">Powered by Lite Publisher</a>';
  384. $this->data['tags'] = array();
  385. }
  386. public function __get($name) {
  387. if (method_exists($this, $get = 'get' . $name)) return $this->$get();
  388. if (array_key_exists($name, $this->data)) return $this->data[$name];
  389. if (preg_match('/^sidebar(\d)$/', $name, $m)) {
  390. $widgets = twidgets::i();
  391. return $widgets->getsidebarindex($this->context, $this->view, (int) $m[1]);
  392. }
  393. if (array_key_exists($name, $this->data['tags'])) {
  394. $tags = ttemplatetags::i();
  395. return $tags->$name;
  396. }
  397. if (isset($this->context) && isset($this->context->$name)) return $this->context->$name;
  398. return parent::__get($name);
  399. }
  400. protected function get_view($context) {
  401. return $this->itemplate ? tview::getview($context) : tview::i();
  402. }
  403. public function request($context) {
  404. $this->context = $context;
  405. ttheme::$vars['context'] = $context;
  406. ttheme::$vars['template'] = $this;
  407. $this->itemplate = $context instanceof itemplate;
  408. $this->view = $this->get_view($context);
  409. $theme = $this->view->theme;
  410. $this->ltoptions['theme']['name'] = $theme->name;
  411. litepublisher::$classes->instances[get_class($theme)] = $theme;
  412. $this->path = litepublisher::$paths->themes . $theme->name . DIRECTORY_SEPARATOR ;
  413. $this->url = litepublisher::$site->files . '/themes/' . $theme->name;
  414. if ($this->view->hovermenu) {
  415. $this->hover = $theme->templates['menu.hover'];
  416. if ($this->hover != 'bootstrap') $this->hover = ($this->hover == 'true');
  417. } else {
  418. $this->hover = false;
  419. }
  420. $result = $this->httpheader();
  421. $result .= $theme->gethtml($context);
  422. $this->callevent('onrequest', array(&$result));
  423. unset(ttheme::$vars['context'], ttheme::$vars['template']);
  424. return $result;
  425. }
  426. protected function httpheader() {
  427. $ctx = $this->context;
  428. if (method_exists($ctx, 'httpheader')) {
  429. $result= $ctx->httpheader();
  430. if (!empty($result)) return $result;
  431. }
  432. if (isset($ctx->idperm) && ($idperm = $ctx->idperm)) {
  433. $perm =tperm::i($idperm);
  434. if ($result = $perm->getheader($ctx)) {
  435. return $result . turlmap::htmlheader($ctx->cache);
  436. }
  437. }
  438. return turlmap::htmlheader($ctx->cache);
  439. }
  440. //html tags
  441. public function getsidebar() {
  442. return twidgets::i()->getsidebar($this->context, $this->view);
  443. }
  444. public function gettitle() {
  445. $title = $this->itemplate ? $this->context->gettitle() : '';
  446. if ($this->callevent('ontitle', array(&$title))) return $title;
  447. return $this->parsetitle($this->view->theme->title, $title);
  448. }
  449. public function parsetitle($tml, $title) {
  450. $args = targs::i();
  451. $args->title = $title;
  452. $result = $this->view->theme->parsearg($tml, $args);
  453. //$result = trim($result, sprintf(' |.:%c%c', 187, 150));
  454. $result = trim($result, " |.:\n\r\t");
  455. if ($result == '') return litepublisher::$site->name;
  456. return $result;
  457. }
  458. public function geticon() {
  459. $result = '';
  460. if (isset($this->context) && isset($this->context->icon)) {
  461. $icon = $this->context->icon;
  462. if ($icon > 0) {
  463. $files = tfiles::i();
  464. if ($files->itemexists($icon)) $result = $files->geturl($icon);
  465. }
  466. }
  467. if ($result == '') return litepublisher::$site->files . '/favicon.ico';
  468. return $result;
  469. }
  470. public function getkeywords() {
  471. $result = $this->itemplate ? $this->context->getkeywords() : '';
  472. if ($result == '') return litepublisher::$site->keywords;
  473. return $result;
  474. }
  475. public function getdescription() {
  476. $result = $this->itemplate ? $this->context->getdescription() : '';
  477. if ($result =='') return litepublisher::$site->description;
  478. return $result;
  479. }
  480. public function getmenu() {
  481. if ($r = $this->ongetmenu()) return $r;
  482. //$current = $this->context instanceof tmenu ? $this->context->id : 0;
  483. $view = $this->view;
  484. $menuclass = $view->menuclass;
  485. $filename = $view->theme->name . sprintf('.%s.%s.php',
  486. $menuclass, litepublisher::$options->group ? litepublisher::$options->group : 'nobody');
  487. if ($result = litepublisher::$urlmap->cache->get($filename)) return $result;
  488. $menus = getinstance($menuclass);
  489. $result = $menus->getmenu($this->hover, 0);
  490. litepublisher::$urlmap->cache->set($filename, $result);
  491. return $result;
  492. }
  493. private function getltoptions() {
  494. return sprintf('<script type="text/javascript">window.ltoptions = %s;</script>', json_encode($this->ltoptions));
  495. }
  496. public function getjavascript($filename) {
  497. return sprintf($this->js, litepublisher::$site->files . $filename);
  498. }
  499. public function getready($s) {
  500. return sprintf($this->jsready, $s);
  501. }
  502. public function getloadjavascript($s) {
  503. return sprintf($this->jsload, $s);
  504. }
  505. public function addtohead($s) {
  506. $s = trim($s);
  507. if (false === strpos($this->heads, $s)) {
  508. $this->heads = trim($this->heads) . "\n" . $s;
  509. $this->save();
  510. }
  511. }
  512. public function deletefromhead($s) {
  513. $s = trim($s);
  514. $i = strpos($this->heads, $s);
  515. if (false !== $i) {
  516. $this->heads = substr_replace($this->heads, '', $i, strlen($s));
  517. $this->heads = trim(str_replace("\n\n", "\n", $this->heads));
  518. $this->save();
  519. }
  520. }
  521. public function gethead() {
  522. $result = $this->heads;
  523. if ($this->itemplate) $result .= $this->context->gethead();
  524. $result = $this->getltoptions() . $result;
  525. $result = $this->view->theme->parse($result);
  526. $this->callevent('onhead', array(&$result));
  527. return $result;
  528. }
  529. public function getcontent() {
  530. $result = '';
  531. $this->callevent('beforecontent', array(&$result));
  532. $result .= $this->itemplate ? $this->context->getcont() : '';
  533. $this->callevent('aftercontent', array(&$result));
  534. return $result;
  535. }
  536. protected function setfooter($s) {
  537. if ($s != $this->data['footer']) {
  538. $this->data['footer'] = $s;
  539. $this->Save();
  540. }
  541. }
  542. public function getpage() {
  543. $page = litepublisher::$urlmap->page;
  544. if ($page <= 1) return '';
  545. return sprintf(tlocal::get('default', 'pagetitle'), $page);
  546. }
  547. public function trimwords($s, array $words) {
  548. if ($s == '') return '';
  549. foreach ($words as $word) {
  550. if (strbegin($s, $word)) $s = substr($s, strlen($word));
  551. if (strend($s, $word)) $s = substr($s, 0, strlen($s) - strlen*($word));
  552. }
  553. return $s;
  554. }
  555. }//class
  556. //theme.class.php
  557. class ttheme extends tevents {
  558. public static $instances = array();
  559. public static $vars = array();
  560. public static $defaultargs;
  561. public static $inifiles;
  562. public $name;
  563. public $parsing;
  564. public $templates;
  565. public $extratml;
  566. private $themeprops;
  567. public static function exists($name) {
  568. return file_exists(litepublisher::$paths->data . 'themes'. DIRECTORY_SEPARATOR . $name . '.php') ||
  569. file_exists(litepublisher::$paths->themes . $name . DIRECTORY_SEPARATOR . 'about.ini');
  570. }
  571. public static function i() {
  572. return getinstance(__class__);
  573. }
  574. public static function getinstance($name) {
  575. if (isset(self::$instances[$name])) return self::$instances[$name];
  576. $result = getinstance(__class__);
  577. if ($result->name != '') $result = litepublisher::$classes->newinstance(__class__);
  578. $result->name = $name;
  579. $result->load();
  580. return $result;
  581. }
  582. public static function getwidgetnames() {
  583. return array('submenu', 'categories', 'tags', 'archives', 'links', 'posts', 'comments', 'friends', 'meta') ;
  584. }
  585. protected function create() {
  586. parent::create();
  587. $this->name = '';
  588. $this->parsing = array();
  589. $this->data['type'] = 'litepublisher';
  590. $this->data['parent'] = '';
  591. $this->addmap('templates', array());
  592. $this->templates = array(
  593. 'index' => '',
  594. 'title' => '',
  595. 'menu' => '',
  596. 'content' => '',
  597. 'sidebars' => array(),
  598. 'custom' => array(),
  599. 'customadmin' => array()
  600. );
  601. $this->themeprops = new tthemeprops($this);
  602. if (!isset(self::$defaultargs)) self::set_defaultargs();
  603. $this->extratml = '';
  604. }
  605. public static function set_defaultargs() {
  606. self::$defaultargs = array(
  607. '$site.url' => litepublisher::$site->url,
  608. '$site.files' => litepublisher::$site->files,
  609. '{$site.q}' => litepublisher::$site->q,
  610. '$site.q' => litepublisher::$site->q
  611. );
  612. }
  613. public function __destruct() {
  614. unset($this->themeprops, self::$instances[$this->name], $this->templates);
  615. parent::__destruct();
  616. }
  617. public function getbasename() {
  618. return 'themes' . DIRECTORY_SEPARATOR . $this->name;
  619. }
  620. public function load() {
  621. if ($this->name == '') return false;
  622. if (parent::load()) {
  623. self::$instances[$this->name] = $this;
  624. return true;
  625. }
  626. return $this->parsetheme();
  627. }
  628. public function parsetheme() {
  629. if (!file_exists(litepublisher::$paths->themes . $this->name . DIRECTORY_SEPARATOR . 'about.ini')) {
  630. $this->error(sprintf('The %s theme not exists', $this->name));
  631. }
  632. $parser = tthemeparser::i();
  633. if ($parser->parse($this)) {
  634. self::$instances[$this->name] = $this;
  635. $this->save();
  636. }else {
  637. $this->error(sprintf('Theme file %s not exists', $filename));
  638. }
  639. }
  640. public function __tostring() {
  641. return $this->templates['index'];
  642. }
  643. public function __get($name) {
  644. if (array_key_exists($name, $this->templates)) return $this->themeprops->setpath($name);
  645. if ($name == 'comment') return $this->themeprops->setpath('content.post.templatecomments.comments.comment');
  646. if ($name == 'sidebar') return $this->themeprops->setroot($this->templates['sidebars'][0]);
  647. if (preg_match('/^sidebar(\d)$/', $name, $m)) return $this->themeprops->setroot($this->templates['sidebars'][$m[1]]);
  648. return parent::__get($name);
  649. }
  650. public function __set($name, $value) {
  651. if (array_key_exists($name, $this->templates)) {
  652. $this->templates[$name] = $value;
  653. return;
  654. }
  655. return parent::__set($name, $value);
  656. }
  657. public function gettag($path) {
  658. if (!array_key_exists($path, $this->templates)) $this->error(sprintf('Path "%s" not found', $path));
  659. $this->themeprops->setpath($path);
  660. $this->themeprops->tostring = true;
  661. return $this->themeprops;
  662. }
  663. public function reg($exp) {
  664. if (!strpos($exp, '\.')) $exp = str_replace('.', '\.', $exp);
  665. $result = array();
  666. foreach ($this->templates as $name => $val) {
  667. if (preg_match($exp, $name)) $result[$name] = $val;
  668. }
  669. return $result;
  670. }
  671. public function getsidebarscount() {
  672. return count($this->templates['sidebars']);
  673. }
  674. private function get_author() {
  675. $context = isset(litepublisher::$urlmap->context) ? litepublisher::$urlmap->context : ttemplate::i()->context;
  676. if (!is_object($context)) {
  677. if (!isset(self::$vars['post'])) return new emptyclass();
  678. $context = self::$vars['post'];
  679. }
  680. if ($context instanceof tuserpages) return $context;
  681. $iduser = 0;
  682. foreach (array('author', 'idauthor', 'user', 'iduser') as $propname) {
  683. if (isset($context->$propname)) {
  684. $iduser = $context->$propname;
  685. break;
  686. }
  687. }
  688. if (!$iduser) return new emptyclass();
  689. $pages = tuserpages::i();
  690. if (!$pages->itemexists($iduser)) return new emptyclass();
  691. $pages->request($iduser);
  692. return $pages;
  693. }
  694. private function getvar($name) {
  695. switch ($name) {
  696. case 'site':
  697. return litepublisher::$site;
  698. case 'lang':
  699. return tlocal::i();
  700. case 'post':
  701. $context = isset(litepublisher::$urlmap->context) ? litepublisher::$urlmap->context : ttemplate::i()->context;
  702. if ($context instanceof tpost) return $context;
  703. break;
  704. case 'author':
  705. return self::get_author();
  706. case 'metapost':
  707. return isset(self::$vars['post']) ? self::$vars['post']->meta : new emptyclass();
  708. } //switch
  709. if (isset($GLOBALS[$name])) {
  710. $var = $GLOBALS[$name];
  711. } else {
  712. $classes = litepublisher::$classes;
  713. $var = $classes->gettemplatevar($name);
  714. if (!$var) {
  715. if (isset($classes->classes[$name])) {
  716. $var = $classes->getinstance($classes->classes[$name]);
  717. } elseif (isset($classes->items[$name])) {
  718. $var = $classes->getinstance($name);
  719. } else {
  720. $class = 't' . $name;
  721. if (isset($classes->items[$class])) $var = $classes->getinstance($class);
  722. }
  723. }
  724. }
  725. if (!is_object($var)) {
  726. litepublisher::$options->trace(sprintf('Object "%s" not found in %s', $name, $this->parsing[count($this->parsing) -1]));
  727. return false;
  728. }
  729. return $var;
  730. }
  731. public function parsecallback($names) {
  732. $name = $names[1];
  733. $prop = $names[2];
  734. if (isset(self::$vars[$name])) {
  735. $var = self::$vars[$name];
  736. } elseif ($name == 'custom') {
  737. return $this->parse($this->templates['custom'][$prop]);
  738. } elseif ($var = $this->getvar($name)) {
  739. self::$vars[$name] = $var;
  740. } elseif (($name == 'metapost') && isset(self::$vars['post'])) {
  741. $var = self::$vars['post']->meta;
  742. } else {
  743. return '';
  744. }
  745. try {
  746. return $var->{$prop};
  747. } catch (Exception $e) {
  748. litepublisher::$options->handexception($e);
  749. }
  750. return '';
  751. }
  752. public function parse($s) {
  753. $s = strtr((string) $s, self::$defaultargs);
  754. if (isset($this->templates['content.admin.tableclass'])) $s = str_replace('$tableclass', $this->templates['content.admin.tableclass'], $s);
  755. array_push($this->parsing, $s);
  756. try {
  757. $s = preg_replace('/%%([a-zA-Z0-9]*+)_(\w\w*+)%%/', '\$$1.$2', $s);
  758. $result = preg_replace_callback('/\$([a-zA-Z]\w*+)\.(\w\w*+)/', array($this, 'parsecallback'), $s);
  759. } catch (Exception $e) {
  760. $result = '';
  761. litepublisher::$options->handexception($e);
  762. }
  763. array_pop($this->parsing);
  764. return $result;
  765. }
  766. public function parsearg($s, targs $args) {
  767. $s = $this->parse($s);
  768. return strtr ($s, $args->data);
  769. }
  770. public function replacelang($s, $lang) {
  771. $s = preg_replace('/%%([a-zA-Z0-9]*+)_(\w\w*+)%%/', '\$$1.$2', (string) $s);
  772. self::$vars['lang'] = isset($lang) ? $lang : tlocal::i('default');
  773. $s = strtr($s, self::$defaultargs);
  774. if (preg_match_all('/\$lang\.(\w\w*+)/', $s, $m, PREG_SET_ORDER)) {
  775. foreach ($m as $item) {
  776. $name = $item[1];
  777. if ($v = $lang->{$name}) {
  778. $s = str_replace($item[0], $v, $s);
  779. }
  780. }
  781. }
  782. return $s;
  783. }
  784. public static function parsevar($name, $var, $s) {
  785. self::$vars[$name] = $var;
  786. return self::i()->parse($s);
  787. }
  788. public function gethtml($context) {
  789. self::$vars['context'] = $context;
  790. if (isset($context->index_tml) && ($tml = $context->index_tml)) return $this->parse($tml);
  791. return $this->parse($this->templates['index']);
  792. }
  793. public function getnotfount() {
  794. return $this->parse($this->templates['content.notfound']);
  795. }
  796. public function getpages($url, $page, $count, $params = '') {
  797. if (!(($count > 1) && ($page >=1) && ($page <= $count))) return '';
  798. $args = new targs();
  799. $args->count = $count;
  800. $from = 1;
  801. $to = $count;
  802. $perpage = litepublisher::$options->perpage;
  803. $args->perpage = $perpage;
  804. $items = array();
  805. if ($count > $perpage * 2) {
  806. //$page is midle of the bar
  807. $from = (int) max(1, $page - ceil($perpage / 2));
  808. $to = (int) min($count, $from + $perpage);
  809. }
  810. if ($from == 1) {
  811. $items = range($from, $to);
  812. } else {
  813. $items[0] = 1;
  814. if ($from > $perpage) {
  815. if ($from - $perpage - 1 < $perpage) {
  816. $items[] = $perpage;
  817. } else {
  818. array_splice($items, count($items), 0, range($perpage, $from - 1, $perpage));
  819. }
  820. }
  821. array_splice($items, count($items), 0, range($from, $to));
  822. }
  823. if ($to < $count) {
  824. $from2 = (int) ($perpage * ceil(($to+1) / $perpage));
  825. if ($from2 + $perpage >= $count) {
  826. if ($from2 < $count) $items[] = $from2;
  827. } else {
  828. array_splice($items, count($items), 0, range($from2, $count, $perpage));
  829. }
  830. if ($items[count($items) -1] != $count) $items[] = $count;
  831. }
  832. $currenttml=$this->templates['content.navi.current'];
  833. $tml =$this->templates['content.navi.link'];
  834. if (!strbegin($url, 'http')) $url = litepublisher::$site->url . $url;
  835. $pageurl = rtrim($url, '/') . '/page/';
  836. $a = array();
  837. foreach ($items as $i) {
  838. $args->page = $i;
  839. $link = $i == 1 ? $url : $pageurl .$i . '/';
  840. if ($params) $link .= litepublisher::$site->q . $params;
  841. $args->link = $link;
  842. $a[] = $this->parsearg(($i == $page ? $currenttml : $tml), $args);
  843. }
  844. $args->link =$url;
  845. $args->pageurl = $pageurl;
  846. $args->page = $page;
  847. $args->items = implode($this->templates['content.navi.divider'], $a);
  848. return $this->parsearg($this->templates['content.navi'], $args);
  849. }
  850. public function getposts(array $items, $lite) {
  851. if (count($items) == 0) return '';
  852. if (dbversion) tposts::i()->loaditems($items);
  853. $result = '';
  854. self::$vars['lang'] = tlocal::i('default');
  855. //$tml = $lite ? $this->templates['content.excerpts.lite.excerpt'] : $this->templates['content.excerpts.excerpt'];
  856. foreach($items as $id) {
  857. $post = tpost::i($id);
  858. $result .= $post->getcontexcerpt($lite);
  859. // has $author.* tags in tml
  860. if (isset(self::$vars['author'])) unset(self::$vars['author']);
  861. }
  862. $tml = $lite ? $this->templates['content.excerpts.lite'] : $this->templates['content.excerpts'];
  863. if ($tml != '') $result = str_replace('$excerpt', $result, $this->parse($tml));
  864. unset(self::$vars['post']);
  865. return $result;
  866. }
  867. public function getpostsnavi(array $items, $lite, $url, $count, $liteperpage = 1000) {
  868. $result = $this->getposts($items, $lite);
  869. $perpage = $lite ? $liteperpage : litepublisher::$options->perpage;
  870. $result .= $this->getpages($url, litepublisher::$urlmap->page, ceil($count / $perpage));
  871. return $result;
  872. }
  873. public function getpostswidgetcontent(array $items, $sidebar, $tml) {
  874. if (count($items) == 0) return '';
  875. $result = '';
  876. if ($tml == '') $tml = $this->getwidgetitem('posts', $sidebar);
  877. foreach ($items as $id) {
  878. self::$vars['post'] = tpost::i($id);
  879. $result .= $this->parse($tml);
  880. }
  881. unset(self::$vars['post']);
  882. return str_replace('$item', $result, $this->getwidgetitems('posts', $sidebar));
  883. }
  884. public function getwidgetcontent($items, $name, $sidebar) {
  885. return str_replace('$item', $items, $this->getwidgetitems($name, $sidebar));
  886. }
  887. public function getwidget($title, $content, $template, $sidebar) {
  888. $args = new targs();
  889. $args->title = $title;
  890. $args->items = $content;
  891. $args->sidebar = $sidebar;
  892. return $this->parsearg($this->getwidgettml($sidebar, $template, ''), $args);
  893. }
  894. public function getidwidget($id, $title, $content, $template, $sidebar) {
  895. $args = new targs();
  896. $args->id = $id;
  897. $args->title = $title;
  898. $args->items = $content;
  899. $args->sidebar = $sidebar;
  900. return $this->parsearg($this->getwidgettml($sidebar, $template, ''), $args);
  901. }
  902. public function getwidgetitem($name, $index) {
  903. return $this->getwidgettml($index, $name, 'item');
  904. }
  905. public function getwidgetitems($name, $index) {
  906. return $this->getwidgettml($index, $name, 'items');
  907. }
  908. public function getwidgettml($index, $name, $tml) {
  909. $count = count($this->templates['sidebars']);
  910. if ($index >= $count) $index = $count - 1;
  911. $widgets = &$this->templates['sidebars'][$index];
  912. if (($tml != '') && ($tml [0] != '.')) $tml = '.' . $tml;
  913. if (isset($widgets[$name . $tml])) return $widgets[$name . $tml];
  914. if (isset($widgets['widget' . $tml])) return $widgets['widget' . $tml];
  915. $this->error("Unknown widget '$name' and template '$tml' in $index sidebar");
  916. }
  917. public function getajaxtitle($id, $title, $sidebar, $tml) {
  918. $args = new targs();
  919. $args->title = $title;
  920. $args->id = $id;
  921. $args->sidebar = $sidebar;
  922. return $this->parsearg($this->templates[$tml], $args);
  923. }
  924. public function simple($content) {
  925. return str_replace('$content', $content, $this->templates['content.simple']);
  926. }
  927. public function getbutton($title) {
  928. return strtr($this->templates['content.admin.button'], array(
  929. '$lang.$name' => $title,
  930. 'name="$name"' => '',
  931. 'id="submitbutton-$name"' => ''
  932. ));
  933. }
  934. public function getsubmit($title) {
  935. return strtr($this->templates['content.admin.button'], array(
  936. '$lang.$name', '$title',
  937. 'name="$name"' => '',
  938. 'id="submitbutton-$name"' => ''
  939. ));
  940. }
  941. public static function clearcache() {
  942. tfiler::delete(litepublisher::$paths->data . 'themes', false, false);
  943. litepublisher::$urlmap->clearcache();
  944. }
  945. public static function cacheini($filename) {
  946. if (isset(self::$inifiles[$filename])) return self::$inifiles[$filename];
  947. $datafile = tlocal::getcachedir() . sprintf('cacheini.%s.php', md5($filename));
  948. if (!tfilestorage::loadvar($datafile, $ini) || !is_array($ini)) {
  949. if (file_exists($filename)) {
  950. $ini = parse_ini_file($filename, true);
  951. tfilestorage::savevar($datafile, $ini);
  952. } else {
  953. $ini = array();
  954. }
  955. }
  956. if (!isset(self::$inifiles)) self::$inifiles = array();
  957. self::$inifiles[$filename] = $ini;
  958. return $ini;
  959. }
  960. public static function inifile($class, $filename) {
  961. $dir = litepublisher::$classes->getresourcedir($class);
  962. return self::cacheini($dir . $filename);
  963. }
  964. public static function getwidgetpath($path) {
  965. if ($path === '') return '';
  966. switch ($path) {
  967. case '.items':
  968. return '.items';
  969. case '.items.item':
  970. case '.item':
  971. return '.item';
  972. case '.items.item.subcount':
  973. case '.item.subcount':
  974. case '.subcount':
  975. return '.subcount';
  976. case '.items.item.subitems':
  977. case '.item.subitems':
  978. case '.subitems':
  979. return '.subitems';
  980. case '.classes':
  981. case '.items.classes':
  982. return '.classes';
  983. }
  984. return false;
  985. }
  986. }//class
  987. class tthemeprops {
  988. public $path;
  989. public $tostring;
  990. private $root;
  991. private $theme;
  992. public function __construct(ttheme $theme) {
  993. $this->theme = $theme;
  994. $this->root = &$theme->templates;
  995. $this->path = '';
  996. $this->tostring = false;
  997. }
  998. public function __destruct() {
  999. unset($this->theme, $this->root);
  1000. }
  1001. public function error($path) {
  1002. litepublisher::$options->trace(sprintf('Path "%s" not found', $path));
  1003. litepublisher::$options->showerrors();
  1004. }
  1005. public function getpath($name) {
  1006. return $this->path == '' ? $name : $this->path . '.' . $name;
  1007. }
  1008. public function setpath($path) {
  1009. $this->root = &$this->theme->templates;
  1010. $this->path = $path;
  1011. $this->tostring = false;
  1012. return $this;
  1013. }
  1014. public function setroot(array &$root) {
  1015. $this->setpath('');
  1016. $this->root = &$root;
  1017. return $this;
  1018. }
  1019. public function __get($name) {
  1020. //echo "$name get tml<br>";
  1021. $path = $this->getpath($name);
  1022. if (!array_key_exists($path, $this->root)) $this->error($path);
  1023. if ($this->tostring) return $this->root[$path];
  1024. $this->path = $path;
  1025. return $this;
  1026. }
  1027. public function __set($name, $value) {
  1028. $this->root[$this->getpath($name)] = $value;
  1029. }
  1030. public function __call($name, $params) {
  1031. if (isset($params[0]) && is_object($params[0]) && ($params[0] instanceof targs)) {
  1032. return $this->theme->parsearg( (string) $this->$name, $params[0]);
  1033. } else {
  1034. return $this->theme->parse((string) $this->$name);
  1035. }
  1036. }
  1037. public function __tostring() {
  1038. if (array_key_exists($this->path, $this->root)) {
  1039. return $this->root[$this->path];
  1040. } else {
  1041. $this->error($this->path);
  1042. }
  1043. }
  1044. public function __isset($name) {
  1045. return array_key_exists($this->getpath($name), $this->root);
  1046. }
  1047. }//class
  1048. class targs {
  1049. public $data;
  1050. public static function i() {
  1051. return litepublisher::$classes->newinstance(__class__);
  1052. }
  1053. public function __construct($thisthis = null) {
  1054. if (!isset(ttheme::$defaultargs)) ttheme::set_defaultargs();
  1055. $this->data = ttheme::$defaultargs;
  1056. if (isset($thisthis)) $this->data['$this'] = $thisthis;
  1057. }
  1058. public function __get($name) {
  1059. if (($name == 'link') && !isset($this->data['$link']) && isset($this->data['$url'])) {
  1060. return litepublisher::$site->url . $this->data['$url'];
  1061. }
  1062. return $this->data['$' . $name];
  1063. }
  1064. public function __set($name, $value) {
  1065. if (!is_string($name)) return;
  1066. if ($name == '') return;
  1067. if (is_bool($value)) {
  1068. $value = $value ? 'checked="checked"' : '';
  1069. }
  1070. $this->data['$'.$name] = $value;
  1071. $this->data["%%$name%%"] = $value;
  1072. if (($name == 'url') && !isset($this->data['$link'])) {
  1073. $this->data['$link'] = litepublisher::$site->url . $value;
  1074. $this->data['%%link%%'] = litepublisher::$site->url . $value;
  1075. }
  1076. }
  1077. public function add(array $a) {
  1078. foreach ($a as $key => $value) {
  1079. $this->__set($key, $value);
  1080. if ($key == 'url') {
  1081. $this->data['$link'] = litepublisher::$site->url . $value;
  1082. $this->data['%%link%%'] = litepublisher::$site->url . $value;
  1083. }
  1084. }
  1085. if (isset($a['title']) && !isset($a['text'])) $this->__set('text', $a['title']);
  1086. if (isset($a['text']) && !isset($a['title'])) $this->__set('title', $a['text']);
  1087. }
  1088. }//class
  1089. class emptyclass{
  1090. public function __get($name) { return ''; }
  1091. }
  1092. //widgets.class.php
  1093. class twidget extends tevents {
  1094. public $id;
  1095. public $template;
  1096. protected $adminclass;
  1097. protected function create() {
  1098. parent::create();
  1099. $this->basename = 'widget';
  1100. $this->cache = 'cache';
  1101. $this->id = 0;
  1102. $this->template = 'widget';
  1103. $this->adminclass = 'tadminwidget';
  1104. }
  1105. public function addtosidebar($sidebar) {
  1106. $widgets = twidgets::i();
  1107. $id = $widgets->add($this);
  1108. $sidebars = tsidebars::i();
  1109. $sidebars->insert($id, false, $sidebar, -1);
  1110. litepublisher::$urlmap->clearcache();
  1111. return $id;
  1112. }
  1113. protected function getadmin() {
  1114. if (($this->adminclass != '') && class_exists($this->adminclass)) {
  1115. $admin = getinstance($this->adminclass);
  1116. $admin->widget = $this;
  1117. return $admin;
  1118. }
  1119. $this->error(sprintf('The "%s" admin class not found', $this->adminclass));
  1120. }
  1121. public function getwidget($id, $sidebar) {
  1122. ttheme::$vars['widget'] = $this;
  1123. try {
  1124. $title = $this->gettitle($id);
  1125. $content = $this->getcontent($id, $sidebar);
  1126. } catch (Exception $e) {
  1127. litepublisher::$options->handexception($e);
  1128. return '';
  1129. }
  1130. $theme = ttheme::i();
  1131. $result = $theme->getidwidget($id, $title, $content, $this->template, $sidebar);
  1132. unset(ttheme::$vars['widget']);
  1133. return $result;
  1134. }
  1135. public function getdeftitle() {
  1136. return '';
  1137. }
  1138. public function gettitle($id) {
  1139. if (!isset($id)) $this->error('no id');
  1140. $widgets = twidgets::i();
  1141. if (isset($widgets->items[$id])) {
  1142. return $widgets->items[$id]['title'];
  1143. }
  1144. return $this->getdeftitle();
  1145. }
  1146. public function settitle($id, $title) {
  1147. $widgets = twidgets::i();
  1148. if (isset($widgets->items[$id]) && ($widgets->items[$id]['title'] != $title)) {
  1149. $widgets->items[$id]['title'] = $title;
  1150. $widgets->save();
  1151. }
  1152. }
  1153. public function getcontent($id, $sidebar) {
  1154. return '';
  1155. }
  1156. public static function getcachefilename($id) {
  1157. $theme = ttheme::i();
  1158. if ($theme->name == '') {
  1159. $theme = tview::i()->theme;
  1160. }
  1161. return sprintf('widget.%s.%d.php', $theme->name, $id);
  1162. }
  1163. public function expired($id) {
  1164. switch ($this->cache) {
  1165. case 'cache':
  1166. $cache = twidgetscache::i();
  1167. $cache->expired($id);
  1168. break;
  1169. case 'include':
  1170. $sidebar = self::findsidebar($id);
  1171. $filename = self::getcachefilename($id, $sidebar);
  1172. litepublisher::$urlmap->cache->set($filename, $this->getcontent($id, $sidebar));
  1173. break;
  1174. }
  1175. }
  1176. public static function findsidebar($id) {
  1177. $view = tview::i();
  1178. foreach ($view->sidebars as $i=> $sidebar) {
  1179. foreach ($sidebar as $item) {
  1180. if ($id == $item['id']) return $i;
  1181. }
  1182. }
  1183. return 0;
  1184. }
  1185. public function expire() {
  1186. $widgets = twidgets::i();
  1187. foreach ($widgets->items as $id => $item) {
  1188. if ($this instanceof $item['class']) $this->expired($id);
  1189. }
  1190. }
  1191. public function getcontext($class) {
  1192. if (litepublisher::$urlmap->context instanceof $class) return litepublisher::$urlmap->context;
  1193. //ajax
  1194. $widgets = twidgets::i();
  1195. return litepublisher::$urlmap->getidcontext($widgets->idurlcontext);
  1196. }
  1197. }//class
  1198. class torderwidget extends twidget {
  1199. protected function create() {
  1200. parent::create();
  1201. unset($this->id);
  1202. $this->data['id'] = 0;
  1203. $this->data['ajax'] = false;
  1204. $this->data['order'] = 0;
  1205. $this->data['sidebar'] = 0;
  1206. }
  1207. public function onsidebar(array &$items, $sidebar) {
  1208. if ($sidebar != $this->sidebar) return;
  1209. $order = $this->order;
  1210. if (($order < 0) || ($order >= count($items))) $order = count($items);
  1211. array_insert($items, array('id' => $this->id, 'ajax' => $this->ajax), $order);
  1212. }
  1213. }//class
  1214. class tclasswidget extends twidget {
  1215. private $item;
  1216. private function isvalue($name) {
  1217. return in_array($name, array('ajax', 'order', 'sidebar'));
  1218. }
  1219. public function __get($name) {
  1220. if ($this->isvalue($name)) {
  1221. if (!$this->item) {
  1222. $widgets = twidgets::i();
  1223. $this->item = &$widgets->finditem($widgets->find($this));
  1224. }
  1225. return $this->item[$name];
  1226. }
  1227. return parent::__get($name);
  1228. }
  1229. public function __set($name, $value) {
  1230. if ($this->isvalue($name)) {
  1231. if (!$this->item) {
  1232. $widgets = twidgets::i();
  1233. $this->item = &$widgets->finditem($widgets->find($this));
  1234. }
  1235. $this->item[$name] = $value;
  1236. } else {
  1237. parent::__set($name, $value);
  1238. }
  1239. }
  1240. public function save() {
  1241. parent::save();
  1242. $widgets = twidgets::i();
  1243. $widgets->save();
  1244. }
  1245. }//class
  1246. class twidgets extends titems_storage {
  1247. public $classes;
  1248. public $currentsidebar;
  1249. public $idwidget;
  1250. public $idurlcontext;
  1251. public static function i($id = null) {
  1252. return getinstance(__class__);
  1253. }
  1254. protected function create() {
  1255. $this->dbversion = false;
  1256. parent::create();
  1257. $this->addevents('onwidget', 'onadminlogged', 'onadminpanel', 'ongetwidgets', 'onsidebar');
  1258. $this->basename = 'widgets';
  1259. $this->currentsidebar = 0;
  1260. $this->idurlcontext = 0;
  1261. $this->addmap('classes', array());
  1262. }
  1263. public function add(twidget $widget) {
  1264. return $this->additem( array(
  1265. 'class' => get_class($widget),
  1266. 'cache' => $widget->cache,
  1267. 'title' => $widget->gettitle(0),
  1268. 'template' => $widget->template
  1269. ));
  1270. }
  1271. public function addext(twidget $widget, $title, $template) {
  1272. return $this->additem( array(
  1273. 'class' => get_class($widget),
  1274. 'cache' => $widget->cache,
  1275. 'title' => $title,
  1276. 'template' => $template
  1277. ));
  1278. }
  1279. public function addclass(twidget $widget, $class) {
  1280. $this->lock();
  1281. $id = $this->add($widget);
  1282. if (!isset($this->classes[$class])) $this->classes[$class] = array();
  1283. $this->classes[$class][] = array(
  1284. 'id' => $id,
  1285. 'order' => 0,
  1286. 'sidebar' => 0,
  1287. 'ajax' => false
  1288. );
  1289. $this->unlock();
  1290. return $id;
  1291. }
  1292. public function subclass($id) {
  1293. foreach ($this->classes as $class => $items) {
  1294. foreach ($items as $item) {
  1295. if ($id == $item['id']) return $class;
  1296. }
  1297. }
  1298. return false;
  1299. }
  1300. public function delete($id) {
  1301. if (!isset($this->items[$id])) return false;
  1302. foreach ($this->classes as $class => $items) {
  1303. foreach ($items as $i => $item) {
  1304. if ($id == $item['id']) array_delete($this->classes[$class], $i);
  1305. }
  1306. }
  1307. unset($this->items[$id]);
  1308. $this->deleted($id);
  1309. $this->save();
  1310. return true;
  1311. }
  1312. public function deleteclass($class) {
  1313. $this->unbind($class);
  1314. $deleted = array();
  1315. foreach ($this->items as $id => $item) {
  1316. if($class == $item['class']) {
  1317. unset($this->items[$id]);
  1318. $deleted[] = $id;
  1319. }
  1320. }
  1321. if (count($deleted) > 0) {
  1322. foreach ($this->classes as $name => $items) {
  1323. foreach ($items as $i => $item) {
  1324. if (in_array($item['id'], $deleted)) array_delete($this->classes[$name], $i);
  1325. }
  1326. if (count($this->classes[$name]) == 0) unset($this->classes[$name]);
  1327. }
  1328. }
  1329. if (isset($this->classes[$class])) unset($this->classes[$class]);
  1330. $this->save();
  1331. foreach ($deleted as $id) $this->deleted($id);
  1332. }
  1333. public function class2id($class) {
  1334. foreach ($this->items as $id => $item) {
  1335. if($class == $item['class']) return $id;
  1336. }
  1337. return false;
  1338. }
  1339. public function getwidget($id) {
  1340. if (!isset($this->items[$id])) return $this->error("The requested $id widget not found");
  1341. $class = $this->items[$id]['class'];
  1342. if (!class_exists($class)) {
  1343. $this->delete($id);
  1344. return $this->error("The $class class not found");
  1345. }
  1346. $result = getinstance($class);
  1347. $result->id = $id;
  1348. return $result;
  1349. }
  1350. public function getsidebar($context, tview $view) {
  1351. return $this->getsidebarindex($context, $view, $this->currentsidebar++);
  1352. }
  1353. public function getsidebarindex($context, tview $view, $sidebar) {
  1354. $items = $this->getwidgets($context, $view, $sidebar);
  1355. if ($context instanceof iwidgets) $context->getwidgets($items, $sidebar);
  1356. if (litepublisher::$options->admincookie) $this->callevent('onadminlogged', array(&$items, $sidebar));
  1357. if (litepublisher::$urlmap->adminpanel) $this->callevent('onadminpanel', array(&$items, $sidebar));
  1358. $this->callevent('ongetwidgets', array(&$items, $sidebar));
  1359. $result = $this->getsidebarcontent($items, $sidebar, !$view->customsidebar && $view->disableajax);
  1360. if ($context instanceof iwidgets) $context->getsidebar($result, $sidebar);
  1361. $this->callevent('onsidebar', array(&$result, $sidebar));
  1362. return $result;
  1363. }
  1364. private function getwidgets($context, tview $view, $sidebar) {
  1365. $theme = $view->theme;
  1366. if (($view->id > 1) && !$view->customsidebar) {
  1367. $view = tview::i(1);
  1368. }
  1369. $items = isset($view->sidebars[$sidebar]) ? $view->sidebars[$sidebar] : array();
  1370. $subitems = $this->getsubitems($context, $sidebar);
  1371. $items = $this->joinitems($items, $subitems);
  1372. if ($sidebar + 1 == $theme->sidebarscount) {
  1373. for ($i = $sidebar + 1; $i < count($view->sidebars); $i++) {
  1374. $subitems = $this->joinitems($view->sidebars[$i], $this->getsubitems($context, $i));
  1375. //delete copies
  1376. foreach ($subitems as $index => $subitem) {
  1377. $id = $subitem['id'];
  1378. foreach ($items as $item) {
  1379. if ($id == $item['id']) array_delete($subitems, $index);
  1380. }
  1381. }
  1382. foreach ($subitems as $item) $items[] = $item;
  1383. }
  1384. }
  1385. return $items;
  1386. }
  1387. private function getsubitems($context, $sidebar) {
  1388. $result = array();
  1389. foreach ($this->classes as $class => $items) {
  1390. if ($context instanceof $class) {
  1391. foreach ($items as $item) {
  1392. if ($sidebar == $item['sidebar']) $result[] = $item;
  1393. }
  1394. }
  1395. }
  1396. return $result;
  1397. }
  1398. private function joinitems(array $items, array $subitems) {
  1399. if (count($subitems) == 0) return $items;
  1400. if (count($items) > 0) {
  1401. //delete copies
  1402. for ($i = count($items) -1; $i >= 0; $i--) {
  1403. $id = $items[$i]['id'];
  1404. foreach ($subitems as $subitem) {
  1405. if ($id == $subitem['id']) array_delete($items, $i);
  1406. }
  1407. }
  1408. }
  1409. //join
  1410. foreach ($subitems as $item) {
  1411. $count = count($items);
  1412. $order = $item['order'];
  1413. if (($order < 0) || ($order >= $count)) {
  1414. $items[] = $item;
  1415. } else {
  1416. array_insert($items, $item, $order);
  1417. }
  1418. }
  1419. return $items;
  1420. }
  1421. private function getsidebarcontent(array $items, $sidebar, $disableajax) {
  1422. $result = '';
  1423. foreach ($items as $item) {
  1424. $id = $item['id'];
  1425. if (!isset($this->items[$id])) continue;
  1426. $cachetype = $this->items[$id]['cache'];
  1427. if ($disableajax) $item['ajax'] = false;
  1428. if ($item['ajax'] === 'inline') {
  1429. switch ($cachetype) {
  1430. case 'cache':
  1431. case 'nocache':
  1432. case false:
  1433. $content = $this->getinline($id, $sidebar);
  1434. break;
  1435. default:
  1436. $content = $this->getajax($id, $sidebar);
  1437. break;
  1438. }
  1439. } elseif ($item['ajax']) {
  1440. $content = $this->getajax($id, $sidebar);
  1441. } else {
  1442. switch ($cachetype) {
  1443. case 'cache':
  1444. $content = $this->getwidgetcache($id, $sidebar);
  1445. break;
  1446. case 'include':
  1447. $content = $this->includewidget($id, $sidebar);
  1448. break;
  1449. case 'nocache':
  1450. case false:
  1451. $widget = $this->getwidget($id);
  1452. $content = $widget->getwidget($id, $sidebar);
  1453. break;
  1454. case 'code':
  1455. $content = $this->getcode($id, $sidebar);
  1456. break;
  1457. }
  1458. }
  1459. $this->callevent('onwidget', array($id, &$content));
  1460. $result .= $content;
  1461. }
  1462. return $result;
  1463. }
  1464. public function getajax($

Large files files are truncated, but you can click here to view the full file