Completed
Push — master ( ae8931...2d50a9 )
by Leo
02:38
created

SHBlock::forTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
class SHBlock extends DataObject {
4
	protected static $db = array (
5
		'SortOrder'			=>	'Int',
6
		'Title'				=>	'Varchar(64)',
7
		'TitleWrapper'		=>	'Enum("h2,h3,h4,h5,h6")',
8
		'hideTitle'			=>	'Boolean',
9
		'showBlockbyClass'	=>	'Boolean',
10
		'Description'		=>	'Varchar(128)',
11
		'MemberVisibility'	=>	'Varchar(255)',
12
		'shownInClass'		=>	'Text',
13
		'addMarginTop'		=>	'Boolean',
14
		'addMarginBottom'	=>	'Boolean'
15
	);
16
	
17
	protected static $many_many = array (
18
		'Pages'				=>	'Page'
19
	);
20
		
21
	protected static $create_table_options = array(
22
		'MySQLDatabase'		=> 'ENGINE=MyISAM'
23
    );
24
	
25
	protected static $extensions = array (
26
		'StandardPermissions'
27
	);
28
	
29
	protected static $summary_fields = array(
30
		'BlockType',
31
		'Title', 
32
		'Description',
33
		'shownOn',
34
		'VisibleTo'
35
	);
36
	
37
	protected static $field_labels = array(
38
		'BlockType'			=>	'Block type',
39
		'shownOn'			=>	'is shown on',
40
		'VisibleTo'			=>	'Visible to'
41
	);
42
	
43
	public function VisibleTo() {
44 View Code Duplication
		if (strlen(trim($this->MemberVisibility)) > 0) {
0 ignored issues
show
Documentation introduced by
The property MemberVisibility does not exist on object<SHBlock>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
			$lists = 'Group: ' . str_replace(',','<br />Group: ', $this->MemberVisibility);
0 ignored issues
show
Documentation introduced by
The property MemberVisibility does not exist on object<SHBlock>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
		}else{
47
			$lists = '<em>&lt;All&gt;</em>';
48
		}
49
		
50
		return new LiteralField('VisibleTo',$lists);
51
	}
52
	
53
	public function BlockType() {
54
		return $this->singular_name();
55
	}
56
	
57
	public function shownOn() {
58
		if ($this->showBlockbyClass) {
0 ignored issues
show
Documentation introduced by
The property showBlockbyClass does not exist on object<SHBlock>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
59 View Code Duplication
			if (strlen(trim($this->shownInClass)) > 0) {
0 ignored issues
show
Documentation introduced by
The property shownInClass does not exist on object<SHBlock>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
				$lists = 'Type: ' . str_replace(',','<br />Type: ', $this->shownInClass);
0 ignored issues
show
Documentation introduced by
The property shownInClass does not exist on object<SHBlock>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
			}else{
62
				$lists = '<em>&lt;not assigned&gt;</em>';
63
			}
64
		}else{
65
			if ($this->Pages()->count() > 0) {
0 ignored issues
show
Documentation Bug introduced by
The method Pages does not exist on object<SHBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
66
				$lists = 'Page: ' . implode('<br />Page: ', $this->Pages()->column('Title'));
0 ignored issues
show
Documentation Bug introduced by
The method Pages does not exist on object<SHBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
67
			}else{
68
				$lists = '<em>&lt;not assigned&gt;</em>';
69
			}
70
		}
71
		return new LiteralField('shownOn',$lists);
72
	}
73
	
74
	public function getCMSFields() {
75
		$fields = parent::getCMSFields();
76
		$fields->removeFieldFromTab('Root', 'Pages');
77
		$fields->removeFieldsFromTab('Root.Main', array(
78
			'SortOrder',
79
			'showBlockbyClass',
80
			'shownInClass',
81
			'MemberVisibility'
82
		));
83
		
84
		$memberGroups = Group::get();
85
		$sourcemap = $memberGroups->map('Code', 'Title');
86
		$source = array(
87
			'anonymous'		=>	'Anonymous visitors'
88
		);
89
		foreach ($sourcemap as $mapping => $key) {
90
			$source[$mapping] = $key;
91
		}
92
		
93
		$memberVisibility = new CheckboxSetField(
94
			$name = "MemberVisibility",
95
			$title = "Show block for specific groups",
96
			$source
97
		);
98
		
99
		$memberVisibility->setDescription('Show this block only for the selected group(s). If you select no groups, the block will be visible to all members.');
100
		
101
		$availabelClasses = $this->availableClasses();
102
		$inClass = new CheckboxSetField(
103
			$name = "shownInClass",
104
			$title = "Show block for specific content types",
105
			$availabelClasses
106
		);
107
		
108
		$filterSelector = OptionsetField::create(
109
			'showBlockbyClass',
110
			'Choose filter set',
111
			array(
112
				'0'			=>	'by page',
113
				'1'			=>	'by page/data type'
114
			)
115
		)->setDescription('<p><br /><strong>by page</strong>: block will be displayed in the selected page(s)<br /><strong>by page/data type</strong>: block will be displayed on the pages created with the particular page/data type. e.g. is <strong>"InternalPage"</strong> is picked, the block will be displayed, and will ONLY be displayed on all <strong>Internal Pages</strong></p>');
116
		
117
		$availablePages = Page::get()->exclude('ClassName', array(
118
			'ErrorPage',
119
			'RedirectorPage',
120
			'VirtualPage'
121
		));
122
		$pageSelector = new CheckboxSetField(
123
			$name = "Pages",
124
			$title = "Show on Page(s)",
125
			$availablePages->map('ID','Title')
126
		);
127
		
128
		
129
		if ($this->canConfigPageAndType(Member::currentUser())) {
0 ignored issues
show
Bug introduced by
The method canConfigPageAndType() does not exist on SHBlock. Did you maybe mean config()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
130
			$fields->addFieldsToTab('Root.VisibilitySettings', array(
131
				$filterSelector,
132
				$pageSelector,
133
				$inClass
134
			));
135
		}
136
		
137
		if ($this->canConfigMemberVisibility(Member::currentUser())) {
0 ignored issues
show
Bug introduced by
The method canConfigMemberVisibility() does not exist on SHBlock. Did you maybe mean config()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
138
			$fields->addFieldToTab('Root.VisibilitySettings', $memberVisibility);
139
		}
140
		
141
		if (!$fields->fieldByName('Options')) {
142
			$fields->insertBefore($right = RightSidebar::create('Options'), 'Root');
0 ignored issues
show
Documentation introduced by
'Root' is of type string, but the function expects a object<FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
143
	    }
144
	
145
	    $fields->addFieldsToTab('Options', array(
146
			CheckboxField::create('addMarginTop', 'add "margin-top" class to block wrapper'),
147
			CheckboxField::create('addMarginBottom', 'add "margin-bottom" class to block wrapper')
148
	    ));
149
150
		return $fields;
151
	}
152
	
153
	public function availableClasses() {
154
		$Classes = array_diff(
155
			ClassInfo::subclassesFor('Page'),
156
			ClassInfo::subclassesFor('RedirectorPage'),
157
			ClassInfo::subclassesFor('VirtualPage')
158
		);
159
		return $Classes;
160
	}
161
	
162
	public function forTemplate() {
163
		if ($this->canDisplayMemberCheck()) {
164
			return $this->renderWith(array($this->getClassName(), 'BaseBlock'));
165
		}
166
		
167
		return false;
168
	}
169
	
170
	public function canDisplayMemberCheck() {
171
		$rawVisibility = $this->MemberVisibility;
0 ignored issues
show
Documentation introduced by
The property MemberVisibility does not exist on object<SHBlock>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
172
		
173
		if (empty($rawVisibility)) {
174
			return true;
175
		}
176
		
177
		$visibility = explode(',', $rawVisibility);
178
		$member = Member::currentUser();
179
		
180
		if (!$member && in_array('anonymous', $visibility)) {
181
			return true;
182
		}
183
		
184
		if ($member) {
185
			$memberGroups = $member->Groups()->column('Code');
186
			foreach ($memberGroups as $memberGroup) {
187
				if (in_array($memberGroup, $visibility)) {
188
					return true;
189
				}
190
			}
191
		}
192
		
193
		return false;
194
	}
195
	
196
	public function frontendEditable() {
197
		$member = Member::currentUser();		
198
		return $this->canEdit($member);
199
	}
200
	
201
	public function Type2Class() {
202
		return strtolower(str_replace(' ', '-', $this->singular_name()));
203
	}
204
}