ProcessWire - Strucured Data

Aus Wikizone
Wechseln zu: Navigation, Suche

Möglichkeiten mit ProcessWire strukturierte Daten zu erzeugen.

Test mit Job-Posting[Bearbeiten]

job.php (Template)

<?php namespace ProcessWire;
include('includes/SchemaHelper.php');
$additionalHeaderData .= '
	<style>
		pre{background: #ccc; border: 1px solid black; padding: 2rem;}
	</style>
';
$sh = new SchemaHelper;
$jsonld = $sh->jsonldJobPosting($page);

$applyForm = '';
$apply = '';
$callToAction = '';
$content = '';
$copyField = '';

$apply .= '
<div>
	<a href="#apply-form" rel="nofollow noopener" target="_self" class="button primary">
	    Jetzt bewerben
	</a>
</div>
<div id="apply" class="">
	<h1>'.$page->title.'</h1>
	'.$page->body.'
	<h3> Alle Details zu dieser Stelle auf einen Blick</h3>
	<table class="">
		<tbody>
';
$apply .='
			<tr>
				<th>Anzeigenart</th>
				<td>Stellenangebot</td>
			</tr>
';
$apply .='
			<tr>
				<th>Arbeitgeber</th>
				<td>'.$page->organization_name.'<br>'.$page->link.'</td>
			</tr>
';
if($page->minValue && $page->maxValue && $page->time_unit){
	$apply .='
				<tr>
					<th>Vergütung</th>
					<td>'.$page->minValue.'€ - '.$page->maxValue.'€ '.$page->time_unit->title.'</td>
				</tr>
	';
}else if($page->minValue && $page->time_unit){
	$apply .='
				<tr>
					<th>Vergütung</th>
					<td>'.$page->minValue.'€ '.$page->unit->title.'</td>
				</tr>
	';
}
// Arbeitsort
$apply .='
			<tr>
				<th>Arbeitsort</th>
				<td>
					'.$page->fp_postal_address->streetAddress.'<br>
					'.$page->fp_postal_address->postalCode.' '.$page->fp_postal_address->addressLocality.'
					</td>
			</tr>
';

$apply .= '
		</tbody>
	</table>
</div>
';

$applyForm .= '
<div id="apply-form" class="">
	<h2>Bewerbung</h2>
	'.$page->body2.'
</div>
';

$callToAction .='
<div>
	<a href="#apply-form" rel="nofollow noopener" target="_self" class="button primary">
		Jetzt bewerben
	</a>
</div>
';

$copyField = '
'.$callToAction.'
'.$apply.'
'.$applyForm.'
';

$copyField2 = '
'.$jsonld.'
';

$copyField = '
<h4>HTML<h4>
<pre class="code">
'.htmlspecialchars($copyField).'
</pre>
';

$copyField2 = '
<h4>JsonLD</h4>
<pre class="code">
'.htmlspecialchars($copyField2).'
</pre>
';

$content .= $callToAction.$apply.$applyForm.$copyField2.$copyField.$jsonld;

	/* @TODO weitere Eigenschaften:
	 * - Aus- und Weiterbildung
	 * - Berufskategorie (z.B. Handel, Vertrieb, Verkauf / Handelsvertretung)
	 * - Berufliche Praxis (z.B. mit Berufserfahrung)
	 * -
	 */
	?>

SchemaHelper.php

<?php namespace ProcessWire;
// @todo outsource some of the nessecary types (i.e. place) in backend use
// fieldgroups for that maybe s.th. like $place = getFg('schema_place')
class SchemaHelper{
	// Load the requested schema from the schemas directory and register the class
	public function jsonldJobPosting ($p) {
		$jsonld = array();
		$sanitizer = wire('sanitizer');
		//set unset fields (for not mandatory fields)
		$p->validThrough = $p->validThrough ? : ''; // todo test

		$jsonld["@context"] = "http://schema.org/";
		$jsonld["@type"] = "JobPosting";
		if($p->link) $jsonld["url"] = $p->link;
		else $jsonld["url"] = $sanitizer->url($p->httpUrl);
		$jsonld["datePosted"] = !empty($p->datePosted) ? date(DATE_ISO8601, strtotime($p->datePosted)) : date(DATE_ISO8601, strtotime($p->created));
		$jsonld["validThrough"] = date(DATE_ISO8601, strtotime($p->validThrough));
		$jsonld["description"] = $sanitizer->textarea($p->body);
		$jsonld['hiringOrganization'] = array(
			"@type" => "Organization",
			"name" => $sanitizer->text($p->organization_name),
			"sameAs" => $sanitizer->url($p->link)
		);
		if($p->link2)$jsonld['hiringOrganization']['logo'] = $p->link2;
		$jsonld['title'] = $sanitizer->text($p->title);

		if($p->minValue && $p->maxValue && $p->time_unit){
			$jsonld['baseSalary'] = array(
				"@type" => "MonetaryAmount",
				"currency" => "EUR",
				"value" => array(
					"@type" => "QuantitativeValue",
					"minValue" => $sanitizer->float($p->minValue),
					"maxValue" => $sanitizer->float($p->maxValue),
					"unitText" => $sanitizer->text($p->time_unit->value)
				)
			);
		}else if($p->minValue && $p->time_unit){
			$jsonld['baseSalary'] = array(
				"@type" => "MonetaryAmount",
				"currency" => "EUR",
				"value" => array(
					"@type" => "QuantitativeValue",
					"value" => $sanitizer->float($p->minValue),
					"unitText" => $sanitizer->text($p->time_unit->value)
				)
			);
		}
		$jsonld['jobLocation'] = array(
			"@type" => "Place",
			"address" => array(
				"@type" => "PostalAddress",
				"streetAddress" => $sanitizer->text($p->fp_postal_address->streetAddress),
				"addressLocality" => $sanitizer->text($p->fp_postal_address->addressLocality),
				"addressRegion" => $sanitizer->text($p->fp_postal_address->addressRegion),
				"postalCode" => $sanitizer->text($p->fp_postal_address->postalCode),
				"addressCountry" => $sanitizer->text($p->fp_postal_address->addressCountry),
			)
		);
		// recommended properties

		if($c = count($p->employmentType)){
			$types = array();
			foreach($p->employmentType as $type){
				$types[] = $sanitizer->text($type->value);
			}
			//var_dump($c);
			if($c > 1) $jsonld['employmentType'] = $types;
			else $jsonld['employmentType'] = $types[0];
		}

		//$out .= '<script type="application/ld+json">' . json_encode($jsonld) . '</script>';
		$out = '<script type="application/ld+json">' . json_encode($jsonld,JSON_PRETTY_PRINT) . '</script>';

		return $out;
	}

}