-
Notifications
You must be signed in to change notification settings - Fork 2
/
ESEOModelBehavior.php
79 lines (73 loc) · 2.03 KB
/
ESEOModelBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* ESeoModelBehavior class file.
*
* @author Dmitry Zasjadko <[email protected]>
* @link https://github.com/segoddnja/YiiSEOBehavior
*/
/**
* Provides SEO functionality for a model.
*
* @version 1.0
* @package YiiSeoBehavior
*/
require_once 'models/SeoData.php';
class ESEOModelBehavior extends CActiveRecordBehavior
{
/**
* @var SeoData seo data for current owner
*/
private $_seoData;
public function afterSave($event)
{
if(isset($_POST['SeoData']))
{
$this->seoData->attributes = $_POST['SeoData'];
}
if($this->seoData->title || $this->seoData->keywords || $this->seoData->description)
$this->seoData->save();
else
if($this->seoData->isNewRecord === false)
$this->seoData->delete();
return parent::afterSave($event);
}
/*
* return SeoData model for current owner
* @return SeoData
*/
public function getSeoData()
{
if(!$this->_seoData)
{
$ownerPK = $this->getOwnerPK();
$ownerClass = get_class($this->owner);
//gets SEO data for owner model
if($ownerPK != null)
$this->_seoData = SeoData::model()->findByPk(array(
'model_name' => $ownerClass,
'model_id' => $ownerPK
));
//if no SEO data, then create new record
if(!$this->_seoData)
{
$this->_seoData = new SeoData();
$this->_seoData->setAttributes(array(
'model_name' => $ownerClass,
'model_id' => $ownerPK
));
}
}
return $this->_seoData;
}
/*
* Return owner model PK, converted to string
* @return String
*/
private function getOwnerPK(){
if(is_array($this->owner->primaryKey))
return implode ('.', $this->owner->primaryKey);
else
return $this->owner->primaryKey;
}
}
?>