gino-news  2.1.0
Modulo News per gino CMS
class_news.php
Go to the documentation of this file.
1 <?php
16 namespace Gino\App\News;
17 
18 use \Gino\Registry;
19 use \Gino\Loader;
20 use \Gino\Error;
21 use \Gino\View;
22 use \Gino\GTag;
23 use \Gino\Options;
24 use \Gino\AdminTable;
25 use \Gino\App\Module\ModuleInstance;
26 
27 require_once('class.Article.php');
28 require_once('class.Category.php');
29 
46 class news extends \Gino\Controller {
47 
52 
56  private $_list_nfp;
57 
62 
67 
72 
76  private $_image_width;
77 
82 
86  private $_tbl_opt;
87 
94  function __construct($mdlId) {
95 
96  parent::__construct($mdlId);
97 
98  $this->_tbl_opt = 'news_opt';
99 
100  $this->_optionsValue = array(
101  'last_news_number'=>3,
102  'list_nfp'=>5,
103  'showcase_news_number'=>5,
104  'showcase_auto_start'=>0,
105  'showcase_auto_interval'=>5000,
106  'image_width'=>600,
107  'newsletter_news_number'=>10,
108  );
109 
110  $this->_last_news_number = $this->setOption('last_news_number', array('value'=>$this->_optionsValue['last_news_number']));
111  $this->_list_nfp = $this->setOption('list_nfp', array('value'=>$this->_optionsValue['list_nfp']));
112  $this->_showcase_news_number = $this->setOption('showcase_news_number', array('value'=>$this->_optionsValue['showcase_news_number']));
113  $this->_showcase_auto_start = $this->setOption('showcase_auto_start', array('value'=>$this->_optionsValue['showcase_auto_start']));
114  $this->_showcase_auto_interval = $this->setOption('showcase_auto_interval', array('value'=>$this->_optionsValue['showcase_auto_interval']));
115  $this->_image_width = $this->setOption('image_width', array('value'=>$this->_optionsValue['image_width']));
116  $this->_newsletter_news_number = $this->setOption('newsletter_news_number', array('value'=>$this->_optionsValue['newsletter_news_number']));
117 
118  $this->_options = new Options($this);
119  $this->_optionsLabels = array(
120  "last_news_number"=>array(
121  'label'=>_("Numero ultime news"),
122  'value'=>$this->_optionsValue['last_news_number'],
123  'section'=>true,
124  'section_title'=>_('Ultime news')
125  ),
126  "list_nfp"=>array(
127  'label'=>_("Numero news per pagina"),
128  'value'=>$this->_optionsValue['list_nfp'],
129  'section'=>true,
130  'section_title'=>_('Archivio news')
131  ),
132  "showcase_news_number"=>array(
133  'label'=>_("Numero news"),
134  'value'=>$this->_optionsValue['showcase_news_number'],
135  'section'=>true,
136  'section_title'=>_('Vetrina news')
137  ),
138  "showcase_auto_start"=>array(
139  'label'=>_("Animazione automatica"),
140  'value'=>$this->_optionsValue['showcase_auto_start']
141  ),
142  "showcase_auto_interval"=>array(
143  'label'=>_("Intervallo animazione automatica (ms)"),
144  'value'=>$this->_optionsValue['showcase_auto_start']
145  ),
146  "image_width"=>array(
147  'label'=>_("Larghezza massima immagini"),
148  'value'=>$this->_optionsValue['image_width'],
149  'section'=>true,
150  'section_title'=>_('Media')
151  ),
152  "newsletter_news_number"=>array(
153  'label'=>_("Numero news esportate nella lista"),
154  'value'=>$this->_optionsValue['newsletter_news_number'],
155  'section'=>true,
156  'section_title'=>_('Newsletter')
157  ),
158  );
159  }
160 
166  public static function getClassElements() {
167 
168  return array(
169  "tables"=>array(
170  'news_article',
171  'news_article_category',
172  'news_category',
173  'news_opt',
174  ),
175  "css"=>array(
176  'news.css'
177  ),
178  "views" => array(
179  'archive.php' => _('Archivio news'),
180  'detail.php' => _('Dettaglio news'),
181  'last.php' => _('Lista ultime news'),
182  'showcase.php' => _('Vetrina news'),
183  'feed_rss.php' => _('Feed RSS'),
184  'newsletter.php' => _('News esportate in newsletter')
185  ),
186  "folderStructure"=>array (
187  CONTENT_DIR.OS.'news'=> array(
188  'img' => null,
189  'attachment' => null
190  )
191  )
192  );
193 
194  }
195 
203  public function deleteInstance() {
204 
205  $this->requirePerm('can_admin');
206 
207  /* eliminazione items */
208  Article::deleteInstance($this);
209  /* eliminazione categorie */
210  Category::deleteInstance($this);
211 
212  /* eliminazione da tabella opzioni */
213  $opt_id = $this->_db->getFieldFromId($this->_tbl_opt, "id", "instance", $this->_instance);
214  \Gino\Translation::deleteTranslations($this->_tbl_opt, $opt_id);
215  $result = $this->_db->delete($this->_tbl_opt, "instance=".$this->_instance);
216 
217  /* eliminazione file css */
218  $classElements = $this->getClassElements();
219  foreach($classElements['css'] as $css) {
220  unlink(APP_DIR.OS.$this->_class_name.OS.\Gino\baseFileName($css)."_".$this->_instance_name.".css");
221  }
222 
223  /* eliminazione views */
224  foreach($classElements['views'] as $k => $v) {
225  unlink($this->_view_dir.OS.\Gino\baseFileName($k)."_".$this->_instance_name.".php");
226  }
227 
228  /* eliminazione cartelle contenuti */
229  foreach($classElements['folderStructure'] as $fld=>$fldStructure) {
230  \Gino\deleteFileDir($fld.OS.$this->_instance_name, true);
231  }
232 
233  return TRUE;
234  }
235 
244  public static function outputFunctions() {
245 
246  $list = array(
247  "last" => array("label"=>_("Lista utime news"), "permissions"=>array()),
248  "archive" => array("label"=>_("Lista news paginata"), "permissions"=>array()),
249  "showcase" => array("label"=>_("Vetrina"), "permissions"=>array()),
250  "feedRSS" => array("label"=>_("Feed RSS"), "permissions"=>array())
251  );
252 
253  return $list;
254  }
255 
260  public function getImageWidth() {
261  return $this->_image_width;
262  }
263 
271  public function download(\Gino\Http\Request $request) {
272 
273  $doc_id = cleanVar($request->GET, 'id', 'int');
274  if($doc_id) {
275  $n = new Article($doc_id, $this);
276  if(!$n->id) {
277  throw new \Gino\Exception\Exception404();
278  }
279  if($n->private && !$this->userHasPerm('can_view_private')) {
280  throw new \Gino\Exception\Exception403();
281  }
282  $attachment = $n->attachment;
283  if($attachment) {
284  $full_path = $this->getBaseAbsPath().OS.'attachment'.OS.$attachment;
285  return \Gino\download($full_path); // restituisce un \Gino\Http\ResponseFile
286  }
287  else {
288  throw new \Gino\Exception\Exception404();
289  }
290  }
291  else {
292  throw new \Gino\Exception\Exception404();
293  }
294  }
295 
300  public function showcase() {
301 
302  if($this->userHasPerm('can_view_private')) {
303  $private = TRUE;
304  }
305  else {
306  $private = FALSE;
307  }
308 
309  $this->_registry->addCss($this->_class_www."/news_".$this->_instance_name.".css");
310  $this->_registry->addJs($this->_class_www."/news.js");
311 
312  $where_arr = array("instance='".$this->_instance."' AND published='1'");
313  if(!$private) {
314  $where_arr[] = "private='0'";
315  }
316 
317  $news = Article::objects($this, array('where' => implode(' AND ', $where_arr), 'order'=>'date DESC, insertion_date DESC', 'limit'=>array(0, $this->_showcase_news_number)));
318 
319  $view = new View($this->_view_dir);
320 
321  $view->setViewTpl('showcase_'.$this->_instance_name);
322  $dict = array(
323  'instance_name' => $this->_instance_name,
324  'feed_url' => $this->link($this->_instance_name, 'feedRSS'),
325  'news' => $news,
326  'autostart' => $this->_showcase_auto_start,
327  'autointerval' => $this->_showcase_auto_interval
328  );
329 
330  return $view->render($dict);
331  }
332 
337  public function last() {
338 
339  $request = \Gino\Http\Request::instance();
340 
341  $private = $this->userHasPerm('can_view_private') ? TRUE : FALSE;
342 
343  $title_site = $this->_registry->sysconf->head_title;
344  $module = new ModuleInstance($this->_instance);
345  $title = $module->label.' | '.$title_site;
346 
347  $this->_registry->addCss($this->_class_www."/news_".$this->_instance_name.".css");
348  $this->_registry->addHeadLink(array(
349  'rel' => 'alternate',
350  'type' => 'application/rss+xml',
351  'title' => \Gino\jsVar($title),
352  'href' => $request->root_absolute_url.$this->link($this->_instance_name, 'feedRSS')
353  ));
354 
355  $where_arr = array("published='1'");
356  if(!$private) {
357  $where_arr[] = "private='0'";
358  }
359 
360  $news = Article::objects($this, array('where' => implode(' AND ', $where_arr), 'order'=>'date DESC, insertion_date DESC', 'limit'=>array(0, $this->_last_news_number)));
361 
362  $view = new View($this->_view_dir, 'last_'.$this->_instance_name);
363 
364  $dict = array(
365  'instance_name' => $this->_instance_name,
366  'news' => $news,
367  'feed_url' => $this->link($this->_instance_name, 'feedRSS'),
368  'archive_url' => $this->link($this->_instance_name, 'archive')
369  );
370 
371  return $view->render($dict);
372 
373  }
374 
382  public function detail(\Gino\Http\Request $request) {
383 
384  $slug = \Gino\cleanVar($request->GET, 'id', 'string');
385 
386  $item = Article::getFromSlug($slug, $this);
387 
388  if(!$item || !$item->id || !$item->published) {
389  throw new \Gino\Exception\Exception404();
390  }
391  if($item->private && !$this->userHasPerm('can_view_private')) {
392  throw new \Gino\Exception\Exception403();
393  }
394 
395  $this->_registry->addCss($this->_class_www."/news_".$this->_instance_name.".css");
396 
397  $view = new view($this->_view_dir, 'detail_'.$this->_instance_name);
398 
399  $dict = array(
400  'instance_name' => $this->_instance_name,
401  'news' => $item,
402  'related_contents_list' => $this->relatedContentsList($item),
403  'social' => \Gino\shareAll('st_all_large', $this->link($this->_instance_name, 'detail', array('id' => $item->slug))),
404  );
405 
406  $document = new \Gino\Document($view->render($dict));
407  return $document();
408 
409  }
410 
416  public function relatedContentsList($item)
417  {
418  $related_contents = GTag::getRelatedContents('Article', $item->id);
419  if(count($related_contents)) {
420  $view = new View(null, 'related_contents_list');
421  return $view->render(array('related_contents' => $related_contents));
422  }
423  else return '';
424  }
425 
431  public function archive(\Gino\Http\Request $request) {
432 
433  $this->_registry->addCss($this->_class_www."/news_".$this->_instance_name.".css");
434 
435  $ctgslug = \Gino\cleanVar($request->GET, 'ctg', 'string');
436 
437  if($this->userHasPerm('can_view_private')) {
438  $private = TRUE;
439  }
440  else {
441  $private = FALSE;
442  }
443 
444  if($ctgslug) {
445  $ctg = Category::getFromSlug($ctgslug, $this);
446  $ctg_id = $ctg ? $ctg->id : 0;
447  }
448  else {
449  $ctg = null;
450  $ctg_id = 0;
451  }
452 
453  $news_number = Article::getCount($this, array('published' => TRUE, 'private'=>$private, 'ctg'=>$ctg_id));
454 
455  $paginator = Loader::load('Paginator', array($news_number, $this->_list_nfp));
456  $limit = $paginator->limitQuery();
457 
458  $where_arr = array("instance='".$this->_instance."' AND published='1'");
459  if($ctg_id) {
460  $where_arr[] = "id IN (SELECT article_id FROM ".Article::$table_ctgs." WHERE category_id='".$ctg_id."')";
461  }
462  if(!$private) {
463  $where_arr[] = "private='0'";
464  }
465 
466  $news = Article::objects($this, array('where'=>implode(' AND ', $where_arr), 'order'=>'date DESC, insertion_date DESC', 'limit'=>$limit));
467 
468  $view = new View($this->_view_dir);
469  $view->setViewTpl('archive_'.$this->_instance_name);
470  $dict = array(
471  'instance_name' => $this->_instance_name,
472  'feed_url' => $this->link($this->_instance_name, 'feedRSS'),
473  'ctg' => $ctg,
474  'news' => $news,
475  'pagination' => $paginator->pagination()
476  );
477 
478  $document = new \Gino\Document($view->render($dict));
479  return $document();
480 
481  }
482 
488  public function manageDoc(\Gino\Http\Request $request) {
489 
490  $this->requirePerm(array('can_admin', 'can_publish', 'can_write'));
491 
492  $block = \Gino\cleanVar($request->GET, 'block', 'string');
493 
494  $link_frontend = sprintf('<a href="%s">%s</a>', $this->linkAdmin(array(), 'block=frontend'), _('Frontend'));
495  $link_options = sprintf('<a href="%s">%s</a>', $this->linkAdmin(array(), 'block=options'), _('Opzioni'));
496  $link_ctg = sprintf('<a href="%s">%s</a>', $this->linkAdmin(array(), 'block=ctg'), _('Categorie'));
497  $link_dft = sprintf('<a href="%s">%s</a>', $this->linkAdmin(), _('Contenuti'));
498  $sel_link = $link_dft;
499 
500  if($block == 'frontend' && $this->userHasPerm('can_admin')) {
501  $backend = $this->manageFrontend();
502  $sel_link = $link_frontend;
503  }
504  elseif($block == 'options' && $this->userHasPerm('can_admin')) {
505  $backend = $this->manageOptions();
506  $sel_link = $link_options;
507  }
508  elseif($block == 'ctg') {
509  $backend = $this->manageCategory($request);
510  $sel_link = $link_ctg;
511  }
512  else {
513  $backend = $this->manageNews($request);
514  }
515 
516  if(is_a($backend, '\Gino\Http\Response')) {
517  return $backend;
518  }
519 
520  // groups privileges
521  if($this->userHasPerm('can_admin')) {
522  $links_array = array($link_frontend, $link_options, $link_ctg, $link_dft);
523  }
524  else {
525  $links_array = array($link_ctg, $link_dft);
526  }
527 
528  $module = ModuleInstance::getFromName($this->_instance_name);
529 
530  $view = new View(null, 'tab');
531  $dict = array(
532  'title' => \Gino\htmlChars($module->label),
533  'links' => $links_array,
534  'selected_link' => $sel_link,
535  'content' => $backend
536  );
537 
538  $document = new \Gino\Document($view->render($dict));
539  return $document();
540 
541  }
542 
548  private function manageCategory(\Gino\Http\Request $request) {
549 
550  $admin_table = new AdminTable($this, array());
551 
552  $backend = $admin_table->backOffice(
553  'Category',
554  array(
555  'list_display' => array('id', 'name', 'slug'),
556  'list_title' => _("Elenco categorie"),
557  'list_description' => "<p>"._('Ciascuna news inserita potrà essere associata ad una o più categorie qui definite.')."</p>",
558  ),
559  array(),
560  array(
561  'description' => array(
562  'widget' => 'editor',
563  'notes' => FALSE,
564  'img_preview' => TRUE,
565  ),
566  'image' => array(
567  'preview' => TRUE
568  )
569  )
570  );
571 
572  return $backend;
573  }
574 
580  private function manageNews(\Gino\Http\Request $request) {
581 
582  if(!$this->userHasPerm('can_admin') and !$this->userHasPerm('can_publish')) {
583  $remove_fields = array('published');
584  $delete_deny = 'all';
585  }
586  else {
587  $remove_fields = array();
588  $delete_deny = array();
589  }
590 
591  $admin_table = new AdminTable($this, array('delete_deny'=>$delete_deny));
592 
593  $backend = $admin_table->backOffice(
594  'Article',
595  array(
596  'list_display' => array('id', 'date', 'categories', 'title', 'published', 'private'),
597  'list_title' => _("Elenco news"),
598  'filter_fields' => array('categories', 'title', 'published')
599  ),
600  array(
601  'removeFields' => $remove_fields
602  ),
603  array(
604  'text' => array(
605  'widget' => 'editor',
606  'notes' => FALSE,
607  'img_preview' => FALSE,
608  ),
609  'img' => array(
610  'preview' => TRUE
611  )
612  )
613  );
614 
615  return $backend;
616  }
617 
626  public function searchSite() {
627 
628  $private = $this->userHasPerm('can_view_private') ? TRUE : FALSE;
629 
630  return array(
631  "table"=>Article::$table,
632  "selected_fields"=>array("id", "slug", "date", array("highlight"=>true, "field"=>"title"), array("highlight"=>true, "field"=>"text")),
633  "required_clauses"=>$private ? array("instance"=>$this->_instance, 'published'=>1) : array("instance"=>$this->_instance, 'private'=>0, 'published'=>1),
634  "weight_clauses"=>array("title"=>array("weight"=>3), "text"=>array("weight"=>1))
635  );
636  }
637 
644  public function searchSiteResult($results) {
645 
646  $obj = new Article($results['id'], $this);
647 
648  $buffer = "<div>".\Gino\dbDatetimeToDate($results['date'], "/")." <a href=\"".$this->link($this->_instance_name, 'detail', array('id'=>$results['slug']))."\">";
649  $buffer .= $results['title'] ? \Gino\htmlChars($results['title']) : \Gino\htmlChars($obj->ml('title'));
650  $buffer .= "</a></div>";
651 
652  if($results['text']) {
653  $buffer .= "<div class=\"search_text_result\">...".\Gino\htmlChars($results['text'])."...</div>";
654  }
655  else {
656  $buffer .= "<div class=\"search_text_result\">".\Gino\htmlChars(\Gino\cutHtmlText($obj->ml('text'), 120, '...', false, false, false, array('endingPosition'=>'in')))."</div>";
657  }
658 
659  return $buffer;
660 
661  }
662 
667  public function systemNewsletterList() {
668 
669  $news = Article::objects($this, array('where' => "instance='".$this->_instance."'", 'order'=>'date DESC, insertion_date DESC', 'limit'=>array(0, $this->_newsletter_news_number)));
670 
671  $items = array();
672  foreach($news as $n) {
673  $items[] = array(
674  _('id') => $n->id,
675  _('titolo') => \Gino\htmlChars($n->ml('title')),
676  _('privata') => $n->private ? _('si') : _('no'),
677  _('pubblicata') => $n->published ? _('si') : _('no'),
678  _('data') => \Gino\dbDateToDate($n->date),
679  );
680  }
681 
682  return $items;
683  }
684 
690  public function systemNewsletterRender($id) {
691 
692  $n = new Article($id, $this);
693 
694  $view = new View($this->_view_dir, 'newsletter_'.$this->_instance_name);
695  $dict = array(
696  'news' => $n,
697  );
698 
699  return $view->render($dict);
700 
701  }
702 
708  public function feedRSS(\Gino\Http\Request $request) {
709 
710  $title_site = $this->_registry->sysconf->head_title;
711  $module = new ModuleInstance($this->_instance);
712  $title = $module->label.' | '.$title_site;
713  $description = $module->description;
714 
715  $news = Article::objects($this, array('where' => "instance='".$this->_instance."' AND private='0' AND published='1'", 'order'=>'date DESC, insertion_date DESC', 'limit'=>array(0, 50)));
716 
717  $view = new \Gino\View($this->_view_dir, 'feed_rss_'.$this->_instance_name);
718  $dict = array(
719  'title' => $title,
720  'description' => $description,
721  'request' => $request,
722  'news' => $news
723  );
724 
725  $response = new \Gino\Http\Response($view->render($dict));
726  $response->setContentType('text/xml');
727  return $response;
728  }
729 
730 }
searchSiteResult($results)
Definisce la presentazione del singolo item trovato a seguito di ricerca (modulo "Ricerca nel sito") ...
Definition: class_news.php:644
showcase()
Frontend vetrina news.
Definition: class_news.php:300
detail(\Gino\Http\Request $request)
Front end dettaglio news.
Definition: class_news.php:382
$_tbl_opt
Tabella di opzioni.
Definition: class_news.php:86
$_list_nfp
numero di news per pagina nella vista elenco news
Definition: class_news.php:56
archive(\Gino\Http\Request $request)
Frontend archivio news.
Definition: class_news.php:431
$_last_news_number
numero di ultime news
Definition: class_news.php:51
$_showcase_auto_interval
animazione vetrina intervallo animazione
Definition: class_news.php:71
getImageWidth()
Getter larghezza di ridimensionamenteo delle immagini.
Definition: class_news.php:260
systemNewsletterRender($id)
Contenuto di una news quanto inserita in una newsletter.
Definition: class_news.php:690
feedRSS(\Gino\Http\Request $request)
Genera un feed RSS standard che presenta le ultime 50 news pubblicate.
Definition: class_news.php:708
download(\Gino\Http\Request $request)
Esegue il download clientside del documento indicato da url ($doc_id)
Definition: class_news.php:271
searchSite()
Metodo per la definizione di parametri da utilizzare per il modulo "Ricerca nel sito".
Definition: class_news.php:626
static getCount($controller, $options=null)
Restituisce il numero di news che soddisfano le condizioni date.
manageCategory(\Gino\Http\Request $request)
Interfaccia di amministrazione delle categorie.
Definition: class_news.php:548
$_showcase_news_number
numero di news nella vetrina
Definition: class_news.php:61
static getClassElements()
Restituisce alcune proprietà della classe utili per la generazione di nuove istanze.
Definition: class_news.php:166
Classe tipo Gino.Model che rappresenta una singola news.
__construct($mdlId)
Costruisce un'istanza di tipo news.
Definition: class_news.php:94
relatedContentsList($item)
Lista di contenuti correlati per tag.
Definition: class_news.php:416
$_newsletter_news_number
Numero ultime news esportate in lista newsletter.
Definition: class_news.php:81
deleteInstance()
Eliminazione istanza.
Definition: class_news.php:203
manageNews(\Gino\Http\Request $request)
Interfaccia di amministrazione delle news.
Definition: class_news.php:580
static $table
Classe di tipo Gino.Controller per la gestione di news categorizzate.
Definition: class_news.php:46
last()
Front end ultime news.
Definition: class_news.php:337
static outputFunctions()
Definizione dei metodi pubblici che forniscono un output per il front-end.
Definition: class_news.php:244
$_showcase_auto_start
animazione vetrina start automatico
Definition: class_news.php:66
$_image_width
Massima larghezza immagini.
Definition: class_news.php:76
systemNewsletterList()
Adattatore per la classe newsletter.
Definition: class_news.php:667
manageDoc(\Gino\Http\Request $request)
Interfaccia di amministrazione del modulo.
Definition: class_news.php:488
Namespace dell'applicazione News