Created
June 29, 2017 06:35
-
-
Save poznet/98269b3995c75d6f9b49583bfa55e191 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Bolt\Extension\Poznet\Archive; | |
use Bolt; | |
use Bolt\Events\CronEvents; | |
use Symfony\Component\HttpFoundation\Request; | |
class Extension extends \Bolt\BaseExtension | |
{ | |
private $iledni=30; //ilosc dni po ktorych wpisy zostanďż˝ przeniesione | |
private $id_archiwum=17; | |
private $strony=array(); | |
private $wpisy=array(); | |
private $doprzeniesienia=array(); | |
public function getStrony($slug){ | |
$query = 'select * from bolt_taxonomy where contenttype="strony" and taxonomytype="kategorie" and slug="'.$slug.'"'; | |
$stmt = $this->app['db']->query($query); | |
$stmt->execute(); | |
$result=$stmt->fetchAll(); //kategorie 1wszego poziomu | |
foreach($result as $wynik){ | |
array_push($this->strony,$wynik['content_id']); | |
} | |
foreach($this->strony as $id){ | |
$query = 'select * from bolt_relations where from_contenttype="strony" and to_contenttype="strony" and to_id=:id'; | |
$stmt = $this->app['db']->prepare($query); | |
$stmt->bindValue("id",$id); | |
$stmt->execute(); | |
$result=$stmt->fetchAll(); | |
foreach($result as $wynik) { | |
array_push($this->strony, $wynik['from_id']); | |
} | |
} | |
return true; | |
} | |
public function getWpisy($strony){ | |
foreach($strony as $id){ | |
$query = 'select * from bolt_relations where from_contenttype="wpisy" and to_contenttype="strony" and to_id=:id'; | |
$stmt = $this->app['db']->prepare($query); | |
$stmt->bindValue("id",$id); | |
$stmt->execute(); | |
$result=$stmt->fetchAll(); | |
foreach($result as $wynik) { | |
array_push($this->wpisy, $wynik['from_id']); | |
} | |
$this->wpisy=array_unique($this->wpisy); | |
} | |
return true; | |
} | |
public function filtrWpisy(){ | |
foreach($this->wpisy as $id){ | |
$query = 'select * from bolt_wpisy where id=:id'; | |
$stmt = $this->app['db']->prepare($query); | |
$stmt->bindValue("id",$id); | |
$stmt->execute(); | |
$result=$stmt->fetchAll(); | |
foreach($result as $wynik) { | |
if($wynik['archiwum']==true) | |
array_push($this->doprzeniesienia, $wynik['id']); | |
$created=new \DateTime($wynik['datecreated']); | |
$teraz=new \DateTime('now'); | |
$roznica=$teraz->diff($created)->days; | |
if($roznica>$this->iledni) | |
array_push($this->doprzeniesienia, $wynik['id']); | |
} | |
} | |
return true; | |
} | |
public function setArchiveFlag(){ | |
$query = 'select * from bolt_relations where from_contenttype="wpisy" and to_contenttype="strony" and to_id=:id'; | |
$stmt = $this->app['db']->prepare($query); | |
$stmt->bindValue("id",$this->id_archiwum); | |
$stmt->execute(); | |
$result=$stmt->fetchAll(); | |
foreach($result as $wynik) { | |
$id=$wynik['from_id']; | |
$query = 'update bolt_wpisy set archiwum=1 where id=:id'; | |
$stmt = $this->app['db']->query($query); | |
$stmt->bindValue("id",$id); | |
$stmt->execute(); | |
} | |
} | |
public function przenies(){ | |
foreach($this->doprzeniesienia as $id){ | |
$idek=$this->id_archiwum; | |
$query = 'update bolt_relations set to_id="'.$idek.'" where from_id=:id'; | |
$stmt = $this->app['db']->prepare($query); | |
$stmt->bindValue("id",$id); | |
$stmt->execute(); | |
$result=$stmt->fetchAll(); | |
} | |
return true; | |
} | |
public function run(){ | |
$this->setUp(); | |
$this->getStrony('aktualnosci'); | |
$this->getWpisy($this->strony); | |
$this->filtrWpisy(); | |
$this->moreThan(5); | |
$this->przenies(); | |
$this->setArchiveFlag(); | |
return true; | |
} | |
public function getName() | |
{ | |
return "Archive"; | |
} | |
public function setUp(){ | |
$query = 'select * from bolt_strony where slug="archiwum" limit 1'; | |
$stmt = $this->app['db']->query($query); | |
$stmt->execute(); | |
$result=$stmt->fetchAll(); //kategorie 1wszego poziomu | |
foreach($result as $wynik){ | |
$this->id_archiwum=$wynik['id']; | |
} | |
$this->checkYear(date('Y')); | |
} | |
public function checkYear($y){ | |
$kategorie=$this->getSubCategories($this->id_archiwum); | |
$jest=false; | |
foreach($kategorie as $kat){ | |
//from_id; | |
$id=$kat['from_id']; | |
$strona=$this->getStrona($id); | |
if ($strona['title']==$y){ | |
$jest=true; | |
return $strona['id']; | |
break; | |
} | |
} | |
if($jest===false) | |
{ | |
$id=$this->addSubCategory($y,$this->id_archiwum); | |
return $id; | |
} | |
} | |
public function addSubCategory($what,$where){ | |
$now=new \DateTime("now"); | |
$now= $now->format('Y-m-d G:i:s'); | |
$data=new \DateTime("01-01-".$what); | |
$data= $data->format('Y-m-d G:i:s'); | |
$query = 'insert into bolt_strony(title,slug,datecreated,datechanged,status,ownerid,datepublish,templatefields,slider,pliki) | |
values("'.$what.'","'.$what.'","'.$now.'","'.$now.'","published","1","'.$data.'","[]","[]","[]");'; | |
$stmt = $this->app['db']->prepare($query); | |
// $stmt->bindValue("id",$id); | |
$stmt->execute(); | |
$idek=$this->app['db']->lastInsertId(); | |
$query = 'insert into bolt_relations(from_contenttype,from_id,to_contenttype,to_id) VALUES | |
("strony","'.$idek.'","strony","'.$where.'");'; | |
$stmt = $this->app['db']->prepare($query); | |
// $stmt->bindValue("id",$id); | |
$stmt->execute(); | |
return $idek; | |
} | |
public function getSubCategories($id){ | |
$query = 'select * from bolt_relations where from_contenttype="strony" and to_contenttype="strony" and to_id=:id'; | |
$stmt = $this->app['db']->prepare($query); | |
$stmt->bindValue("id",$id); | |
$stmt->execute(); | |
$result=$stmt->fetchAll(); | |
return $result; | |
} | |
public function getStrona($id){ | |
$query = 'select * from bolt_strony where id=:id limit 1'; | |
$stmt = $this->app['db']->query($query); | |
$stmt->bindValue("id",$id); | |
$stmt->execute(); | |
$result=$stmt->fetchAll(); //kategorie 1wszego poziomu | |
foreach($result as $wynik){ | |
return $wynik; | |
} | |
} | |
public function moreThan($x){ | |
$this->getStrony('aktualnosci'); | |
$this->getWpisy($this->strony); | |
arsort($this->wpisy); | |
$i=0; | |
foreach($this->wpisy as $id){ | |
$i++; | |
if($i>$x) array_push($this->doprzeniesienia, $id); | |
} | |
} | |
public function initialize() | |
{ | |
$this->app['dispatcher']->addListener(CronEvents::CRON_DAILY, array($this, 'run')); | |
$request=Request::createFromGlobals(); | |
//$this->run(); | |
if(substr($request->getRequestUri(),0,9)=="/archiwum") { | |
$this->addTwigFunction('archiwum', 'backend'); | |
}else{ | |
$this->addTwigFunction('archiwum', 'frontend'); | |
} | |
//podpina controller | |
$this->app->mount('/archiwum/' , new Controller\ArchiwumController($this->app)); | |
} | |
public function backend(){ | |
$a=new Archiwum($this->app['db']); | |
$lata=$a->getYearsFromWpisy(); | |
$txt='<li class="menucenter"><ul>'; | |
foreach($lata as $rok){ | |
$txt.='<li class="menucenter " ><a href="#" class="show-rok" data-id="'.$rok.'">'.$rok.'</a> '; | |
$m=$a->getMonthsFromYear($rok); | |
if( count($m)>0){ | |
$txt.='<ul class="archiwum_rok" data-rok="'.$rok.'">'; | |
foreach($m as $miesiac){ | |
if($a->getMonthAsString((integer)$miesiac)!='') | |
$txt.='<li class="menucenter"><a href="/archiwum/'.$rok.'/'.$miesiac.'">'.$a->getMonthAsString((integer)$miesiac).'</a> '; | |
} | |
$txt.='</ul>'; | |
} | |
} | |
$txt.='</ul></li>'; | |
return $txt; | |
} | |
public function frontend(){ | |
return '<li class="menucenter"><a href="/archiwum/">Archiwum</a></li>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment