Skip to content

Commit

Permalink
valid types added
Browse files Browse the repository at this point in the history
  • Loading branch information
jirisvoboda committed Jun 4, 2015
1 parent e735425 commit cbb46d9
Showing 1 changed file with 76 additions and 16 deletions.
92 changes: 76 additions & 16 deletions src/OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,58 @@
* OpenGraph meta data
* @see http://ogp.me
*/
class OpenGraph {
class OpenGraph extends \yii\base\Component {

/**
* Types
*/
// activities
const TYPE_ACTIVITY = 'activity';
const TYPE_SPORT = 'sport';
// bussiness
const TYPE_BAR = 'bar';
const TYPE_COMPANY = 'company';
const TYPE_CAFE = 'cafe';
const TYPE_HOTEL = 'hotel';
const TYPE_RESTAURANT = 'restaurant';
// groups
const TYPE_CAUSE = 'cause';
const TYPE_SPORTS_LEAGUE = 'sports_league';
const TYPE_SPORTS_TEAM = 'sports_team';
// organizations
const TYPE_BAND = 'band';
const TYPE_GOVERNMENT = 'government';
const TYPE_NON_PROFIT = 'non_profit';
const TYPE_SCHOOL = 'school';
const TYPE_UNIVERSITY = 'university';
// people
const TYPE_ACTOR = 'actor';
const TYPE_ATHLETE = 'athlete';
const TYPE_AUTHOR = 'author';
const TYPE_DIRECTOR = 'director';
const TYPE_MUSICIAN = 'musician';
const TYPE_POLITICIAN = 'politician';
const TYPE_PROFILE = 'profile';
const TYPE_PUBLIC_FIGURE = 'public_figure';
// places
const TYPE_CITY = 'city';
const TYPE_COUNTRY = 'country';
const TYPE_LANDMARK = 'landmark';
const TYPE_STATE_PROVINCE = 'state_province';
// products & entertainment
const TYPE_ALBUM = 'album';
const TYPE_BOOK = 'book';
const TYPE_DRINK = 'drink';
const TYPE_FOOD = 'food';
const TYPE_GAME = 'game';
const TYPE_MOVIE = 'movie';
const TYPE_PRODUCT = 'product';
const TYPE_SONG = 'song';
const TYPE_TV_SHOW = 'tv_show';
// websites
const TYPE_ARTICLE = 'article';
const TYPE_BLOG = 'blog';
const TYPE_WEBSITE = 'website';

/**
* @var string The title of your object as it should appear within the graph, e.g., "The Rock"
Expand Down Expand Up @@ -70,34 +121,43 @@ class OpenGraph {
*/
public function __construct()
{
$this->init();

\Yii::$app->view->on(View::EVENT_BEGIN_PAGE, function() {
return $this->handlePageBegin();
});
}

/**
* @inheritdoc
*/
public function init()
{
// default initialization goes here
$this->site_name = \Yii::$app->name;
$this->locale = \Yii::$app->language;
}

/**
* Handles page begin event
*/
protected function handlePageBegin()
{
\Yii::$app->controller->view->registerMetaTag(['property' => 'og:title', 'content' => $this->title], 'og:title');
\Yii::$app->controller->view->registerMetaTag(['property' => 'og:site_name', 'content' => $this->site_name], 'og:site_name');
\Yii::$app->controller->view->registerMetaTag(['property' => 'og:url', 'content' => $this->url], 'og:url');
\Yii::$app->controller->view->registerMetaTag(['property' => 'og:type', 'content' => $this->type], 'og:type');

// Locale issafe to be specifued since it has default value on Yii applications
\Yii::$app->controller->view->registerMetaTag(['property' => 'og:locale', 'content' => $this->locale], 'og:locale');
$properties = get_object_vars($this);

// Only add a description meta if specified
if ($this->description !== null)
foreach ($properties as $key => $value)
{
\Yii::$app->controller->view->registerMetaTag(['property' => 'og:description', 'content' => $this->description], 'og:description');
}
if ('locale_alternate' == $key)
{
$key = str_replace('_', ':', $key);
}

// Only add an image meta if specified
if ($this->image !== null)
{
\Yii::$app->controller->view->registerMetaTag(['property' => 'og:image', 'content' => $this->image], 'og:image');
if ($value)
{
$property = sprintf('og:%s', $key);

\Yii::$app->controller->view->registerMetaTag(['property' => $property, 'content' => $value], $property);
}
}
}
}

0 comments on commit cbb46d9

Please sign in to comment.