-
Notifications
You must be signed in to change notification settings - Fork 6
/
Alias.php
132 lines (115 loc) · 4.06 KB
/
Alias.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* This file is part of MetaModels/attribute_alias.
*
* (c) 2012-2016 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels
* @subpackage AttributeAlias
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Andreas Isaak <info@andreas-isaak.de>
* @author Stefan Heimes <stefan_heimes@hotmail.com>
* @author Oliver Hoff <oliver@hofff.com>
* @copyright 2012-2016 The MetaModels team.
* @license https://github.com/MetaModels/attribute_alias/blob/master/LICENSE LGPL-3.0
* @filesource
*/
namespace MetaModels\Attribute\Alias;
use ContaoCommunityAlliance\Contao\Bindings\ContaoEvents;
use ContaoCommunityAlliance\Contao\Bindings\Events\Controller\ReplaceInsertTagsEvent;
use MetaModels\Attribute\BaseSimple;
/**
* This is the MetaModelAttribute class for handling text fields.
*
* @package MetaModels
* @subpackage AttributeAlias
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
*/
class Alias extends BaseSimple
{
/**
* {@inheritDoc}
*/
public function getSQLDataType()
{
return 'varchar(255) NOT NULL default \'\'';
}
/**
* {@inheritDoc}
*/
public function getAttributeSettingNames()
{
return array_merge(
parent::getAttributeSettingNames(),
array(
'alias_fields',
'isunique',
'force_alias',
'mandatory',
'alwaysSave',
'filterable',
'searchable',
'sortable'
)
);
}
/**
* {@inheritDoc}
*/
public function getFieldDefinition($arrOverrides = array())
{
$arrFieldDef = parent::getFieldDefinition($arrOverrides);
$arrFieldDef['inputType'] = 'text';
// W do not need to set mandatory, as we will automatically update our value when isunique is given.
if ($this->get('isunique')) {
$arrFieldDef['eval']['mandatory'] = false;
}
// If "force_alias" is ture set alwaysSave to true.
if ($this->get('force_alias')) {
$arrFieldDef['eval']['alwaysSave'] = true;
}
return $arrFieldDef;
}
/**
* {@inheritdoc}
*/
public function modelSaved($objItem)
{
// Alias already defined and no update forced, get out!
if ($objItem->get($this->getColName()) && (!$this->get('force_alias'))) {
return;
}
// Item is a variant but no overriding allowed, get out!
if ($objItem->isVariant() && (!$this->get('isvariant'))) {
return;
}
$arrAlias = '';
foreach (deserialize($this->get('alias_fields')) as $strAttribute) {
$arrValues = $objItem->parseAttribute($strAttribute['field_attribute'], 'text', null);
$arrAlias[] = $arrValues['text'];
}
$dispatcher = $this->getMetaModel()->getServiceContainer()->getEventDispatcher();
$replaceEvent = new ReplaceInsertTagsEvent(implode('-', $arrAlias));
$dispatcher->dispatch(ContaoEvents::CONTROLLER_REPLACE_INSERT_TAGS, $replaceEvent);
// Implode with '-', replace inserttags and strip HTML elements.
$strAlias = standardize(strip_tags($replaceEvent->getBuffer()));
// We need to fetch the attribute values for all attributes in the alias_fields and update the database and the
// model accordingly.
if ($this->get('isunique')) {
// Ensure uniqueness.
$strBaseAlias = $strAlias;
$arrIds = array($objItem->get('id'));
$intCount = 2;
while (array_diff($this->searchFor($strAlias), $arrIds)) {
$strAlias = $strBaseAlias . '-' . ($intCount++);
}
}
$this->setDataFor(array($objItem->get('id') => $strAlias));
$objItem->set($this->getColName(), $strAlias);
}
}