123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
- namespace app\modules\ldjson;
- use Yii;
- use yii\base\BaseObject;
- use yii\helpers\Html;
- use yii\helpers\Url;
- use Masterminds\HTML5;
- class jsonLD extends BaseObject{
- public static $author=array();
- public static $publisher=array();
- public static $ld=array(
- "@context"=>"https://schema.org",
- "@type"=>"NewsArticle"
- );
- public static $wp = array(
- "@context"=>"https://schema.org",
- "@type"=>"WebPage"
- );
- public static $person = array(
- );
- public static $identifier = array(
- "@type"=>"PropertyValue",
- "propertyID"=>"WebPage",
- );
- public static $faq = array(
- "@context"=>"https://schema.org",
- "@type"=>"FAQPage",
- "mainEntity" => []
- );
- public static $about=array();
- public static $breadcrumb=array();
- public static $init = false;
- public static $ffaq = false;
- public function jsonLD(){
- }
- public function init(){
- self::$init = false;
- }
- public static function addAmicORG(){
- self::$init = true;
- self::$publisher=array(
- "@type"=>"NewsMediaOrganization",
- "name"=> Yii::$app->params['orgname'],
- "legalName"=>"Информационное агентство Амител",
- "masthead"=>"https://www.amic.ru/about",
- "actionableFeedbackPolicy"=>"https://www.amic.ru/feedback/",
- "logo"=>array(
- "@type"=>"ImageObject",
- "url"=>Yii::$app->urlManager->hostInfo.'/img/logo-google.png'
- ),
- // "description"=>'Новости Барнаула и Алтайского края, новости мира и России самое интересное. Новостное агенство ООО "Амител"',
- "email"=>"news@amic.ru",
- "telephone"=>"(7-385-2) 27-18-18",
- "url"=>Yii::$app->urlManager->hostInfo
- );
- }
- //sameAs
- public static function story($name){
- self::$about[] = $name;
- }
- public static function author($name, $type = true){
- self::$author["name"] = $name;
- if( $type ){
- self::$author["@type"] = "Person";
- }else{
- self::$author["@type"] = "Organization";
- }
- }
- public static function publisher($name){
- $publisher["name"] = $name;
- }
- public static function addBreadcrumb( $lavel, $url, $name ){
- $lb = array( 'position'=>$lavel, '@id'=>$url, 'name'=>$name );
- self::$breadcrumb[] = $lb;
- }
- //_big_640.jpg _size1
- public static function news( $title, $description, $image, $dtpub, $url, $uid='', $text='', $alt_title='' ){
- self::$wp["@id"] = $url;
- self::$identifier['value'] = $uid;
- $a = array(
- "@type"=>"Person",
- "headline"=>$title,
- "url"=>Yii::$app->urlManager->hostInfo.$url,
- "mainEntityOfPage"=>$url,
- "datePublished"=>date("c", strtotime($dtpub)),
- "description"=>$description,
- "@id"=>Yii::$app->urlManager->hostInfo.$url,
- "image"=>$image,
- "mainEntityOfPage"=>$url //self::$wp
- );
- //alternativeHeadline
- //dateModified
- //dateCreate
- if( $uid ){
- $a['identifier'] = self::$identifier;
- }
- if( $text ){
- $text = trim($text);
- $text = str_replace(["\n","\r"," "],'',$text);
- $text = str_replace([" "],' ',$text);
- $a['articleBody'] = strip_tags( $text );
- }
- if( $alt_title ){
- $a['alternativeHeadline'] = htmlspecialchars( strip_tags($alt_title) );
- }
- self::$ld = self::$ld + $a;
- }
- public static function addfaq( $text, $headline ){
- self::$faq['headline'] = $headline;
- $html5 = new HTML5();
- $dom = $html5->loadHTML($text);
- $el = $dom->getElementsByTagName('div');
- $divs = iterator_to_array($el);
- foreach ($divs as $div) {
- if($div->getAttribute('class') == 'Card-fon'){
- $flag = 0;
- $Question = '';
- $Answer = '';
- $card = $div->getElementsByTagName('h2');
- $divcard = iterator_to_array($card);
- foreach ($divcard as $cdiv) {
- if($cdiv->getAttribute('class') == 'Card-vnutr'){
- if( strstr( $cdiv->textContent, '?') !== false ){
- $Question = $cdiv->textContent;
- $flag++;
- }
- }
- }
- if( $flag == 0 ){
- $card = $div->getElementsByTagName('div');
- $divcard = iterator_to_array($card);
- foreach ($divcard as $cdiv) {
- if($cdiv->getAttribute('class') == 'Card-vnutr'){
- if( strstr( $cdiv->textContent, '?') !== false ){
- $Question = $cdiv->textContent;
- $flag++;
- }
- }
- }
- }
- if( $flag ){
- $card = $div->getElementsByTagName('div');
- $divcard = iterator_to_array($card);
- $Answer = '';
- foreach ($divcard as $cdiv) {
- if($cdiv->getAttribute('class') == 'Card-text'){
- $Answer .= $cdiv->textContent;
- $flag++;
- }
- }
- }
- if( $flag > 1 ){
- $a = [
- "@type" => "Question",
- "name" => $Question,
- "acceptedAnswer" => [
- "@type" => "Answer",
- "text"=>$Answer
- ]
- ];
- self::$faq['mainEntity'][] = $a;
- self::$ffaq = true;
- }
- }
- }
- }
- public static function person( $title, $description, $jobTitle, $image, $url, $birthDate = '' ){
- $text = strip_tags(trim($description));
- $text = str_replace(["\n","\r"," "],'',$text);
- $text = str_replace([" "],' ',$text);
- $a = array(
- "name"=>$title,
- "url"=>$url,
- "description"=>$text,
- "image"=>$image,
- "jobTitle"=>htmlspecialchars($jobTitle)
- );
- if( $birthDate && $birthDate != '1970-01-01' && strtotime($birthDate) > 10){
- $a['birthDate'] = date("c", strtotime($birthDate));
- }
- self::$person = self::$person + $a;
- }
- public static function showtest(){
- $a = self::$ld;
- if( self::$author ) $a["author"] = self::$author;
- $a["publisher"] = self::$publisher;
- if( count( self::$person ) ){
- $a['@type'] = "Person";
- $a += self::$person;
- }
- if( count( self::$about ) ){
- $labout = array();
- foreach( self::$about as $name ){
- $labout[] = array( "@type" => "InformAction", "name" => $name );
- }
- $a["about"] = $labout;
- }
- if( count( self::$breadcrumb ) ){
- $labout = array();
- foreach( self::$breadcrumb as $item ){
- $labout[] = array( "@type" => "ListItem", 'position' => $item['position'], 'item' => array( "@id" => $item['@id'], "name" => $item['name'] ) );
- }
- $aa = array(
- "@type" => "BreadcrumbList",
- "itemListElement" => $labout
- );
- //$a = $a + $aa;
- $a = array_diff_key( $a, array( '@context' => 'https://schema.org' ) );
- $b = array("@context" => 'https://schema.org', "@graph" => array( $a, $aa ));
- }else{
- $a = array_diff_key( $a, array( '@context' => 'https://schema.org' ) );
- $b = array("@context" => 'https://schema.org', "@graph" => array( $a ));
- }
- echo "<!-- debug\n".self::_format_json(json_encode( $b, JSON_UNESCAPED_UNICODE ), false)." \n-->\n";
- if( self::$ffaq ){
- $aa = array(
- "@context" => 'https://schema.org'
- );
- echo "<!-- debug\n".self::_format_json(json_encode( $aa+self::$faq, JSON_UNESCAPED_UNICODE ), false)." \n-->\n";
- }
- }
- public static function show(){
- $a = self::$ld;
- $a["author"] = self::$author;
- $a["publisher"] = self::$publisher;
- echo json_encode( $a );
- }
- public static function get(){
- if( self::$init == false ) return '';
- $a = self::$ld;
- if( count( self::$person ) ){
- $a['@type'] = "Person";
- $a += self::$person;
- }else{
- $a["author"] = self::$author;
- $a["publisher"] = self::$publisher;
- }
- if( count( self::$about ) ){
- $labout = array();
- foreach( self::$about as $name ){
- $labout[] = array( "@type" => "InformAction", "name" => $name );
- }
- $a["about"] = $labout;
- }
- /*
- $a = $this->ld;
- $a["author"] = $this->author;
- $a["publisher"] = $this->publisher;
- if( count( $this->about ) ){
- $labout = array();
- foreach( $this->about as $name ){
- $labout[] = array( "@type" => "Event", "name" => $name );
- }
- $a["about"] = $labout;
- }
- */
- if( count( self::$breadcrumb ) ){
- $labout = array();
- foreach( self::$breadcrumb as $item ){
- $labout[] = array( "@type" => "ListItem", 'position' => $item['position'], 'item' => array( "@id" => $item['@id'], "name" => $item['name'] ) );
- }
- $aa = array(
- "@context" => 'https://schema.org',
- "@type" => "BreadcrumbList",
- "itemListElement" => $labout
- );
- //$a = $a + $aa;
- // $a = array_diff_key( $a, array( '@context' => 'https://schema.org' ) );
- // $b = array( "@graph" => array( $aa, $a ));
- }
- $r = '<script type="application/ld+json">'.json_encode( $a, JSON_UNESCAPED_UNICODE ).'</script>'."\n";
- $r .= ( isset($aa) )?'<script type="application/ld+json">'.json_encode( $aa, JSON_UNESCAPED_UNICODE ).'</script>'."\n":'';
- if( self::$ffaq ){
- $aaa = array(
- "@context" => 'https://schema.org'
- );
- $r .='<script type="application/ld+json">'.json_encode( $aaa+self::$faq, JSON_UNESCAPED_UNICODE ).'</script>'."\n";
- }
- return $r;
- }
- public static function _format_json($json, $html = false) {
- $tabcount = 0;
- $result = '';
- $inquote = false;
- $ignorenext = false;
- if ($html) {
- $tab = " ";
- $newline = "<br/>";
- } else {
- $tab = "\t";
- $newline = "\n";
- }
- for($i = 0; $i < strlen($json); $i++) {
- $char = $json[$i];
- if ($ignorenext) {
- $result .= $char;
- $ignorenext = false;
- } else {
- switch($char) {
- case '[':
- case '{':
- $tabcount++;
- $result .= $char . $newline . str_repeat($tab, $tabcount);
- break;
- case ']':
- case '}':
- $tabcount--;
- $result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char;
- break;
- case ',':
- $result .= $char . $newline . str_repeat($tab, $tabcount);
- break;
- case ':':
- $result .= $char . " ";
- break;
- case '"':
- $inquote = !$inquote;
- $result .= $char;
- break;
- case '\\':
- if ($inquote) $ignorenext = true;
- $result .= $char;
- break;
- default:
- $result .= $char;
- }
- }
- }
- return str_replace(': //','://',str_replace('\/','/',$result));
- }
- public static function addBreadcrumbList()
- {
- $view = Yii::$app->getView();
- $breadcrumbList = [];
- if (isset($view->params['breadcrumbs'])) {
- $position = 1;
- foreach ($view->params['breadcrumbs'] as $breadcrumb) {
- if (is_array($breadcrumb)) {
- $breadcrumbList[] = (object)[
- "@type" => "http://schema.org/ListItem",
- "http://schema.org/position" => $position,
- "http://schema.org/item" => (object)[
- "@id" => Url::to($breadcrumb['url'], true),
- "http://schema.org/name" => $breadcrumb['label'],
- ]
- ];
- } else {
- // Is it ok to omit URL here or not? Google is not clear on that:
- // http://stackoverflow.com/questions/33688608/how-to-markup-the-last-non-linking-item-in-breadcrumbs-list-using-json-ld
- $breadcrumbList[] = (object)[
- "@type" => "http://schema.org/ListItem",
- "http://schema.org/position" => $position,
- "http://schema.org/item" => (object)[
- "http://schema.org/name" => $breadcrumb,
- ]
- ];
- }
- $position++;
- }
- }
- $doc = (object)[
- "@type" => "http://schema.org/BreadcrumbList",
- "http://schema.org/itemListElement" => $breadcrumbList
- ];
- JsonLDHelper::add($doc);
- }
- }
|