summaryrefslogtreecommitdiff
path: root/add_extbase_property.sh
blob: b431fef55d8d1831d17e30bac6e1c9e419616ca8 (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
#!/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

model=$1
property=$2

# Working for int-only currently
# Need to create a lookup table for type-related definitions in ext_tables.sql/TCA (and Model)
#typ=int
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

# TODO: search backwards for ext_emconf.php and cd to that directory and use it as extension name
# TODO: fail if ext_emconf.php is not found (probably the wrong directory)
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"
)

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

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

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

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

php_type="${type_map["$typ"]}"
tca_type="${tca_types["$typ"]}"
tca_options="${tca_option_map["$typ"]}"
sql_type="${sql_types["$typ"]}"
default_value="${default_values["$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/\('showRecordFieldList.*\)',\$/\1, $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."