2020-01-14 22:25:43 +01:00
< ? php
2018-01-27 18:13:38 +01:00
2020-01-19 13:16:40 +01:00
class Books implements IWebsiteModule
2018-01-27 18:13:38 +01:00
{
2020-01-17 01:28:34 +01:00
/** @var Website */
private $site ;
2020-01-15 00:29:45 +01:00
/** @var array */
private $staticData ;
2020-01-17 01:28:34 +01:00
public function __construct ( Website $site )
2020-01-15 00:29:45 +01:00
{
2020-01-17 01:28:34 +01:00
$this -> site = $site ;
2020-01-15 00:29:45 +01:00
$this -> load ();
}
private function load ()
{
2020-01-15 02:50:23 +01:00
$all = require ( __DIR__ . '/../../statics/books/__all.php' );
2020-01-15 00:29:45 +01:00
$this -> staticData = array_map ( function ( $a ){ return self :: readSingle ( $a );}, $all );
}
2020-01-15 00:32:24 +01:00
private static function readSingle ( $a )
2018-01-27 18:13:38 +01:00
{
2020-01-20 18:11:06 +01:00
$a [ 'imgfront_url' ] = '/data/images/book_img/' . $a [ 'id' ] . '_front.png' ;
$a [ 'imgfront_path' ] = __DIR__ . '/../../data/images/book_img/' . $a [ 'id' ] . '_front.png' ;
2018-01-27 18:13:38 +01:00
2020-01-20 18:11:06 +01:00
$a [ 'imgfull_url' ] = '/data/images/book_img/' . $a [ 'id' ] . '_full.png' ;
$a [ 'imgfull_path' ] = __DIR__ . '/../../data/images/book_img/' . $a [ 'id' ] . '_full.png' ;
2018-01-27 18:13:38 +01:00
2020-01-20 18:11:06 +01:00
$a [ 'preview_url' ] = '/data/dynamic/bookprev_' . $a [ 'id' ] . '.png' ;
$a [ 'preview_path' ] = __DIR__ . '/../../data/dynamic/bookprev_' . $a [ 'id' ] . '.png' ;
2018-01-27 18:13:38 +01:00
2020-01-23 12:14:13 +01:00
$a [ 'file_readme' ] = ( __DIR__ . '/../../statics/books/' . $a [ 'id' ] . '.md' );
2018-01-28 01:29:25 +01:00
$a [ 'url' ] = '/books/view/' . $a [ 'id' ] . '/' . destructiveUrlEncode ( $a [ 'title' ]);
$a [ 'extraimages_urls' ] = [];
$a [ 'extraimages_paths' ] = [];
for ( $i = 1 ; $i <= $a [ 'imagecount' ]; $i ++ )
{
2020-01-19 13:16:40 +01:00
$a [ 'extraimages_urls' ] [] = '/data/images/book_img/' . $a [ 'id' ] . '_img' . $i . '.jpg' ;
$a [ 'extraimages_paths' ] [] = __DIR__ . '/../../data/images/book_img/' . $a [ 'id' ] . '_img' . $i . '.jpg' ;
2018-01-28 01:29:25 +01:00
}
2018-01-27 18:13:38 +01:00
2019-12-29 17:49:10 +01:00
$a [ 'book_count' ] = is_array ( $a [ 'pdf' ]) ? count ( $a [ 'pdf' ]) : 1 ;
2018-01-27 18:13:38 +01:00
return $a ;
}
2020-01-15 00:29:45 +01:00
public function listAll ()
2018-01-27 18:13:38 +01:00
{
2020-01-15 00:29:45 +01:00
return $this -> staticData ;
2018-01-27 18:13:38 +01:00
}
2020-01-15 00:29:45 +01:00
public function listAllNewestFirst ()
2018-01-27 18:13:38 +01:00
{
2020-01-15 00:29:45 +01:00
$data = $this -> staticData ;
2018-01-27 18:13:38 +01:00
usort ( $data , function ( $a , $b ) { return strcasecmp ( $b [ 'date' ], $a [ 'date' ]); });
return $data ;
}
2020-01-15 00:29:45 +01:00
public function checkConsistency ()
2018-01-27 18:13:38 +01:00
{
$warn = null ;
2020-01-15 00:29:45 +01:00
$this -> load ();
2018-01-27 18:13:38 +01:00
$ids = [];
2020-01-23 12:14:13 +01:00
foreach ( $this -> staticData as $book )
2018-01-27 18:13:38 +01:00
{
2020-01-23 12:14:13 +01:00
if ( in_array ( $book [ 'id' ], $ids )) return [ 'result' => 'err' , 'message' => 'Duplicate id ' . $book [ 'id' ]];
$ids [] = $book [ 'id' ];
2018-01-27 18:13:38 +01:00
2020-01-23 12:14:13 +01:00
if ( ! file_exists ( $book [ 'imgfront_path' ])) return [ 'result' => 'err' , 'message' => 'Image (Front) not found ' . $book [ 'title_short' ]];
if ( ! file_exists ( $book [ 'imgfull_path' ])) return [ 'result' => 'err' , 'message' => 'Image (Full) not found ' . $book [ 'title_short' ]];
2018-02-09 22:59:02 +01:00
2020-01-23 12:14:13 +01:00
foreach ( $book [ 'extraimages_paths' ] as $eipath )
2018-02-09 22:59:02 +01:00
{
2020-01-23 12:14:13 +01:00
if ( ! file_exists ( $eipath )) return [ 'result' => 'err' , 'message' => 'Extra-Image not found ' . $book [ 'title_short' ]];
2018-02-09 22:59:02 +01:00
}
2019-12-29 17:49:10 +01:00
2020-02-21 03:24:17 +01:00
if ( $book [ 'book_count' ] < 0 ) return [ 'result' => 'err' , 'message' => 'BookCount must be greater than zero ' . $book [ 'title_short' ]];
2019-12-29 17:49:10 +01:00
2020-01-23 12:14:13 +01:00
if ( $book [ 'book_count' ] > 1 && ! is_array ( $book [ 'pdf' ])) return [ 'result' => 'err' , 'message' => 'Attribute [pdf] must be an array ' . $book [ 'title_short' ]];
if ( $book [ 'book_count' ] > 1 && count ( $book [ 'pdf' ]) !== $book [ 'book_count' ]) return [ 'result' => 'err' , 'message' => 'Attribute [pdf] must be the correct size ' . $book [ 'title_short' ]];
if ( $book [ 'book_count' ] === 1 && ! is_string ( $book [ 'pdf' ])) return [ 'result' => 'err' , 'message' => 'Attribute [pdf] must be an string ' . $book [ 'title_short' ]];
2019-12-29 17:49:10 +01:00
2020-01-23 12:14:13 +01:00
if ( $book [ 'book_count' ] > 1 && ! is_array ( $book [ 'pages' ])) return [ 'result' => 'err' , 'message' => 'Attribute [pages] must be an array ' . $book [ 'title_short' ]];
if ( $book [ 'book_count' ] > 1 && count ( $book [ 'pages' ]) !== $book [ 'book_count' ]) return [ 'result' => 'err' , 'message' => 'Attribute [pages] must be the correct size ' . $book [ 'title_short' ]];
if ( $book [ 'book_count' ] === 1 && ! is_string ( $book [ 'pages' ])) return [ 'result' => 'err' , 'message' => 'Attribute [pages] must be an string ' . $book [ 'title_short' ]];
2018-01-27 18:13:38 +01:00
2020-01-23 12:14:13 +01:00
if ( ! file_exists ( $book [ 'file_readme' ])) return [ 'result' => 'err' , 'message' => 'Readme not found ' . $book [ 'title_short' ]];
2018-01-27 18:13:38 +01:00
2020-01-23 12:14:13 +01:00
if ( ! file_exists ( $book [ 'preview_path' ])) $warn = [ 'result' => 'warn' , 'message' => 'Preview not found ' . $book [ 'title_short' ]];
2018-01-27 18:13:38 +01:00
}
2020-01-23 12:14:13 +01:00
if ( $warn != null ) return $warn ;
2018-01-27 18:13:38 +01:00
return [ 'result' => 'ok' , 'message' => '' ];
}
2020-01-15 00:29:45 +01:00
public function createPreview ( $prog )
2018-01-27 18:13:38 +01:00
{
$src = $prog [ 'imgfront_path' ];
$dst = $prog [ 'preview_path' ];
2020-01-17 01:28:34 +01:00
if ( $this -> site -> config [ 'use_magick' ])
2018-02-03 23:49:44 +01:00
magick_resize_image ( $src , 200 , 0 , $dst );
else
smart_resize_image ( $src , 200 , 0 , true , $dst );
2018-01-27 18:13:38 +01:00
}
2018-01-28 01:29:25 +01:00
2020-01-15 00:29:45 +01:00
public function getBook ( $id )
2018-01-28 01:29:25 +01:00
{
foreach ( self :: listAll () as $book ) {
if ( $book [ 'id' ] == $id ) return $book ;
}
return null ;
}
2018-03-24 02:55:01 +01:00
2020-01-15 00:29:45 +01:00
public function getRepositoryHost ( $book )
2018-03-24 02:55:01 +01:00
{
$r = $book [ 'repository' ];
if ( startsWith ( $r , " http:// " )) $r = substr ( $r , strlen ( " http:// " ));
if ( startsWith ( $r , " https:// " )) $r = substr ( $r , strlen ( " https:// " ));
if ( startsWith ( $r , " www. " )) $r = substr ( $r , strlen ( " www. " ));
if ( startsWith ( strtolower ( $r ), " gitlab " )) return " Gitlab " ;
if ( startsWith ( strtolower ( $r ), " github " )) return " Github " ;
if ( startsWith ( strtolower ( $r ), " bitbucket " )) return " Bitbucket " ;
return " Online " ;
}
2020-01-23 12:14:13 +01:00
public function getREADME ( $book )
{
return file_get_contents ( $book [ 'file_readme' ]);
}
2018-01-27 18:13:38 +01:00
}