"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 "\n";
if( self::$ffaq ){
$aa = array(
"@context" => 'https://schema.org'
);
echo "\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 = ''."\n";
$r .= ( isset($aa) )?''."\n":'';
if( self::$ffaq ){
$aaa = array(
"@context" => 'https://schema.org'
);
$r .=''."\n";
}
return $r;
}
public static function _format_json($json, $html = false) {
$tabcount = 0;
$result = '';
$inquote = false;
$ignorenext = false;
if ($html) {
$tab = " ";
$newline = "
";
} 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);
}
}