gino-news  2.1.0
Modulo News per gino CMS
class.Article.php
Go to the documentation of this file.
1 <?php
12 namespace Gino\App\News;
13 
14 use \Gino\ManyToManyField;
15 use \Gino\BooleanField;
16 use \Gino\TagField;
17 use \Gino\DatetimeField;
18 use \Gino\ImageField;
19 use \Gino\FileField;
20 use \Gino\SlugField;
21 use \Gino\Db;
22 use \Gino\Link;
23 
32 class Article extends \Gino\Model {
33 
34  protected static $_extension_img = array('jpg', 'jpeg', 'png');
35  protected static $_extension_attachment = array('pdf', 'doc', 'xdoc', 'odt', 'xls', 'csv', 'txt');
36  public static $table = 'news_article';
37  public static $table_ctgs = 'news_article_category';
38 
45  function __construct($id, $instance) {
46 
47  $this->_controller = $instance;
48  $this->_tbl_data = self::$table;
49 
50  $this->_fields_label = array(
51  'insertion_date'=>_('Data inserimento'),
52  'last_edit_date'=>_('Data ultima modifica'),
53  'date'=>_('Data'),
54  'categories'=>_("Categorie"),
55  'title'=>_("Titolo"),
56  'slug'=>array(_("Slug"), _('utilizzato per creare un permalink alla risorsa')),
57  'text'=>_('Testo'),
58  'tags'=>array(_('Tag'), _("elenco separato da virgola")),
59  'img'=>_('Immagine'),
60  'attachment'=>_('Allegato'),
61  'private'=>array(_('Privata'), _('le news private sono visualizzabili solamente dagli utenti che hanno il permesso \'visualizzazione news private\'')),
62  'social'=>_('Condivisione social networks'),
63  'published'=>_('Pubblicata'),
64  );
65 
66  parent::__construct($id);
67 
68  $this->_model_label = _('Articolo');
69  }
70 
75  function __toString() {
76  return (string) $this->title;
77  }
78 
86  public function structure($id) {
87 
88  $structure = parent::structure($id);
89 
90  $structure['categories'] = new ManyToManyField(array(
91  'name' => 'categories',
92  'model' => $this,
93  'm2m' => '\Gino\App\News\Category',
94  'm2m_where' => 'instance=\''.$this->_controller->getInstance().'\'',
95  'm2m_controller' => $this->_controller,
96  'join_table' => self::$table_ctgs,
97  'add_related' => TRUE,
98  'add_related_url' => $this->_controller->linkAdmin(array(), 'block=ctg&insert=1')
99  ));
100 
101  $structure['slug'] = new SlugField(array(
102  'name' => 'slug',
103  'model' => $this,
104  'required' => TRUE,
105  'autofill' => array('date', 'title')
106  ));
107 
108  $structure['tags'] = new TagField(array(
109  'name' => 'tags',
110  'model' => $this,
111  'model_controller_class' => 'news',
112  'model_controller_instance' => $this->_controller->getInstance()
113  ));
114 
115  $structure['published'] = new BooleanField(array(
116  'name' => 'published',
117  'model' => $this,
118  'enum' => array(1 => _('si'), 0 => _('no')),
119  ));
120 
121  $structure['private'] = new BooleanField(array(
122  'name' => 'private',
123  'model' => $this,
124  'enum' => array(1 => _('si'), 0 => _('no'))
125  ));
126 
127  $structure['social'] = new BooleanField(array(
128  'name' => 'social',
129  'model' => $this,
130  'enum' => array(1 => _('si'), 0 => _('no'))
131  ));
132 
133  $structure['insertion_date'] = new DatetimeField(array(
134  'name' => 'insertion_date',
135  'model' => $this,
136  'auto_now' => FALSE,
137  'auto_now_add' => TRUE
138  ));
139 
140  $structure['last_edit_date'] = new DatetimeField(array(
141  'name' => 'last_edit_date',
142  'model' => $this,
143  'auto_now' => TRUE,
144  'auto_now_add' => TRUE
145  ));
146 
147  $base_path = $this->_controller->getBaseAbsPath() . OS . 'img';
148  $structure['img'] = new ImageField(array(
149  'name' => 'img',
150  'model' => $this,
151  'extensions' => self::$_extension_img,
152  'path' => $base_path,
153  'resize' => TRUE,
154  'thumb' => FALSE,
155  'width' => $this->_controller->getImageWidth()
156  ));
157 
158  $base_path = $this->_controller->getBaseAbsPath() . OS . 'attachment';
159  $structure['attachment'] = new FileField(array(
160  'name' => 'attachment',
161  'model' => $this,
162  'extensions' => self::$_extension_attachment,
163  'path' => $base_path,
164  'check_type' => FALSE
165  ));
166 
167  return $structure;
168  }
169 
174  public function objCategories() {
175 
176  $res = array();
177  foreach($this->categories as $ctgid) {
178  $res[] = new Category($ctgid, $this->_controller);
179  }
180 
181  return $res;
182 
183  }
184 
192  public static function getCount($controller, $options = null) {
193 
194  $res =0;
195 
196  $private = \Gino\gOpt('private', $options, FALSE);
197  $published = \Gino\gOpt('published', $options, TRUE);
198  $ctg = \Gino\gOpt('ctg', $options, false);
199 
200  $db = Db::instance();
201  $selection = 'COUNT(id) AS tot';
202  $table = self::$table;
203  $where_arr = array("instance='".$controller->getInstance()."'");
204  if(!$private) {
205  $where_arr[] = "private='0'";
206  }
207  if($published) {
208  $where_arr[] = "published='1'";
209  }
210  if($ctg) {
211  $where_arr[] = "id IN (SELECT article_id FROM ".self::$table_ctgs." WHERE category_id='".$ctg."')";
212  }
213  $where = implode(' AND ', $where_arr);
214 
215  $rows = $db->select($selection, $table, $where, null, null);
216 
217  if($rows && count($rows)) {
218  $res = $rows[0]['tot'];
219  }
220 
221  return $res;
222 
223  }
224 
229  public function getUrl() {
230 
231  return $this->_controller->link($this->_controller->getInstanceName(), 'detail', array('id' => $this->slug));
232  }
233 
238  public function getImgPath() {
239 
240  return $this->_controller->getBasePath().'/img/'.$this->img;
241  }
242 
247  public function getAttachmentPath() {
248 
249  return $this->_controller->getBasePath().'/attachment/'.$this->attachment;
250  }
251 
256  public function attachmentDownloadUrl() {
257 
258  return $this->_controller->link($this->_controller->getInstanceName(), 'download', array('id' => $this->id));
259  }
260 
266  public function dateIso()
267  {
268  $datetime = new \Datetime($this->date);
269  return $datetime->format('c');
270  }
271 }
dateIso()
Data in formato iso 8601.
static $table_ctgs
objCategories()
Lista di oggetti categoria associati alla news.
attachmentDownloadUrl()
Path relativo al download dell'allegato.
structure($id)
Sovrascrive la struttura di default.
static $_extension_img
static getCount($controller, $options=null)
Restituisce il numero di news che soddisfano le condizioni date.
Classe tipo Gino.Model che rappresenta una singola news.
getImgPath()
Path relativo dell'immagine associata.
static $_extension_attachment
__construct($id, $instance)
Costruttore.
static $table
Classe di tipo Gino.Model che rappresenta una categoria di news.
__toString()
Rappresentazione a stringa dell'oggetto.
getUrl()
Url relativo al dettaglio della news.
getAttachmentPath()
Path relativo dell'allegato.
Namespace dell'applicazione News