| Conditions | 134 |
| Paths | 3 |
| Total Lines | 1154 |
| Code Lines | 622 |
| Lines | 122 |
| Ratio | 10.57 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 845 | public function set_extra_fields_in_form( |
||
| 846 | $form, |
||
| 847 | $extraData, |
||
| 848 | $adminPermissions = false, |
||
| 849 | $extra = array(), |
||
| 850 | $itemId = null, |
||
| 851 | $exclude = [], |
||
| 852 | $useTagAsSelect = false, |
||
| 853 | $showOnlyTheseFields = [], |
||
| 854 | $orderFields = [] |
||
| 855 | ) { |
||
| 856 | $type = $this->type; |
||
| 857 | $jquery_ready_content = null; |
||
| 858 | if (!empty($extra)) { |
||
| 859 | $newOrder = []; |
||
| 860 | if (!empty($orderFields)) { |
||
| 861 | foreach ($orderFields as $order) { |
||
| 862 | foreach ($extra as $field_details) { |
||
| 863 | if ($order == $field_details['variable']) { |
||
| 864 | $newOrder[] = $field_details; |
||
| 865 | } |
||
| 866 | } |
||
| 867 | } |
||
| 868 | $extra = $newOrder; |
||
| 869 | } |
||
| 870 | |||
| 871 | foreach ($extra as $field_details) { |
||
| 872 | if (!empty($showOnlyTheseFields)) { |
||
| 873 | if (!in_array($field_details['variable'], $showOnlyTheseFields)) { |
||
| 874 | continue; |
||
| 875 | } |
||
| 876 | } |
||
| 877 | |||
| 878 | // Getting default value id if is set |
||
| 879 | $defaultValueId = null; |
||
| 880 | if (isset($field_details['options']) && !empty($field_details['options'])) { |
||
| 881 | $valueToFind = null; |
||
| 882 | if (isset($field_details['field_default_value'])) { |
||
| 883 | $valueToFind = $field_details['field_default_value']; |
||
| 884 | } |
||
| 885 | // If a value is found we override the default value |
||
| 886 | if (isset($extraData['extra_'.$field_details['variable']])) { |
||
| 887 | $valueToFind = $extraData['extra_'.$field_details['variable']]; |
||
| 888 | } |
||
| 889 | |||
| 890 | foreach ($field_details['options'] as $option) { |
||
| 891 | if ($option['option_value'] == $valueToFind) { |
||
| 892 | $defaultValueId = $option['id']; |
||
| 893 | } |
||
| 894 | } |
||
| 895 | } |
||
| 896 | |||
| 897 | if (!$adminPermissions) { |
||
| 898 | if ($field_details['visible_to_self'] == 0) { |
||
| 899 | continue; |
||
| 900 | } |
||
| 901 | |||
| 902 | if (in_array($field_details['variable'], $exclude)) { |
||
| 903 | continue; |
||
| 904 | } |
||
| 905 | } |
||
| 906 | |||
| 907 | $freezeElement = false; |
||
| 908 | if (!$adminPermissions) { |
||
| 909 | $freezeElement = $field_details['visible_to_self'] == 0 || $field_details['changeable'] == 0; |
||
| 910 | } |
||
| 911 | |||
| 912 | switch ($field_details['field_type']) { |
||
| 913 | case self::FIELD_TYPE_TEXT: |
||
| 914 | $form->addElement( |
||
| 915 | 'text', |
||
| 916 | 'extra_'.$field_details['variable'], |
||
| 917 | $field_details['display_text'], |
||
| 918 | array( |
||
| 919 | 'id' => 'extra_'.$field_details['variable'] |
||
| 920 | ) |
||
| 921 | ); |
||
| 922 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 923 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
| 924 | if ($freezeElement) { |
||
| 925 | $form->freeze('extra_'.$field_details['variable']); |
||
| 926 | } |
||
| 927 | break; |
||
| 928 | case self::FIELD_TYPE_TEXTAREA: |
||
| 929 | $form->addHtmlEditor( |
||
| 930 | 'extra_'.$field_details['variable'], |
||
| 931 | $field_details['display_text'], |
||
| 932 | false, |
||
| 933 | false, |
||
| 934 | array( |
||
| 935 | 'ToolbarSet' => 'Profile', |
||
| 936 | 'Width' => '100%', |
||
| 937 | 'Height' => '130', |
||
| 938 | 'id' => 'extra_'.$field_details['variable'] |
||
| 939 | ) |
||
| 940 | ); |
||
| 941 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 942 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
| 943 | if ($freezeElement) { |
||
| 944 | $form->freeze('extra_'.$field_details['variable']); |
||
| 945 | } |
||
| 946 | break; |
||
| 947 | case self::FIELD_TYPE_RADIO: |
||
| 948 | $group = array(); |
||
| 949 | if (isset($field_details['options']) && !empty($field_details['options'])) { |
||
| 950 | foreach ($field_details['options'] as $option_details) { |
||
| 951 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
| 952 | $group[] = $form->createElement( |
||
| 953 | 'radio', |
||
| 954 | 'extra_'.$field_details['variable'], |
||
| 955 | $option_details['option_value'], |
||
| 956 | $option_details['display_text'].'<br />', |
||
| 957 | $option_details['option_value'] |
||
| 958 | ); |
||
| 959 | } |
||
| 960 | } |
||
| 961 | $form->addGroup( |
||
| 962 | $group, |
||
| 963 | 'extra_'.$field_details['variable'], |
||
| 964 | $field_details['display_text'] |
||
| 965 | ); |
||
| 966 | if ($freezeElement) { |
||
| 967 | $form->freeze('extra_'.$field_details['variable']); |
||
| 968 | } |
||
| 969 | break; |
||
| 970 | case self::FIELD_TYPE_CHECKBOX: |
||
| 971 | $group = array(); |
||
| 972 | if (isset($field_details['options']) && !empty($field_details['options'])) { |
||
| 973 | foreach ($field_details['options'] as $option_details) { |
||
| 974 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
| 975 | $group[] = $form->createElement( |
||
| 976 | 'checkbox', |
||
| 977 | 'extra_'.$field_details['variable'], |
||
| 978 | $option_details['option_value'], |
||
| 979 | $option_details['display_text'].'<br />', |
||
| 980 | $option_details['option_value'] |
||
| 981 | ); |
||
| 982 | } |
||
| 983 | } else { |
||
| 984 | $fieldVariable = "extra_{$field_details['variable']}"; |
||
| 985 | $checkboxAttributes = array(); |
||
| 986 | if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) { |
||
| 987 | if (!empty($extraData[$fieldVariable])) { |
||
| 988 | $checkboxAttributes['checked'] = 1; |
||
| 989 | } |
||
| 990 | } |
||
| 991 | |||
| 992 | // We assume that is a switch on/off with 1 and 0 as values |
||
| 993 | $group[] = $form->createElement( |
||
| 994 | 'checkbox', |
||
| 995 | 'extra_'.$field_details['variable'], |
||
| 996 | null, |
||
| 997 | //$field_details['display_text'].'<br />', |
||
| 998 | get_lang('Yes'), |
||
| 999 | $checkboxAttributes |
||
| 1000 | ); |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | $form->addGroup( |
||
| 1004 | $group, |
||
| 1005 | 'extra_'.$field_details['variable'], |
||
| 1006 | $field_details['display_text'] |
||
| 1007 | ); |
||
| 1008 | if ($freezeElement) { |
||
| 1009 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1010 | } |
||
| 1011 | break; |
||
| 1012 | case self::FIELD_TYPE_SELECT: |
||
| 1013 | $get_lang_variables = false; |
||
| 1014 | if (in_array( |
||
| 1015 | $field_details['variable'], |
||
| 1016 | array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message') |
||
| 1017 | ) |
||
| 1018 | ) { |
||
| 1019 | $get_lang_variables = true; |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | // Get extra field workflow |
||
| 1023 | $userInfo = api_get_user_info(); |
||
| 1024 | $addOptions = array(); |
||
| 1025 | $optionsExists = false; |
||
| 1026 | global $app; |
||
| 1027 | // Check if exist $app['orm.em'] object |
||
| 1028 | if (isset($app['orm.em']) && is_object($app['orm.em'])) { |
||
| 1029 | $optionsExists = $app['orm.em'] |
||
| 1030 | ->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption') |
||
| 1031 | ->findOneBy(array('fieldId' => $field_details['id'])); |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | if ($optionsExists) { |
||
| 1035 | if (isset($userInfo['status']) && !empty($userInfo['status'])) { |
||
| 1036 | $fieldWorkFlow = $app['orm.em'] |
||
| 1037 | ->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption') |
||
| 1038 | ->findBy( |
||
| 1039 | array( |
||
| 1040 | 'fieldId' => $field_details['id'], |
||
| 1041 | 'relatedFieldOptionId' => $defaultValueId, |
||
| 1042 | 'roleId' => $userInfo['status'] |
||
| 1043 | ) |
||
| 1044 | ); |
||
| 1045 | foreach ($fieldWorkFlow as $item) { |
||
| 1046 | $addOptions[] = $item->getFieldOptionId(); |
||
| 1047 | } |
||
| 1048 | } |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | $options = array(); |
||
| 1052 | if (empty($defaultValueId)) { |
||
| 1053 | $options[''] = get_lang('SelectAnOption'); |
||
| 1054 | } |
||
| 1055 | |||
| 1056 | $optionList = array(); |
||
| 1057 | if (!empty($field_details['options'])) { |
||
| 1058 | foreach ($field_details['options'] as $option_details) { |
||
| 1059 | $optionList[$option_details['id']] = $option_details; |
||
| 1060 | if ($get_lang_variables) { |
||
| 1061 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
| 1062 | } else { |
||
| 1063 | if ($optionsExists) { |
||
| 1064 | // Adding always the default value |
||
| 1065 | View Code Duplication | if ($option_details['id'] == $defaultValueId) { |
|
| 1066 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
| 1067 | } else { |
||
| 1068 | if (isset($addOptions) && !empty($addOptions)) { |
||
| 1069 | // Parsing filters |
||
| 1070 | if (in_array($option_details['id'], $addOptions)) { |
||
| 1071 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
| 1072 | } |
||
| 1073 | } |
||
| 1074 | } |
||
| 1075 | } else { |
||
| 1076 | // Normal behaviour |
||
| 1077 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
| 1078 | } |
||
| 1079 | } |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | if (isset($optionList[$defaultValueId])) { |
||
| 1083 | if (isset($optionList[$defaultValueId]['option_value']) && |
||
| 1084 | $optionList[$defaultValueId]['option_value'] == 'aprobada' |
||
| 1085 | ) { |
||
| 1086 | // @todo function don't exists api_is_question_manager |
||
| 1087 | /*if (api_is_question_manager() == false) { |
||
| 1088 | $form->freeze(); |
||
| 1089 | }*/ |
||
| 1090 | } |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | // Setting priority message |
||
| 1094 | if (isset($optionList[$defaultValueId]) && |
||
| 1095 | isset($optionList[$defaultValueId]['priority']) |
||
| 1096 | ) { |
||
| 1097 | if (!empty($optionList[$defaultValueId]['priority'])) { |
||
| 1098 | $priorityId = $optionList[$defaultValueId]['priority']; |
||
| 1099 | $option = new ExtraFieldOption($this->type); |
||
| 1100 | $messageType = $option->getPriorityMessageType($priorityId); |
||
| 1101 | $form->addElement( |
||
| 1102 | 'label', |
||
| 1103 | null, |
||
| 1104 | Display::return_message( |
||
| 1105 | $optionList[$defaultValueId]['priority_message'], |
||
| 1106 | $messageType |
||
| 1107 | ) |
||
| 1108 | ); |
||
| 1109 | } |
||
| 1110 | } |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | // chzn-select doesn't work for sessions?? |
||
| 1114 | $form->addElement( |
||
| 1115 | 'select', |
||
| 1116 | 'extra_'.$field_details['variable'], |
||
| 1117 | $field_details['display_text'], |
||
| 1118 | $options, |
||
| 1119 | array('id' => 'extra_'.$field_details['variable']) |
||
| 1120 | ); |
||
| 1121 | |||
| 1122 | /* Enable this when field_loggeable is introduced as a table field (2.0) |
||
| 1123 | if ($optionsExists && $field_details['field_loggeable'] && !empty($defaultValueId)) { |
||
| 1124 | |||
| 1125 | $form->addElement( |
||
| 1126 | 'textarea', |
||
| 1127 | 'extra_' . $field_details['variable'] . '_comment', |
||
| 1128 | $field_details['display_text'] . ' ' . get_lang('Comment') |
||
| 1129 | ); |
||
| 1130 | |||
| 1131 | $extraFieldValue = new ExtraFieldValue($this->type); |
||
| 1132 | $repo = $app['orm.em']->getRepository($extraFieldValue->entityName); |
||
| 1133 | $repoLog = $app['orm.em']->getRepository('Gedmo\Loggable\Entity\LogEntry'); |
||
| 1134 | $newEntity = $repo->findOneBy( |
||
| 1135 | array( |
||
| 1136 | $this->handlerEntityId => $itemId, |
||
| 1137 | 'fieldId' => $field_details['id'] |
||
| 1138 | ) |
||
| 1139 | ); |
||
| 1140 | // @todo move this in a function inside the class |
||
| 1141 | if ($newEntity) { |
||
| 1142 | $logs = $repoLog->getLogEntries($newEntity); |
||
| 1143 | if (!empty($logs)) { |
||
| 1144 | $html = '<b>' . get_lang('LatestChanges') . '</b><br /><br />'; |
||
| 1145 | |||
| 1146 | $table = new HTML_Table(array('class' => 'data_table')); |
||
| 1147 | $table->setHeaderContents(0, 0, get_lang('Value')); |
||
| 1148 | $table->setHeaderContents(0, 1, get_lang('Comment')); |
||
| 1149 | $table->setHeaderContents(0, 2, get_lang('ModifyDate')); |
||
| 1150 | $table->setHeaderContents(0, 3, get_lang('Username')); |
||
| 1151 | $row = 1; |
||
| 1152 | foreach ($logs as $log) { |
||
| 1153 | $column = 0; |
||
| 1154 | $data = $log->getData(); |
||
| 1155 | $fieldValue = isset($data['fieldValue']) ? $data['fieldValue'] : null; |
||
| 1156 | $comment = isset($data['comment']) ? $data['comment'] : null; |
||
| 1157 | |||
| 1158 | $table->setCellContents($row, $column, $fieldValue); |
||
| 1159 | $column++; |
||
| 1160 | $table->setCellContents($row, $column, $comment); |
||
| 1161 | $column++; |
||
| 1162 | $table->setCellContents($row, $column, api_get_local_time($log->getLoggedAt()->format('Y-m-d H:i:s'))); |
||
| 1163 | $column++; |
||
| 1164 | $table->setCellContents($row, $column, $log->getUsername()); |
||
| 1165 | $row++; |
||
| 1166 | } |
||
| 1167 | $form->addElement('label', null, $html.$table->toHtml()); |
||
| 1168 | } |
||
| 1169 | } |
||
| 1170 | } |
||
| 1171 | */ |
||
| 1172 | |||
| 1173 | if ($freezeElement) { |
||
| 1174 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1175 | } |
||
| 1176 | break; |
||
| 1177 | case self::FIELD_TYPE_SELECT_MULTIPLE: |
||
| 1178 | $options = array(); |
||
| 1179 | foreach ($field_details['options'] as $option_id => $option_details) { |
||
| 1180 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
| 1181 | } |
||
| 1182 | $form->addElement( |
||
| 1183 | 'select', |
||
| 1184 | 'extra_'.$field_details['variable'], |
||
| 1185 | $field_details['display_text'], |
||
| 1186 | $options, |
||
| 1187 | array('multiple' => 'multiple', 'id' => 'extra_'.$field_details['variable']) |
||
| 1188 | ); |
||
| 1189 | if ($freezeElement) { |
||
| 1190 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1191 | } |
||
| 1192 | break; |
||
| 1193 | case self::FIELD_TYPE_DATE: |
||
| 1194 | $form->addDatePicker('extra_'.$field_details['variable'], $field_details['display_text']); |
||
| 1195 | if ($freezeElement) { |
||
| 1196 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1197 | } |
||
| 1198 | break; |
||
| 1199 | case self::FIELD_TYPE_DATETIME: |
||
| 1200 | $form->addDateTimePicker( |
||
| 1201 | 'extra_'.$field_details['variable'], |
||
| 1202 | $field_details['display_text'] |
||
| 1203 | ); |
||
| 1204 | |||
| 1205 | $defaults['extra_'.$field_details['variable']] = api_get_local_time(); |
||
| 1206 | if (!isset($form->_defaultValues['extra_'.$field_details['variable']])) { |
||
| 1207 | $form->setDefaults($defaults); |
||
| 1208 | } |
||
| 1209 | if ($freezeElement) { |
||
| 1210 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1211 | } |
||
| 1212 | break; |
||
| 1213 | case self::FIELD_TYPE_DOUBLE_SELECT: |
||
| 1214 | $first_select_id = 'first_extra_'.$field_details['variable']; |
||
| 1215 | $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1'; |
||
| 1216 | |||
| 1217 | $jquery_ready_content .= ' |
||
| 1218 | $("#'.$first_select_id.'").on("change", function() { |
||
| 1219 | var id = $(this).val(); |
||
| 1220 | if (id) { |
||
| 1221 | $.ajax({ |
||
| 1222 | url: "'.$url.'&a=get_second_select_options", |
||
| 1223 | dataType: "json", |
||
| 1224 | data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id, |
||
| 1225 | success: function(data) { |
||
| 1226 | $("#second_extra_'.$field_details['variable'].'").empty(); |
||
| 1227 | $.each(data, function(index, value) { |
||
| 1228 | $("#second_extra_'.$field_details['variable'].'").append($("<option/>", { |
||
| 1229 | value: index, |
||
| 1230 | text: value |
||
| 1231 | })); |
||
| 1232 | }); |
||
| 1233 | $("#second_extra_'.$field_details['variable'].'").selectpicker("refresh"); |
||
| 1234 | }, |
||
| 1235 | }); |
||
| 1236 | } else { |
||
| 1237 | $("#second_extra_'.$field_details['variable'].'").empty(); |
||
| 1238 | } |
||
| 1239 | });'; |
||
| 1240 | |||
| 1241 | $first_id = null; |
||
| 1242 | if (!empty($extraData)) { |
||
| 1243 | if (isset($extraData['extra_'.$field_details['variable']])) { |
||
| 1244 | $first_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable']]; |
||
| 1245 | } |
||
| 1246 | } |
||
| 1247 | |||
| 1248 | $options = self::extra_field_double_select_convert_array_to_ordered_array( |
||
| 1249 | $field_details['options'] |
||
| 1250 | ); |
||
| 1251 | $values = array('' => get_lang('Select')); |
||
| 1252 | |||
| 1253 | $second_values = array(); |
||
| 1254 | if (!empty($options)) { |
||
| 1255 | foreach ($options as $option) { |
||
| 1256 | foreach ($option as $sub_option) { |
||
| 1257 | if ($sub_option['option_value'] == '0') { |
||
| 1258 | $values[$sub_option['id']] = $sub_option['display_text']; |
||
| 1259 | } else { |
||
| 1260 | if ($first_id === $sub_option['option_value']) { |
||
| 1261 | $second_values[$sub_option['id']] = $sub_option['display_text']; |
||
| 1262 | } |
||
| 1263 | } |
||
| 1264 | } |
||
| 1265 | } |
||
| 1266 | } |
||
| 1267 | $group = array(); |
||
| 1268 | $group[] = $form->createElement( |
||
| 1269 | 'select', |
||
| 1270 | 'extra_'.$field_details['variable'], |
||
| 1271 | null, |
||
| 1272 | $values, |
||
| 1273 | array('id' => $first_select_id) |
||
| 1274 | ); |
||
| 1275 | $group[] = $form->createElement( |
||
| 1276 | 'select', |
||
| 1277 | 'extra_'.$field_details['variable'].'_second', |
||
| 1278 | null, |
||
| 1279 | $second_values, |
||
| 1280 | array('id' => 'second_extra_'.$field_details['variable']) |
||
| 1281 | ); |
||
| 1282 | $form->addGroup( |
||
| 1283 | $group, |
||
| 1284 | 'extra_'.$field_details['variable'], |
||
| 1285 | $field_details['display_text'] |
||
| 1286 | ); |
||
| 1287 | |||
| 1288 | if ($freezeElement) { |
||
| 1289 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1290 | } |
||
| 1291 | break; |
||
| 1292 | case self::FIELD_TYPE_DIVIDER: |
||
| 1293 | $form->addHtml(' |
||
| 1294 | <div class="form-group "> |
||
| 1295 | <div class="col-sm-12"> |
||
| 1296 | <div class="panel-separator"> |
||
| 1297 | <h4 id="' . $field_details['variable'].'" class="form-separator">'.$field_details['display_text'].'</h4> |
||
| 1298 | </div> |
||
| 1299 | </div> |
||
| 1300 | </div> |
||
| 1301 | '); |
||
| 1302 | break; |
||
| 1303 | case self::FIELD_TYPE_TAG: |
||
| 1304 | $variable = $field_details['variable']; |
||
| 1305 | $field_id = $field_details['id']; |
||
| 1306 | |||
| 1307 | $tagsSelect = $form->addSelect( |
||
| 1308 | "extra_{$field_details['variable']}", |
||
| 1309 | $field_details['display_text'], |
||
| 1310 | [], |
||
| 1311 | ['style' => 'width: 100%;'] |
||
| 1312 | ); |
||
| 1313 | |||
| 1314 | if ($useTagAsSelect == false) { |
||
| 1315 | $tagsSelect->setAttribute('class', null); |
||
| 1316 | } |
||
| 1317 | |||
| 1318 | $tagsSelect->setAttribute('id', "extra_{$field_details['variable']}"); |
||
| 1319 | $tagsSelect->setMultiple(true); |
||
| 1320 | |||
| 1321 | $selectedOptions = []; |
||
| 1322 | if ($this->type === 'user') { |
||
| 1323 | // The magic should be here |
||
| 1324 | $user_tags = UserManager::get_user_tags($itemId, $field_details['id']); |
||
| 1325 | |||
| 1326 | if (is_array($user_tags) && count($user_tags) > 0) { |
||
| 1327 | foreach ($user_tags as $tag) { |
||
| 1328 | $tagsSelect->addOption( |
||
| 1329 | $tag['tag'], |
||
| 1330 | $tag['tag'] |
||
| 1331 | ); |
||
| 1332 | $selectedOptions[] = $tag['tag']; |
||
| 1333 | } |
||
| 1334 | } |
||
| 1335 | $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php'; |
||
| 1336 | } else { |
||
| 1337 | $em = Database::getManager(); |
||
| 1338 | |||
| 1339 | $fieldTags = $em |
||
| 1340 | ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag') |
||
| 1341 | ->findBy([ |
||
| 1342 | 'fieldId' => $field_id, |
||
| 1343 | 'itemId' => $itemId |
||
| 1344 | ]); |
||
| 1345 | /** @var \Chamilo\CoreBundle\Entity\ExtraFieldRelTag $fieldTag */ |
||
| 1346 | foreach ($fieldTags as $fieldTag) { |
||
| 1347 | /** @var \Chamilo\CoreBundle\Entity\Tag $tag */ |
||
| 1348 | $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId()); |
||
| 1349 | |||
| 1350 | if (empty($tag)) { |
||
| 1351 | continue; |
||
| 1352 | } |
||
| 1353 | $tagsSelect->addOption( |
||
| 1354 | $tag->getTag(), |
||
| 1355 | $tag->getTag() |
||
| 1356 | ); |
||
| 1357 | $selectedOptions[] = $tag->getTag(); |
||
| 1358 | } |
||
| 1359 | |||
| 1360 | if ($useTagAsSelect) { |
||
| 1361 | $fieldTags = $em |
||
| 1362 | ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag') |
||
| 1363 | ->findBy([ |
||
| 1364 | 'fieldId' => $field_id |
||
| 1365 | ]); |
||
| 1366 | $tagsAdded = []; |
||
| 1367 | foreach ($fieldTags as $fieldTag) { |
||
| 1368 | $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId()); |
||
| 1369 | |||
| 1370 | if (empty($tag)) { |
||
| 1371 | continue; |
||
| 1372 | } |
||
| 1373 | |||
| 1374 | $tagText = $tag->getTag(); |
||
| 1375 | |||
| 1376 | if (in_array($tagText, $tagsAdded)) { |
||
| 1377 | continue; |
||
| 1378 | } |
||
| 1379 | |||
| 1380 | $tagsSelect->addOption( |
||
| 1381 | $tag->getTag(), |
||
| 1382 | $tag->getTag(), |
||
| 1383 | [] |
||
| 1384 | ); |
||
| 1385 | |||
| 1386 | $tagsAdded[] = $tagText; |
||
| 1387 | } |
||
| 1388 | |||
| 1389 | } |
||
| 1390 | |||
| 1391 | $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php'; |
||
| 1392 | } |
||
| 1393 | |||
| 1394 | $form->setDefaults([ |
||
| 1395 | 'extra_'.$field_details['variable'] => $selectedOptions |
||
| 1396 | ]); |
||
| 1397 | |||
| 1398 | if ($useTagAsSelect == false) { |
||
| 1399 | $jquery_ready_content .= " |
||
| 1400 | $('#extra_$variable').select2({ |
||
| 1401 | ajax: { |
||
| 1402 | url: '$url?a=search_tags&field_id=$field_id&type={$this->type}', |
||
| 1403 | processResults: function (data) { |
||
| 1404 | return { |
||
| 1405 | results: data.items |
||
| 1406 | } |
||
| 1407 | } |
||
| 1408 | }, |
||
| 1409 | cache: false, |
||
| 1410 | tags: true, |
||
| 1411 | tokenSeparators: [','], |
||
| 1412 | placeholder: '".get_lang('StartToType')."' |
||
| 1413 | }); |
||
| 1414 | "; |
||
| 1415 | } |
||
| 1416 | break; |
||
| 1417 | View Code Duplication | case self::FIELD_TYPE_TIMEZONE: |
|
| 1418 | $form->addElement( |
||
| 1419 | 'select', |
||
| 1420 | 'extra_'.$field_details['variable'], |
||
| 1421 | $field_details['display_text'], |
||
| 1422 | api_get_timezones(), |
||
| 1423 | '' |
||
| 1424 | ); |
||
| 1425 | if ($freezeElement) { |
||
| 1426 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1427 | } |
||
| 1428 | break; |
||
| 1429 | case self::FIELD_TYPE_SOCIAL_PROFILE: |
||
| 1430 | // get the social network's favicon |
||
| 1431 | $extra_data_variable = isset($extraData['extra_'.$field_details['variable']]) ? $extraData['extra_'.$field_details['variable']] : null; |
||
| 1432 | $field_default_value = isset($field_details['field_default_value']) ? $field_details['field_default_value'] : null; |
||
| 1433 | $icon_path = UserManager::get_favicon_from_url( |
||
| 1434 | $extra_data_variable, |
||
| 1435 | $field_default_value |
||
| 1436 | ); |
||
| 1437 | // special hack for hi5 |
||
| 1438 | $leftpad = '1.7'; |
||
| 1439 | $top = '0.4'; |
||
| 1440 | $domain = parse_url($icon_path, PHP_URL_HOST); |
||
| 1441 | if ($domain == 'www.hi5.com' or $domain == 'hi5.com') { |
||
| 1442 | $leftpad = '3'; |
||
| 1443 | $top = '0'; |
||
| 1444 | } |
||
| 1445 | // print the input field |
||
| 1446 | $form->addElement( |
||
| 1447 | 'text', |
||
| 1448 | 'extra_'.$field_details['variable'], |
||
| 1449 | $field_details['display_text'], |
||
| 1450 | array( |
||
| 1451 | 'size' => 60, |
||
| 1452 | 'size' => implode( |
||
| 1453 | '; ', |
||
| 1454 | [ |
||
| 1455 | "background-image: url('$icon_path')", |
||
| 1456 | 'background-repeat: no-repeat', |
||
| 1457 | "background-position: 0.4em {$top}em", |
||
| 1458 | "padding-left: {$leftpad}em" |
||
| 1459 | ] |
||
| 1460 | ) |
||
| 1461 | ) |
||
| 1462 | ); |
||
| 1463 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 1464 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
| 1465 | if ($freezeElement) { |
||
| 1466 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1467 | } |
||
| 1468 | break; |
||
| 1469 | case self::FIELD_TYPE_MOBILE_PHONE_NUMBER: |
||
| 1470 | $form->addElement( |
||
| 1471 | 'text', |
||
| 1472 | 'extra_'.$field_details[1], |
||
| 1473 | $field_details[3]." (".get_lang('CountryDialCode').")", |
||
| 1474 | array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx') |
||
| 1475 | ); |
||
| 1476 | $form->applyFilter('extra_'.$field_details[1], 'stripslashes'); |
||
| 1477 | $form->applyFilter('extra_'.$field_details[1], 'trim'); |
||
| 1478 | $form->applyFilter('extra_'.$field_details[1], 'mobile_phone_number_filter'); |
||
| 1479 | $form->addRule( |
||
| 1480 | 'extra_'.$field_details[1], |
||
| 1481 | get_lang('MobilePhoneNumberWrong'), |
||
| 1482 | 'mobile_phone_number' |
||
| 1483 | ); |
||
| 1484 | if ($freezeElement) { |
||
| 1485 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1486 | } |
||
| 1487 | break; |
||
| 1488 | View Code Duplication | case self::FIELD_TYPE_INTEGER: |
|
| 1489 | $form->addElement( |
||
| 1490 | 'number', |
||
| 1491 | 'extra_'.$field_details['variable'], |
||
| 1492 | $field_details['display_text'], |
||
| 1493 | array('class' => 'span1', 'step' => 1) |
||
| 1494 | ); |
||
| 1495 | |||
| 1496 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 1497 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
| 1498 | $form->applyFilter('extra_'.$field_details['variable'], 'intval'); |
||
| 1499 | |||
| 1500 | if ($freezeElement) { |
||
| 1501 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1502 | } |
||
| 1503 | break; |
||
| 1504 | case self::FIELD_TYPE_FILE_IMAGE: |
||
| 1505 | $fieldVariable = "extra_{$field_details['variable']}"; |
||
| 1506 | $fieldTexts = [ |
||
| 1507 | $field_details['display_text'] |
||
| 1508 | ]; |
||
| 1509 | |||
| 1510 | View Code Duplication | if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) { |
|
| 1511 | if (file_exists(api_get_path(SYS_UPLOAD_PATH).$extraData[$fieldVariable])) { |
||
| 1512 | $fieldTexts[] = Display::img( |
||
| 1513 | api_get_path(WEB_UPLOAD_PATH).$extraData[$fieldVariable], |
||
| 1514 | $field_details['display_text'], |
||
| 1515 | ['width' => '300'] |
||
| 1516 | ); |
||
| 1517 | } |
||
| 1518 | } |
||
| 1519 | |||
| 1520 | if ($fieldTexts[0] === 'Image') { |
||
| 1521 | $fieldTexts[0] = get_lang($fieldTexts[0]); |
||
| 1522 | } |
||
| 1523 | |||
| 1524 | $form->addFile( |
||
| 1525 | $fieldVariable, |
||
| 1526 | $fieldTexts, |
||
| 1527 | ['accept' => 'image/*', 'id' => 'extra_image', 'crop_image' => 'true'] |
||
| 1528 | ); |
||
| 1529 | |||
| 1530 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 1531 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
| 1532 | |||
| 1533 | $allowed_picture_types = ['jpg', 'jpeg', 'png', 'gif']; |
||
| 1534 | $form->addRule( |
||
| 1535 | 'extra_'.$field_details['variable'], |
||
| 1536 | get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', |
||
| 1537 | 'filetype', |
||
| 1538 | $allowed_picture_types |
||
| 1539 | ); |
||
| 1540 | |||
| 1541 | if ($freezeElement) { |
||
| 1542 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1543 | } |
||
| 1544 | break; |
||
| 1545 | View Code Duplication | case self::FIELD_TYPE_FLOAT: |
|
| 1546 | $form->addElement( |
||
| 1547 | 'number', |
||
| 1548 | 'extra_'.$field_details['variable'], |
||
| 1549 | $field_details['display_text'], |
||
| 1550 | array('class' => 'span1', 'step' => '0.01') |
||
| 1551 | ); |
||
| 1552 | |||
| 1553 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 1554 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
| 1555 | $form->applyFilter('extra_'.$field_details['variable'], 'floatval'); |
||
| 1556 | |||
| 1557 | if ($freezeElement) { |
||
| 1558 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1559 | } |
||
| 1560 | break; |
||
| 1561 | case self::FIELD_TYPE_FILE: |
||
| 1562 | $fieldVariable = "extra_{$field_details['variable']}"; |
||
| 1563 | $fieldTexts = array( |
||
| 1564 | $field_details['display_text'] |
||
| 1565 | ); |
||
| 1566 | |||
| 1567 | if (is_array($extraData) && |
||
| 1568 | array_key_exists($fieldVariable, $extraData) |
||
| 1569 | ) { |
||
| 1570 | View Code Duplication | if (file_exists(api_get_path(SYS_UPLOAD_PATH).$extraData[$fieldVariable])) { |
|
| 1571 | $fieldTexts[] = Display::url( |
||
| 1572 | api_get_path(WEB_UPLOAD_PATH).$extraData[$fieldVariable], |
||
| 1573 | api_get_path(WEB_UPLOAD_PATH).$extraData[$fieldVariable], |
||
| 1574 | array( |
||
| 1575 | 'title' => $field_details['display_text'], |
||
| 1576 | 'target' => '_blank' |
||
| 1577 | ) |
||
| 1578 | ); |
||
| 1579 | } |
||
| 1580 | } |
||
| 1581 | |||
| 1582 | $form->addElement( |
||
| 1583 | 'file', |
||
| 1584 | $fieldVariable, |
||
| 1585 | $fieldTexts, |
||
| 1586 | array() |
||
| 1587 | ); |
||
| 1588 | |||
| 1589 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 1590 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
| 1591 | |||
| 1592 | if ($freezeElement) { |
||
| 1593 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1594 | } |
||
| 1595 | break; |
||
| 1596 | case self::FIELD_TYPE_VIDEO_URL: |
||
| 1597 | $form->addUrl( |
||
| 1598 | "extra_{$field_details['variable']}", |
||
| 1599 | $field_details['display_text'], |
||
| 1600 | false, |
||
| 1601 | ['placeholder' => 'https://'] |
||
| 1602 | ); |
||
| 1603 | if ($freezeElement) { |
||
| 1604 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1605 | } |
||
| 1606 | break; |
||
| 1607 | View Code Duplication | case self::FIELD_TYPE_LETTERS_ONLY: |
|
| 1608 | $form->addTextLettersOnly( |
||
| 1609 | "extra_{$field_details['variable']}", |
||
| 1610 | $field_details['display_text'] |
||
| 1611 | ); |
||
| 1612 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 1613 | |||
| 1614 | if ($freezeElement) { |
||
| 1615 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1616 | } |
||
| 1617 | break; |
||
| 1618 | View Code Duplication | case self::FIELD_TYPE_ALPHANUMERIC: |
|
| 1619 | $form->addTextAlphanumeric( |
||
| 1620 | "extra_{$field_details['variable']}", |
||
| 1621 | $field_details['display_text'] |
||
| 1622 | ); |
||
| 1623 | $form->applyFilter( |
||
| 1624 | 'extra_'.$field_details['variable'], |
||
| 1625 | 'stripslashes' |
||
| 1626 | ); |
||
| 1627 | if ($freezeElement) { |
||
| 1628 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1629 | } |
||
| 1630 | break; |
||
| 1631 | View Code Duplication | case self::FIELD_TYPE_LETTERS_SPACE: |
|
| 1632 | $form->addTextLettersAndSpaces( |
||
| 1633 | "extra_{$field_details['variable']}", |
||
| 1634 | $field_details['display_text'] |
||
| 1635 | ); |
||
| 1636 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 1637 | |||
| 1638 | if ($freezeElement) { |
||
| 1639 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1640 | } |
||
| 1641 | break; |
||
| 1642 | View Code Duplication | case self::FIELD_TYPE_ALPHANUMERIC_SPACE: |
|
| 1643 | $form->addTextAlphanumericAndSpaces( |
||
| 1644 | "extra_{$field_details['variable']}", |
||
| 1645 | $field_details['display_text'] |
||
| 1646 | ); |
||
| 1647 | $form->applyFilter( |
||
| 1648 | 'extra_'.$field_details['variable'], |
||
| 1649 | 'stripslashes' |
||
| 1650 | ); |
||
| 1651 | if ($freezeElement) { |
||
| 1652 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1653 | } |
||
| 1654 | break; |
||
| 1655 | case self::FIELD_TYPE_GEOLOCALIZATION: |
||
| 1656 | $dataValue = isset($extraData['extra_'.$field_details['variable']]) |
||
| 1657 | ? $extraData['extra_'.$field_details['variable']] |
||
| 1658 | : ''; |
||
| 1659 | $form->addElement( |
||
| 1660 | 'text', |
||
| 1661 | 'extra_'.$field_details['variable'], |
||
| 1662 | $field_details['display_text'], |
||
| 1663 | ['id' => 'extra_'.$field_details['variable']] |
||
| 1664 | ); |
||
| 1665 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 1666 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
| 1667 | if ($freezeElement) { |
||
| 1668 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1669 | } |
||
| 1670 | |||
| 1671 | $form->addHtml(" |
||
| 1672 | <script> |
||
| 1673 | $(document).ready(function() { |
||
| 1674 | if (typeof google === 'object') { |
||
| 1675 | var address = '$dataValue'; |
||
| 1676 | initializeGeo{$field_details['variable']}(address, false); |
||
| 1677 | |||
| 1678 | $('#geolocalization_extra_{$field_details['variable']}').on('click', function() { |
||
| 1679 | var address = $('#extra_{$field_details['variable']}').val(); |
||
| 1680 | initializeGeo{$field_details['variable']}(address, false); |
||
| 1681 | return false; |
||
| 1682 | }); |
||
| 1683 | |||
| 1684 | $('#myLocation_extra_{$field_details['variable']}').on('click', function() { |
||
| 1685 | myLocation{$field_details['variable']}(); |
||
| 1686 | return false; |
||
| 1687 | }); |
||
| 1688 | |||
| 1689 | $('#extra_{$field_details['variable']}').keypress(function(event) { |
||
| 1690 | if (event.which == 13) { |
||
| 1691 | $('#geolocalization_extra_{$field_details['variable']}').click(); |
||
| 1692 | return false; |
||
| 1693 | } |
||
| 1694 | }); |
||
| 1695 | |||
| 1696 | return; |
||
| 1697 | } |
||
| 1698 | |||
| 1699 | $('#map_extra_{$field_details['variable']}') |
||
| 1700 | .html('<div class=\"alert alert-info\">" |
||
| 1701 | .get_lang('YouNeedToActivateTheGoogleMapsPluginInAdminPlatformToSeeTheMap') |
||
| 1702 | ."</div>'); |
||
| 1703 | }); |
||
| 1704 | |||
| 1705 | function myLocation{$field_details['variable']}() { |
||
| 1706 | if (navigator.geolocation) { |
||
| 1707 | var geoPosition = function(position) { |
||
| 1708 | var lat = position.coords.latitude; |
||
| 1709 | var lng = position.coords.longitude; |
||
| 1710 | var latLng = new google.maps.LatLng(lat, lng); |
||
| 1711 | initializeGeo{$field_details['variable']}(false, latLng) |
||
| 1712 | }; |
||
| 1713 | |||
| 1714 | var geoError = function(error) { |
||
| 1715 | console.log(error); |
||
| 1716 | alert('Geocode ".get_lang('Error').": ' + error); |
||
| 1717 | }; |
||
| 1718 | |||
| 1719 | var geoOptions = { |
||
| 1720 | enableHighAccuracy: true |
||
| 1721 | }; |
||
| 1722 | |||
| 1723 | navigator.geolocation.getCurrentPosition(geoPosition, geoError, geoOptions); |
||
| 1724 | } |
||
| 1725 | } |
||
| 1726 | |||
| 1727 | function initializeGeo{$field_details['variable']}(address, latLng) { |
||
| 1728 | var geocoder = new google.maps.Geocoder(); |
||
| 1729 | var latlng = new google.maps.LatLng(-34.397, 150.644); |
||
| 1730 | var myOptions = { |
||
| 1731 | zoom: 15, |
||
| 1732 | center: latlng, |
||
| 1733 | mapTypeControl: true, |
||
| 1734 | mapTypeControlOptions: { |
||
| 1735 | style: google.maps.MapTypeControlStyle.DROPDOWN_MENU |
||
| 1736 | }, |
||
| 1737 | navigationControl: true, |
||
| 1738 | mapTypeId: google.maps.MapTypeId.ROADMAP |
||
| 1739 | }; |
||
| 1740 | |||
| 1741 | map_{$field_details['variable']} = new google.maps.Map( |
||
| 1742 | document.getElementById('map_extra_{$field_details['variable']}'), |
||
| 1743 | myOptions |
||
| 1744 | ); |
||
| 1745 | |||
| 1746 | var parameter = address ? {'address': address} : latLng ? {'latLng': latLng} : false; |
||
| 1747 | |||
| 1748 | if (geocoder && parameter) { |
||
| 1749 | geocoder.geocode(parameter, function(results, status) { |
||
| 1750 | if (status == google.maps.GeocoderStatus.OK) { |
||
| 1751 | if (status != google.maps.GeocoderStatus.ZERO_RESULTS) { |
||
| 1752 | map_{$field_details['variable']}.setCenter(results[0].geometry.location); |
||
| 1753 | if (!address) { |
||
| 1754 | $('#extra_{$field_details['variable']}').val(results[0].formatted_address); |
||
| 1755 | } |
||
| 1756 | var infowindow = new google.maps.InfoWindow({ |
||
| 1757 | content: '<b>' + $('#extra_{$field_details['variable']}').val() + '</b>', |
||
| 1758 | size: new google.maps.Size(150, 50) |
||
| 1759 | }); |
||
| 1760 | |||
| 1761 | var marker = new google.maps.Marker({ |
||
| 1762 | position: results[0].geometry.location, |
||
| 1763 | map: map_{$field_details['variable']}, |
||
| 1764 | title: $('#extra_{$field_details['variable']}').val() |
||
| 1765 | }); |
||
| 1766 | google.maps.event.addListener(marker, 'click', function() { |
||
| 1767 | infowindow.open(map_{$field_details['variable']}, marker); |
||
| 1768 | }); |
||
| 1769 | } else { |
||
| 1770 | alert('".get_lang('NotFound')."'); |
||
| 1771 | } |
||
| 1772 | } else { |
||
| 1773 | alert('Geocode ".get_lang('Error').": ".get_lang("AddressField") |
||
| 1774 | ." ".get_lang('NotFound')."'); |
||
| 1775 | } |
||
| 1776 | }); |
||
| 1777 | } |
||
| 1778 | } |
||
| 1779 | </script> |
||
| 1780 | "); |
||
| 1781 | $form->addHtml(' |
||
| 1782 | <div class="form-group"> |
||
| 1783 | <label for="geolocalization_extra_'.$field_details['variable'].'" |
||
| 1784 | class="col-sm-2 control-label"></label> |
||
| 1785 | <div class="col-sm-8"> |
||
| 1786 | <button class="null btn btn-default " |
||
| 1787 | id="geolocalization_extra_'.$field_details['variable'].'" |
||
| 1788 | name="geolocalization_extra_'.$field_details['variable'].'" |
||
| 1789 | type="submit"> |
||
| 1790 | <em class="fa fa-map-marker"></em> '.get_lang('Geolocalization').' |
||
| 1791 | </button> |
||
| 1792 | <button class="null btn btn-default " id="myLocation_extra_'.$field_details['variable'].'" |
||
| 1793 | name="myLocation_extra_'.$field_details['variable'].'" |
||
| 1794 | type="submit"> |
||
| 1795 | <em class="fa fa-crosshairs"></em> '.get_lang('MyLocation').' |
||
| 1796 | </button> |
||
| 1797 | </div> |
||
| 1798 | </div> |
||
| 1799 | '); |
||
| 1800 | |||
| 1801 | $form->addHtml(' |
||
| 1802 | <div class="form-group"> |
||
| 1803 | <label for="map_extra_'.$field_details['variable'].'" class="col-sm-2 control-label"> |
||
| 1804 | '.$field_details['display_text'].' - '.get_lang('Map').' |
||
| 1805 | </label> |
||
| 1806 | <div class="col-sm-8"> |
||
| 1807 | <div name="map_extra_'.$field_details['variable'].'" |
||
| 1808 | id="map_extra_'.$field_details['variable'].'" style="width:100%; height:300px;"> |
||
| 1809 | </div> |
||
| 1810 | </div> |
||
| 1811 | </div> |
||
| 1812 | '); |
||
| 1813 | break; |
||
| 1814 | case self::FIELD_TYPE_GEOLOCALIZATION_COORDINATES: |
||
| 1815 | $dataValue = isset($extraData['extra_'.$field_details['variable']]) |
||
| 1816 | ? $extraData['extra_'.$field_details['variable']] |
||
| 1817 | : ''; |
||
| 1818 | $form->addElement( |
||
| 1819 | 'text', |
||
| 1820 | 'extra_'.$field_details['variable'], |
||
| 1821 | $field_details['display_text'], |
||
| 1822 | ['id' => 'extra_'.$field_details['variable']] |
||
| 1823 | ); |
||
| 1824 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
| 1825 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
| 1826 | if ($freezeElement) { |
||
| 1827 | $form->freeze('extra_'.$field_details['variable']); |
||
| 1828 | } |
||
| 1829 | $latLag = explode(",", $dataValue); |
||
| 1830 | |||
| 1831 | // if no value, set default coordinates value |
||
| 1832 | if (empty($dataValue)) { |
||
| 1833 | $lat = '-34.397'; |
||
| 1834 | $lng = '150.644'; |
||
| 1835 | } else { |
||
| 1836 | $lat = $latLag[0]; |
||
| 1837 | $lng = $latLag[1]; |
||
| 1838 | } |
||
| 1839 | |||
| 1840 | $form->addHtml(" |
||
| 1841 | <script> |
||
| 1842 | $(document).ready(function() { |
||
| 1843 | if (typeof google === 'object') { |
||
| 1844 | var lat = '$lat'; |
||
| 1845 | var lng = '$lng'; |
||
| 1846 | var latLng = new google.maps.LatLng(lat, lng); |
||
| 1847 | initializeGeo{$field_details['variable']}(false, latLng); |
||
| 1848 | |||
| 1849 | $('#geolocalization_extra_{$field_details['variable']}').on('click', function() { |
||
| 1850 | var latLng = $('#extra_{$field_details['variable']}').val().split(','); |
||
| 1851 | var lat = latLng[0]; |
||
| 1852 | var lng = latLng[1]; |
||
| 1853 | var latLng = new google.maps.LatLng(lat, lng); |
||
| 1854 | initializeGeo{$field_details['variable']}(false, latLng); |
||
| 1855 | return false; |
||
| 1856 | }); |
||
| 1857 | |||
| 1858 | $('#myLocation_extra_{$field_details['variable']}').on('click', function() { |
||
| 1859 | myLocation{$field_details['variable']}(); |
||
| 1860 | return false; |
||
| 1861 | }); |
||
| 1862 | |||
| 1863 | $('#extra_{$field_details['variable']}').keypress(function (event) { |
||
| 1864 | if (event.which == 13) { |
||
| 1865 | $('#geolocalization_extra_{$field_details['variable']}').click(); |
||
| 1866 | return false; |
||
| 1867 | } |
||
| 1868 | }); |
||
| 1869 | |||
| 1870 | return; |
||
| 1871 | } |
||
| 1872 | |||
| 1873 | |||
| 1874 | $('#map_extra_{$field_details['variable']}') |
||
| 1875 | .html('<div class=\"alert alert-info\">" |
||
| 1876 | .get_lang('YouNeedToActivateTheGoogleMapsPluginInAdminPlatformToSeeTheMap') |
||
| 1877 | ."</div>'); |
||
| 1878 | }); |
||
| 1879 | |||
| 1880 | function myLocation{$field_details['variable']}() { |
||
| 1881 | if (navigator.geolocation) { |
||
| 1882 | var geoPosition = function(position) { |
||
| 1883 | var lat = position.coords.latitude; |
||
| 1884 | var lng = position.coords.longitude; |
||
| 1885 | var latLng = new google.maps.LatLng(lat, lng); |
||
| 1886 | initializeGeo{$field_details['variable']}(false, latLng) |
||
| 1887 | }; |
||
| 1888 | |||
| 1889 | var geoError = function(error) { |
||
| 1890 | alert('Geocode ".get_lang('Error').": ' + error); |
||
| 1891 | }; |
||
| 1892 | |||
| 1893 | var geoOptions = { |
||
| 1894 | enableHighAccuracy: true |
||
| 1895 | }; |
||
| 1896 | |||
| 1897 | navigator.geolocation.getCurrentPosition(geoPosition, geoError, geoOptions); |
||
| 1898 | } |
||
| 1899 | } |
||
| 1900 | |||
| 1901 | function initializeGeo{$field_details['variable']}(address, latLng) { |
||
| 1902 | var geocoder = new google.maps.Geocoder(); |
||
| 1903 | var latlng = new google.maps.LatLng(-34.397, 150.644); |
||
| 1904 | var myOptions = { |
||
| 1905 | zoom: 15, |
||
| 1906 | center: latlng, |
||
| 1907 | mapTypeControl: true, |
||
| 1908 | mapTypeControlOptions: { |
||
| 1909 | style: google.maps.MapTypeControlStyle.DROPDOWN_MENU |
||
| 1910 | }, |
||
| 1911 | navigationControl: true, |
||
| 1912 | mapTypeId: google.maps.MapTypeId.ROADMAP |
||
| 1913 | }; |
||
| 1914 | |||
| 1915 | map_{$field_details['variable']} = new google.maps.Map( |
||
| 1916 | document.getElementById('map_extra_{$field_details['variable']}'), |
||
| 1917 | myOptions |
||
| 1918 | ); |
||
| 1919 | |||
| 1920 | var parameter = address ? {'address': address} : latLng ? {'latLng': latLng} : false; |
||
| 1921 | |||
| 1922 | if (geocoder && parameter) { |
||
| 1923 | geocoder.geocode(parameter, function(results, status) { |
||
| 1924 | if (status == google.maps.GeocoderStatus.OK) { |
||
| 1925 | if (status != google.maps.GeocoderStatus.ZERO_RESULTS) { |
||
| 1926 | map_{$field_details['variable']}.setCenter(results[0].geometry.location); |
||
| 1927 | |||
| 1928 | $('#extra_{$field_details['variable']}') |
||
| 1929 | .val(results[0].geometry.location.lat() + ',' + results[0].geometry.location.lng()); |
||
| 1930 | |||
| 1931 | var infowindow = new google.maps.InfoWindow({ |
||
| 1932 | content: '<b>' + $('#extra_{$field_details['variable']}').val() + '</b>', |
||
| 1933 | size: new google.maps.Size(150, 50) |
||
| 1934 | }); |
||
| 1935 | |||
| 1936 | var marker = new google.maps.Marker({ |
||
| 1937 | position: results[0].geometry.location, |
||
| 1938 | map: map_{$field_details['variable']}, |
||
| 1939 | title: $('#extra_{$field_details['variable']}').val() |
||
| 1940 | }); |
||
| 1941 | google.maps.event.addListener(marker, 'click', function() { |
||
| 1942 | infowindow.open(map_{$field_details['variable']}, marker); |
||
| 1943 | }); |
||
| 1944 | } else { |
||
| 1945 | alert('".get_lang("NotFound")."'); |
||
| 1946 | } |
||
| 1947 | |||
| 1948 | } else { |
||
| 1949 | alert('Geocode ".get_lang('Error').": ' + status); |
||
| 1950 | } |
||
| 1951 | }); |
||
| 1952 | } |
||
| 1953 | } |
||
| 1954 | </script> |
||
| 1955 | "); |
||
| 1956 | $form->addHtml(' |
||
| 1957 | <div class="form-group"> |
||
| 1958 | <label for="geolocalization_extra_'.$field_details['variable'].'" |
||
| 1959 | class="col-sm-2 control-label"></label> |
||
| 1960 | <div class="col-sm-8"> |
||
| 1961 | <button class="null btn btn-default " |
||
| 1962 | id="geolocalization_extra_'.$field_details['variable'].'" |
||
| 1963 | name="geolocalization_extra_'.$field_details['variable'].'" |
||
| 1964 | type="submit"> |
||
| 1965 | <em class="fa fa-map-marker"></em> '.get_lang('Geolocalization').' |
||
| 1966 | </button> |
||
| 1967 | <button class="null btn btn-default" |
||
| 1968 | id="myLocation_extra_'.$field_details['variable'].'" |
||
| 1969 | name="myLocation_extra_'.$field_details['variable'].'" type="submit"> |
||
| 1970 | <em class="fa fa-crosshairs"></em> '.get_lang('MyLocation').' |
||
| 1971 | </button> |
||
| 1972 | </div> |
||
| 1973 | </div> |
||
| 1974 | '); |
||
| 1975 | |||
| 1976 | $form->addHtml(' |
||
| 1977 | <div class="form-group"> |
||
| 1978 | <label for="map_extra_'.$field_details['variable'].'" class="col-sm-2 control-label"> |
||
| 1979 | '.$field_details['display_text'].' - '.get_lang('Map').' |
||
| 1980 | </label> |
||
| 1981 | <div class="col-sm-8"> |
||
| 1982 | <div name="map_extra_'.$field_details['variable'].'" |
||
| 1983 | id="map_extra_'.$field_details['variable'].'" |
||
| 1984 | style="width:100%; height:300px;"> |
||
| 1985 | </div> |
||
| 1986 | </div> |
||
| 1987 | </div> |
||
| 1988 | '); |
||
| 1989 | break; |
||
| 1990 | } |
||
| 1991 | } |
||
| 1992 | } |
||
| 1993 | |||
| 1994 | $return = array(); |
||
| 1995 | $return['jquery_ready_content'] = $jquery_ready_content; |
||
| 1996 | |||
| 1997 | return $return; |
||
| 1998 | } |
||
| 1999 | |||
| 2871 |
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.