Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Content.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * This class represents content taken from Gentics Content Connector.
11  * The main purpose is to store content attributes in cache
12  * and in case cache expired or crashed reload this attributes.
13  */
14 class Content
15 {
16  private $_path;
17 
18  private $_attributes;
19 
20  private $_personalisationAttributes;
21 
22  private $_cachePrefix = 'content_';
23 
24  private $_source;
25 
26  /**
27  * Content active path
28  *
29  * @return string
30  */
31  public function getPath()
32  {
33  return $this->_path;
34  }
35 
36  /**
37  * Content file location
38  *
39  * @return string
40  */
41  public function getFile()
42  {
43  return $this->_source->contentFile($this->getPath());
44  }
45 
46  /**
47  * Save content attributes to cache
48  *
49  * @param array $attributes content attributes
50  *
51  * @return void
52  */
53  public function setAttributes($attributes)
54  {
55  $this->_attributes = $attributes;
56  if ($this->_source->usePersonalisation) {
57  Yii::app()->cache->set($this->_cachePrefix . $this->_path, $attributes, Yii::app()->getModule('contentSource')->cacheTime);
58  }
59  }
60 
61  /**
62  * Get content attributes from cache if $this->_source->usePersonalization== true
63  * If cache is empty but personalisation enabled, request api for content attributes
64  *
65  * @throws Exception
66  * @return mixed
67  */
68  public function getAttributes()
69  {
70  if ($this->_attributes === null && $this->_source->usePersonalisation) {
71  $this->_attributes = Yii::app()->cache->get($this->_cachePrefix . $this->_path);
72  if ($this->_attributes === false) {
73  $attributes = $this->_source->getRepositoryApi()->getContentAttributes($this->_path, $this->_source->getRequestedAttributes());
74  if ($attributes !== false) {
75  $this->setAttributes($attributes);
76  } else {
77  $this->_attributes = false;
78  }
79  }
80  }
81 
82  return $this->_attributes;
83  }
84 
85  /**
86  * Get content personalisation attributes
87  *
88  * @return mixed
89  */
90  public function getPersonalisationAttributes()
91  {
92  if ($this->_personalisationAttributes === null) {
93  $this->_personalisationAttributes = array();
94  if ($attributes = $this->getAttributes()) {
95  foreach ($this->_source->personalisationFields as $pf) {
96  if (isset($attributes[$pf]) && is_array($attributes[$pf])) {
97  $this->_personalisationAttributes = array_merge($this->_personalisationAttributes, $attributes[$pf]);
98  }
99  }
100  }
101  }
102  return $this->_personalisationAttributes;
103  }
104 
105  /**
106  * Constructor
107  *
108  * @param ContentSource $source content source. Used for getting current personalisation configuration
109  * @param string $path content active path
110  * @param array $attributes content attributes
111  *
112  * @return \Content
113  */
114  public function __construct(ContentSource $source, $path, $attributes = null)
115  {
116  $this->_source = $source;
117  $this->_path = $path;
118  if ($attributes !== null) {
119  $this->setAttributes($attributes);
120  }
121  }
122 
123  /**
124  * Check if user has access to this content
125  *
126  * @param object $user user
127  *
128  * @return bool
129  */
130  public function hasAccess($user)
131  {
132  if ($this->_source->usePersonalisation) {
133  $hasAccess = Yii::app()->getModule('personalisation')->rule->checkAccess($user->id, $this->getPersonalisationAttributes());
134  Yii::trace("Check personalisation access. Result: [$hasAccess].".print_r($this->getPersonalisationAttributes(), true),'personalisation');
135  return $hasAccess;
136  }
137  return true;
138  }
139 
140 }