TildaController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace manager\controllers;
  3. use Yii;
  4. use manager\models\TildaAPI;
  5. class TildaController extends BaseController
  6. {
  7. const keylist = 'uactive';
  8. const project = 461598;
  9. public function actionIndex()
  10. {
  11. $cache = Yii::$app->cache;
  12. $ckey = "Tilda_ProjectsLis";
  13. if( !($proj = $cache->get( $ckey )) ){
  14. $api = $this->GetApi();
  15. $proj = $api->getProjectsList( self::project );
  16. $cache->set( $ckey, $proj, 60*10 );
  17. }
  18. return $this->render("index", ['proj'=>$proj]);
  19. }
  20. public function actionProj($id)
  21. {
  22. $cache = Yii::$app->cache;
  23. $ckey = "Tilda_PageLis_".$id;
  24. $ckey2 = "Tilda_PageInf_".$id;
  25. if( !($proj = $cache->get( $ckey )) ){
  26. $api = $this->GetApi();
  27. $proj = $api->getPagesList( $id );
  28. $cache->set( $ckey, $proj, 60*10 );
  29. }
  30. if( !($info = $cache->get( $ckey2 )) ){
  31. $api = isset($api)?$api:$this->GetApi();
  32. $info = $api->getProjectInfo($id);
  33. $cache->set( $ckey2, $info, 60*60 );
  34. }
  35. return $this->render("pages", ['pages'=>$proj,'info'=>$info]);
  36. }
  37. public function actionExport()
  38. {
  39. if (Yii::$app->request->isPost && $post = Yii::$app->request->post()){
  40. $api = $this->GetApi();
  41. return $this->render("pagesconf", ['pages'=>$post['Check'],'proj'=>$post['proj'], 'api'=>$api]);
  42. }
  43. return $this->redirect('index');
  44. }
  45. public function actionExportstart()
  46. {
  47. if (Yii::$app->request->isPost && $post = Yii::$app->request->post()){
  48. $api = $this->GetApi();
  49. return $this->render("export", ['pages'=>$post['Check'],'proj'=>$post['proj'], 'api'=>$api, 'main'=>$post['main'], 'url'=>$post['url']]);
  50. }
  51. return $this->redirect('index');
  52. }
  53. public function actionProcess()
  54. {
  55. if (Yii::$app->request->isPost && $post = Yii::$app->request->post()){
  56. $api = $this->GetApi();
  57. // $tildapage = unserialize($post['data']);
  58. $pageid = $post['data'];
  59. /* запрашиваем все данные для экспорта страницы */
  60. $tildapage = $api->getPageFullExport($pageid);
  61. if (! $tildapage || empty($tildapage['html'])) {
  62. echo "Error: cannot get page [$pageid] or page not publish<br>\n";
  63. return;
  64. }
  65. /* временный фикс */
  66. $html = preg_replace(array('|//static.tildacdn.com/js/|','|//static.tildacdn.com/css/|'),array('',''), $tildapage['html']);
  67. if ($html > '') {
  68. $tildapage['html'] = $html;
  69. }
  70. $tildapage['export_imgpath'] = $api->local->arProject['export_imgpath'];
  71. $tildapage['needsync'] = '0';
  72. //print_r($local->arSearchFiles);
  73. /* так как мы копировали общие файлы в одни папки, а в HTML они указывают на другие, то произведем замену */
  74. $html = preg_replace($api->local->arSearchFiles, $api->local->arReplaceFiles, $tildapage['html']);
  75. if ($html > '') {
  76. $html = str_replace( $api->local->getProjectDir() ,'',$html);
  77. $html = str_replace('href="/','href="',$html);
  78. $html = str_replace('src="/','src="',$html);
  79. $html = str_replace('src="/','src="//',$html);
  80. $html = str_replace('src="/yastatic.net','src="//yastatic.net',$html);
  81. $html = str_replace('rentafont_webfonts.js','../.js/rentafont_webfonts.js',$html);
  82. $html = str_replace('http://amic.ru/page'.$tildapage['id'],$api->local->getProjectDir().'/page'.$tildapage['id'],$html);
  83. $html = str_replace('https://amic.ru/page'.$tildapage['id'],$api->local->getProjectDir().'/page'.$tildapage['id'],$html);
  84. $html = str_replace('><',">\n<",$html);
  85. $tildapage['html'] = $html;
  86. }
  87. $tildapage = $api->local->savePage($tildapage);
  88. $tildapage = $api->local->saveMetaPage($tildapage);
  89. return json_encode( ['status'=>'ok', 'id'=>$tildapage['id']] );
  90. }
  91. return json_encode( ['status'=>'err'] );
  92. }
  93. public function actionView()
  94. {
  95. $api = $this->GetApi();
  96. return $this->render("view", ['api'=>$api]);
  97. }
  98. protected function GetApi()
  99. {
  100. return new TildaAPI( 'vre13190dtp27i45zo6g', 'ainf80ynghlc80dc1mdf');
  101. }
  102. }