summaryrefslogtreecommitdiff
path: root/add_extbase_property.sh
blob: 664a7424e110a24548edfd8ac2f05dd9c7ddfcbb (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash

# TODO: fail if the current directon is not under version control
# TODO: fail if there are uncommitted changes
# TODO: @required support
# TODO: add relation with optional @lazy support
# TODO: search backwards for ext_emconf.php and cd to that directory and use it as extension name

if [ ! -e "ext_emconf.php" ]; then
	echo "Error: This script needs to be run from an extensions root dir"
	exit 1
fi

if [ $# -lt 3 ]; then
	echo "Usage:"
	echo "  `basename $0` <ModelName> <newField> <type>"
	echo
	echo "  <ModelName> without namespace e.g. 'Event'"
	echo "  <newField>  the field to be added, camel case as written in the model"
	echo "  <type> is one of: "
	echo "    int"
	echo "    float"
	echo "    bool"
	echo "    string"
	echo "    text"
	echo "    rte"
	echo "    password"
	echo "    date"
	echo "    datetime"
	echo "    time"
	echo "    timesec"
	echo "    select"
	echo "    date_timestamp"
	echo "    datetime_timestamp"
	echo
	echo "Examples:"
	echo "    `basename $0` Event title string"
	echo "    `basename $0` Event start datetime"
	echo "    `basename $0` Event end datetime"
	echo "    `basename $0` Event description rte"
	echo "    `basename $0` Event isTop bool"
	echo "    `basename $0` Product price float"

	exit 1
fi

model=$1
property=$2
typ=$3

extension=$(basename `pwd`)
extension_normalize=`echo "$extension" | sed 's/_//g'`
model_normalize=`echo "$model" | tr '[:upper:]' '[:lower:]'`
tablename="tx_${extension_normalize}_domain_model_${model_normalize}"

model_file=Classes/Domain/Model/${model}.php
tca_file=Configuration/TCA/${tablename}.php

[[ -e "$tca_file" ]] || tca_file=Configuration/TCA/${model}.php

field=`echo $property | sed -r 's/([a-z]+)([A-Z][a-z]+)/\1_\l\2/g'`
uproperty=`echo $property | sed -r 's/^./\u&/'`


declare -A type_map=(
	["int"]="int"
	["string"]="string"
	["text"]="string"
	["rte"]="string"

	["password"]="string"
	["float"]="float"
	["bool"]="bool"
	["date"]="\\DateTime"
	["datetime"]="\\DateTime"
	["time"]="int"
	["timesec"]="int"
	["select"]="int"
	["date_timestamp"]="\\DateTime"
	["datetime_timestamp"]="\\DateTime"
)

declare -A default_values=(
	["int"]="0"
	["string"]="''"
	["text"]="''"
	["rte"]="''"

	["password"]="''"
	["float"]="0.0"
	["bool"]="false"
	["date"]="null"
	["datetime"]="null"
	["time"]="0"
	["timesec"]="0"
	["select"]="0"
	["date_timestamp"]="null"
	["datetime_timestamp"]="null"
)

declare -A tca_types=(
	["int"]="input"
	["string"]="input"
	["text"]="text"
	["rte"]="text"

	["password"]="input"
	["float"]="input"
	["bool"]="check"
	["date"]="input"
	["datetime"]="input"
	["time"]="input"
	["timesec"]="input"
	["select"]="select"
	["date_timestamp"]="input"
	["datetime_timestamp"]="input"
)

declare -A tca_evals=(
	["int"]="trim,int"
	["string"]="trim"
	["text"]="trim"
	["rte"]="trim"

	["password"]="nospace,password"
	["float"]="double2"
	["bool"]=""
	["date"]="date"
	["datetime"]="datetime"
	["time"]="time"
	["timesec"]="timesec"
	["select"]=""
	["date_timestamp"]="date"
	["datetime_timestamp"]="datetime"
)

declare -A tca_option_map=(
	["int"]="'size' => 30"
	["string"]="'size' => 30"
	["text"]="'cols' => 40, 'rows' => 15"
	["rte"]="'cols' => 40, 'rows' => 15"

	["password"]="'size' => 30"
	["float"]="'size' => 30"
	["bool"]="'default' => 0"
	["date"]="'dbType' => 'date', 'size' => 7, 'checkbox' => 0, 'default' => '0000-00-00'"
	["datetime"]="'dbType' => 'date', 'size' => 12, 'checkbox' => 0, 'default' => '0000-00-00 00:00:00'"
	["time"]="'size' => 4, 'checkbox' => 1, 'default' => time()"
	["timesec"]="'size' => 6, 'checkbox' => 1, 'default' => time()"
	["select"]="'renderType' => 'selectSingle', 'size' => 1, 'maxitems' => 1, 'items' => [['-- Label --', 0]]"
	["date_timestamp"]="'size' => 7, 'checkbox' => 1, 'default' => time()"
	["datetime_timestamp"]="'size' => 12, 'checkbox' => 1, 'default' => time()"
)

declare -A sql_types=(
	["int"]="int(11) unsigned DEFAULT '0' NOT NULL"
	["string"]="varchar(255) DEFAULT '' NOT NULL"
	["text"]="text NOT NULL"
	["rte"]="text NOT NULL"

	["password"]="varchar(255) DEFAULT '' NOT NULL"
	["float"]="double(11,2) DEFAULT '0.00' NOT NULL"
	["bool"]="tinyint(1) unsigned DEFAULT '0' NOT NULL"
	["date"]="date DEFAULT '0000-00-00'"
	["datetime"]="datetime DEFAULT '0000-00-00 00:00:00'"
	["time"]="int(11) DEFAULT '0' NOT NULL"
	["time_sec"]="int(11) DEFAULT '0' NOT NULL"
	["select_list"]="int(11) DEFAULT '0' NOT NULL"
	["date_timestamp"]="int(11) DEFAULT '0' NOT NULL"
	["datetime_timestamp"]="int(11) DEFAULT '0' NOT NULL"
)

php_type="${type_map["$typ"]}"
default_value="${default_values["$typ"]}"
tca_type="${tca_types["$typ"]}"
tca_options="${tca_option_map["$typ"]}"
sql_type="${sql_types["$typ"]}"

#######################################################################################################################

# TCA

# The first sed command is a fix for extension_builder's trailing comma in searchFields
sed -i \
	-e "s/\('searchFields' => .*\),',/\1',/" \
	-e "s/\('searchFields.*\)',\$/\1,$field',/" \
	-e "s/\('searchFields' => '\),\(.*\)/\1\2/" \
	-e "s/\('showRecordFieldList.*\)',\$/\1, $field',/" \
	-e "s/'label' => 'uid'/'label' => '${field}'/" \
	$tca_file

# Place before the access tab, if that is available
if grep --quiet -- '--div--;LLL:EXT:cms/locallang_ttc.xlf:tabs.access' $tca_file; then
	sed -i "s/\('showitem' => '..*\)--div/\1$field, --div/" $tca_file
else
	sed -i "s/\('showitem' => '..*\)'),/\1, $field'),/" $tca_file
fi

sed -i "s#'columns'.*#&\n\n\
        '${field}' => array(\n\
            'exclude' => 1,\n\
            'label' => 'LLL:EXT:${extension}/Resources/Private/Language/locallang_db.xlf:${tablename}.${field}',\n\
            'config' => array(\n\
                'type' => '${tca_type}',\n\
                $tca_options,\n\
                'eval' => 'trim'\n\
            ),\n\
        ),#" \
	$tca_file

# Locallang fixes
sed -i "s/.*<\/body>/\t\t\t<trans-unit id=\"${tablename}.${field}\">\n\t\t\t\t<source>${uproperty}<\/source>\n\t\t\t<\/trans-unit>\n&/" \
	Resources/Private/Language/locallang.xlf \
	Resources/Private/Language/locallang_db.xlf

sed -i "s/.*<\/body>/\t\t\t<trans-unit id=\"${field}.description\">\n\t\t\t\t<source>${field}<\/source>\n\t\t\t<\/trans-unit>\n&/" \
	Resources/Private/Language/locallang_csh_${tablename}.xlf


#ext_tables.sql
sed -i "s/CREATE TABLE ${tablename} (/&\n\n\t${field} ${sql_type},/" ext_tables.sql

#if grep --quient function 

sed -i "\$s#^#\n\
    /**\n\
     * ${property}\n\
     *\n\
     * @var ${php_type}\n\
     */\n\
    protected \$${property} = ${default_value};\n\
\n\
    /**\n\
     * Returns the ${property}\n\
     *\n\
     * @return ${php_type} \$${property}\n\
     */\n\
    public function get${uproperty}()\n\
    {\n\
        return \$this->${property};\n\
    }\n\
\n\
    /**\n\
     * Sets the ${property}\n\
     *\n\
     * @param  ${php_type} \$${property}\n\
     * @return void\n\
     */\n\
    public function set${uproperty}(\$${property})\n\
    {\n\
        \$this->${property} = \$${property};\n\
    }\n#" \
	$model_file

echo "Created \$${property} in ${model}"
echo
echo "Edit Resources/Private/Language/locallang_db.xlf to edit the label shown in the TCA."
echo "Edit Resources/Private/Language/locallang.xlf to edit the label shown in the Frontend."
echo
echo "You should edit ext_tables.sql, $tca_file and $model_file to move some definitions to the proper place."