1+ <?php
2+
3+ namespace LiveHelperChat \Helpers \Bot ;
4+
5+ class IndividualizationValidator
6+ {
7+ /**
8+ * Validates individualization (translation) item from submitted form data
9+ *
10+ * @param \erLhcoreClassModelGenericBotTrItem $trItem
11+ *
12+ * @return array Errors
13+ */
14+ public static function validate (& $ trItem )
15+ {
16+ $ definition = array (
17+ 'identifier ' => new \ezcInputFormDefinitionElement (\ezcInputFormDefinitionElement::OPTIONAL , 'unsafe_raw ' ),
18+ 'translation ' => new \ezcInputFormDefinitionElement (\ezcInputFormDefinitionElement::OPTIONAL , 'unsafe_raw ' ,null ,FILTER_REQUIRE_ARRAY ),
19+ 'default_message ' => new \ezcInputFormDefinitionElement (\ezcInputFormDefinitionElement::OPTIONAL , 'unsafe_raw ' ),
20+ 'message_item ' => new \ezcInputFormDefinitionElement (\ezcInputFormDefinitionElement::OPTIONAL , 'unsafe_raw ' ,null ,FILTER_REQUIRE_ARRAY ),
21+ 'languages ' => new \ezcInputFormDefinitionElement (\ezcInputFormDefinitionElement::OPTIONAL , 'unsafe_raw ' ,null ,FILTER_REQUIRE_ARRAY ),
22+ 'group_id ' => new \ezcInputFormDefinitionElement (\ezcInputFormDefinitionElement::OPTIONAL , 'int ' , array ('min_range ' => 1 )),
23+ 'auto_translate ' => new \ezcInputFormDefinitionElement (\ezcInputFormDefinitionElement::OPTIONAL , 'boolean ' )
24+ );
25+
26+ $ form = new \ezcInputForm ( INPUT_POST , $ definition );
27+ $ Errors = array ();
28+
29+ if ( !$ form ->hasValidData ( 'identifier ' ) || $ form ->identifier == '' ) {
30+ $ Errors [] = \erTranslationClassLhTranslation ::getInstance ()->getTranslation ('departament/edit ' ,'Please enter translation group name! ' );
31+ } else {
32+ $ trItem ->identifier = $ form ->identifier ;
33+ }
34+
35+ $ data = array ('default ' => '' , 'items ' => array ());
36+
37+ $ languagesData = array ();
38+ if ( $ form ->hasValidData ( 'languages ' ) && !empty ($ form ->languages ) )
39+ {
40+ foreach ($ form ->languages as $ index => $ languages ) {
41+ $ languagesData [] = array (
42+ 'languages ' => $ form ->languages [$ index ],
43+ 'message ' => $ form ->message_item [$ index ]
44+ );
45+ }
46+ }
47+
48+ $ data ['items ' ] = $ languagesData ;
49+
50+ if ( $ form ->hasValidData ( 'default_message ' ) ) {
51+ $ data ['default ' ] = $ form ->default_message ;
52+ }
53+
54+ $ trItem ->translation_array = $ data ;
55+ $ trItem ->translation = json_encode ($ data );
56+
57+ if ( $ form ->hasValidData ( 'group_id ' ) ) {
58+ $ trItem ->group_id = $ form ->group_id ;
59+ } else {
60+ $ Errors [] = \erTranslationClassLhTranslation ::getInstance ()->getTranslation ('departament/edit ' ,'Please choose a group! ' );
61+ }
62+
63+ if ( $ form ->hasValidData ( 'auto_translate ' ) ) {
64+ $ trItem ->auto_translate = 1 ;
65+ } else {
66+ $ trItem ->auto_translate = 0 ;
67+ }
68+
69+ return $ Errors ;
70+ }
71+
72+ /**
73+ * Copies individualization item to other individualization groups based on submitted form data
74+ *
75+ * @param \erLhcoreClassModelGenericBotTrItem $trItem
76+ */
77+ public static function copyToGroupsFromRequest (& $ trItem )
78+ {
79+ $ definition = array (
80+ 'copy_mode ' => new \ezcInputFormDefinitionElement (\ezcInputFormDefinitionElement::OPTIONAL , 'unsafe_raw ' ),
81+ 'copy_group_ids ' => new \ezcInputFormDefinitionElement (\ezcInputFormDefinitionElement::OPTIONAL , 'int ' , array ('min_range ' => 1 ), FILTER_REQUIRE_ARRAY )
82+ );
83+
84+ $ form = new \ezcInputForm ( INPUT_POST , $ definition );
85+
86+ if ( $ form ->hasValidData ( 'copy_mode ' ) && in_array ($ form ->copy_mode , array ('create ' ,'update ' ))
87+ && $ form ->hasValidData ( 'copy_group_ids ' ) && !empty ($ form ->copy_group_ids ) ) {
88+ self ::copyToGroups ($ trItem , $ form ->copy_group_ids , $ form ->copy_mode == 'update ' );
89+ }
90+ }
91+
92+ /**
93+ * Copies individualization item to other individualization groups
94+ *
95+ * @param \erLhcoreClassModelGenericBotTrItem $trItem Item which should be copied
96+ * @param array $groupIds Individualization groups ID's
97+ * @param bool $updateExisting true - update existing item, false - create only if it does not exist
98+ */
99+ public static function copyToGroups ($ trItem , $ groupIds , $ updateExisting = false )
100+ {
101+ foreach (array_unique (array_map ('intval ' , (array )$ groupIds )) as $ groupId ) {
102+
103+ if ($ groupId <= 0 || $ groupId == $ trItem ->group_id ) {
104+ continue ;
105+ }
106+
107+ $ trItemExisting = \erLhcoreClassModelGenericBotTrItem ::findOne (array (
108+ 'filter ' => array (
109+ 'group_id ' => $ groupId ,
110+ 'identifier ' => $ trItem ->identifier ,
111+ )
112+ ));
113+
114+ if ($ trItemExisting !== false ) {
115+ if ($ updateExisting == true ) {
116+ $ trItemExisting ->translation = $ trItem ->translation ;
117+ $ trItemExisting ->auto_translate = $ trItem ->auto_translate ;
118+ $ trItemExisting ->updateThis ();
119+ }
120+ continue ;
121+ }
122+
123+ $ trItemCopy = new \erLhcoreClassModelGenericBotTrItem ();
124+ $ trItemCopy ->group_id = $ groupId ;
125+ $ trItemCopy ->identifier = $ trItem ->identifier ;
126+ $ trItemCopy ->translation = $ trItem ->translation ;
127+ $ trItemCopy ->auto_translate = $ trItem ->auto_translate ;
128+ $ trItemCopy ->saveThis ();
129+ }
130+ }
131+ }
0 commit comments