Conditions | 145 |
Paths | 3 |
Total Lines | 995 |
Code Lines | 573 |
Lines | 215 |
Ratio | 21.61 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
791 | public function set_extra_fields_in_form( |
||
792 | $form, |
||
793 | $extraData, |
||
794 | $admin_permissions = false, |
||
795 | $extra = array(), |
||
796 | $itemId = null, |
||
797 | $exclude = [], |
||
798 | $useTagAsSelect = false, |
||
799 | $showOnlyThisFields = [], |
||
800 | $orderFields = [] |
||
801 | ) { |
||
802 | $type = $this->type; |
||
803 | $jquery_ready_content = null; |
||
804 | if (!empty($extra)) { |
||
805 | |||
806 | $newOrder = []; |
||
807 | if (!empty($orderFields)) { |
||
808 | foreach ($orderFields as $order) { |
||
809 | foreach ($extra as $field_details) { |
||
810 | if ($order == $field_details['variable']) { |
||
811 | $newOrder[] = $field_details; |
||
812 | } |
||
813 | } |
||
814 | } |
||
815 | $extra = $newOrder; |
||
816 | } |
||
817 | |||
818 | foreach ($extra as $field_details) { |
||
819 | if (!empty($showOnlyThisFields)) { |
||
820 | if (!in_array($field_details['variable'], $showOnlyThisFields)) { |
||
821 | continue; |
||
822 | } |
||
823 | } |
||
824 | |||
825 | // Getting default value id if is set |
||
826 | $defaultValueId = null; |
||
827 | if (isset($field_details['options']) && !empty($field_details['options'])) { |
||
828 | $valueToFind = null; |
||
829 | if (isset($field_details['field_default_value'])) { |
||
830 | $valueToFind = $field_details['field_default_value']; |
||
831 | } |
||
832 | // If a value is found we override the default value |
||
833 | if (isset($extraData['extra_'.$field_details['variable']])) { |
||
834 | $valueToFind = $extraData['extra_'.$field_details['variable']]; |
||
835 | } |
||
836 | |||
837 | foreach ($field_details['options'] as $option) { |
||
838 | if ($option['option_value'] == $valueToFind) { |
||
839 | $defaultValueId = $option['id']; |
||
840 | } |
||
841 | } |
||
842 | } |
||
843 | |||
844 | if (!$admin_permissions) { |
||
845 | if ($field_details['visible'] == 0) { |
||
846 | continue; |
||
847 | } |
||
848 | |||
849 | if (in_array($field_details['variable'], $exclude)) { |
||
850 | continue; |
||
851 | } |
||
852 | } |
||
853 | |||
854 | switch ($field_details['field_type']) { |
||
855 | case ExtraField::FIELD_TYPE_TEXT: |
||
856 | $form->addElement( |
||
857 | 'text', |
||
858 | 'extra_'.$field_details['variable'], |
||
859 | $field_details['display_text'], |
||
860 | array() |
||
861 | ); |
||
862 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
863 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
864 | if (!$admin_permissions) { |
||
865 | if ($field_details['visible'] == 0) { |
||
866 | $form->freeze( |
||
867 | 'extra_'.$field_details['variable'] |
||
868 | ); |
||
869 | } |
||
870 | } |
||
871 | break; |
||
872 | View Code Duplication | case ExtraField::FIELD_TYPE_TEXTAREA: |
|
873 | $form->addHtmlEditor( |
||
874 | 'extra_'.$field_details['variable'], |
||
875 | $field_details['display_text'], |
||
876 | false, |
||
877 | false, |
||
878 | array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130') |
||
879 | ); |
||
880 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
881 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
882 | if (!$admin_permissions) { |
||
883 | if ($field_details['visible'] == 0) { |
||
884 | $form->freeze( |
||
885 | 'extra_'.$field_details['variable'] |
||
886 | ); |
||
887 | } |
||
888 | } |
||
889 | break; |
||
890 | case ExtraField::FIELD_TYPE_RADIO: |
||
891 | $group = array(); |
||
892 | if (isset($field_details['options']) && !empty($field_details['options'])) { |
||
893 | View Code Duplication | foreach ($field_details['options'] as $option_details) { |
|
894 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
895 | $group[] = $form->createElement( |
||
896 | 'radio', |
||
897 | 'extra_'.$field_details['variable'], |
||
898 | $option_details['option_value'], |
||
899 | $option_details['display_text'].'<br />', |
||
900 | $option_details['option_value'] |
||
901 | ); |
||
902 | } |
||
903 | } |
||
904 | $form->addGroup( |
||
905 | $group, |
||
906 | 'extra_'.$field_details['variable'], |
||
907 | $field_details['display_text'], |
||
908 | '' |
||
909 | ); |
||
910 | if (!$admin_permissions) { |
||
911 | if ($field_details['visible'] == 0) { |
||
912 | $form->freeze( |
||
913 | 'extra_'.$field_details['variable'] |
||
914 | ); |
||
915 | } |
||
916 | } |
||
917 | break; |
||
918 | case ExtraField::FIELD_TYPE_CHECKBOX: |
||
919 | $group = array(); |
||
920 | if (isset($field_details['options']) && !empty($field_details['options'])) { |
||
921 | View Code Duplication | foreach ($field_details['options'] as $option_details) { |
|
922 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
923 | $group[] = $form->createElement( |
||
924 | 'checkbox', |
||
925 | 'extra_'.$field_details['variable'], |
||
926 | $option_details['option_value'], |
||
927 | $option_details['display_text'].'<br />', |
||
928 | $option_details['option_value'] |
||
929 | ); |
||
930 | } |
||
931 | } else { |
||
932 | $fieldVariable = "extra_{$field_details['variable']}"; |
||
933 | $checkboxAttributes = array(); |
||
934 | if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) { |
||
935 | if (!empty($extraData[$fieldVariable])) { |
||
936 | $checkboxAttributes['checked'] = 1; |
||
937 | } |
||
938 | } |
||
939 | |||
940 | // We assume that is a switch on/off with 1 and 0 as values |
||
941 | $group[] = $form->createElement( |
||
942 | 'checkbox', |
||
943 | 'extra_'.$field_details['variable'], |
||
944 | null, |
||
945 | //$field_details['display_text'].'<br />', |
||
946 | get_lang('Yes'), |
||
947 | $checkboxAttributes |
||
948 | ); |
||
949 | } |
||
950 | |||
951 | $form->addGroup( |
||
952 | $group, |
||
953 | 'extra_'.$field_details['variable'], |
||
954 | $field_details['display_text'], |
||
955 | '' |
||
956 | ); |
||
957 | if (!$admin_permissions) { |
||
958 | if ($field_details['visible'] == 0) { |
||
959 | $form->freeze( |
||
960 | 'extra_'.$field_details['variable'] |
||
961 | ); |
||
962 | } |
||
963 | } |
||
964 | break; |
||
965 | case ExtraField::FIELD_TYPE_SELECT: |
||
966 | $get_lang_variables = false; |
||
967 | if (in_array( |
||
968 | $field_details['variable'], |
||
969 | array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message') |
||
970 | ) |
||
971 | ) { |
||
972 | $get_lang_variables = true; |
||
973 | } |
||
974 | |||
975 | // Get extra field workflow |
||
976 | $userInfo = api_get_user_info(); |
||
977 | $addOptions = array(); |
||
978 | $optionsExists = false; |
||
979 | global $app; |
||
980 | // Check if exist $app['orm.em'] object |
||
981 | if (isset($app['orm.em']) && is_object($app['orm.em'])) { |
||
982 | $optionsExists = $app['orm.em'] |
||
983 | ->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption') |
||
984 | ->findOneBy(array('fieldId' => $field_details['id'])); |
||
985 | } |
||
986 | |||
987 | if ($optionsExists) { |
||
988 | if (isset($userInfo['status']) && !empty($userInfo['status'])) { |
||
989 | |||
990 | $fieldWorkFlow = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption') |
||
991 | ->findBy( |
||
992 | array( |
||
993 | 'fieldId' => $field_details['id'], |
||
994 | 'relatedFieldOptionId' => $defaultValueId, |
||
995 | 'roleId' => $userInfo['status'] |
||
996 | ) |
||
997 | ); |
||
998 | foreach ($fieldWorkFlow as $item) { |
||
999 | $addOptions[] = $item->getFieldOptionId(); |
||
1000 | } |
||
1001 | } |
||
1002 | } |
||
1003 | |||
1004 | $options = array(); |
||
1005 | if (empty($defaultValueId)) { |
||
1006 | $options[''] = get_lang('SelectAnOption'); |
||
1007 | } |
||
1008 | |||
1009 | $optionList = array(); |
||
1010 | if (!empty($field_details['options'])) { |
||
1011 | foreach ($field_details['options'] as $option_details) { |
||
1012 | $optionList[$option_details['id']] = $option_details; |
||
1013 | if ($get_lang_variables) { |
||
1014 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
1015 | } else { |
||
1016 | if ($optionsExists) { |
||
1017 | // Adding always the default value |
||
1018 | View Code Duplication | if ($option_details['id'] == $defaultValueId) { |
|
1019 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
1020 | } else { |
||
1021 | if (isset($addOptions) && !empty($addOptions)) { |
||
1022 | // Parsing filters |
||
1023 | if (in_array($option_details['id'], $addOptions)) { |
||
1024 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
1025 | } |
||
1026 | } |
||
1027 | } |
||
1028 | } else { |
||
1029 | // Normal behaviour |
||
1030 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
1031 | } |
||
1032 | } |
||
1033 | } |
||
1034 | |||
1035 | if (isset($optionList[$defaultValueId])) { |
||
1036 | if (isset($optionList[$defaultValueId]['option_value']) && |
||
1037 | $optionList[$defaultValueId]['option_value'] == 'aprobada' |
||
1038 | ) { |
||
1039 | // @todo function don't exists api_is_question_manager |
||
1040 | /*if (api_is_question_manager() == false) { |
||
1041 | $form->freeze(); |
||
1042 | }*/ |
||
1043 | } |
||
1044 | } |
||
1045 | |||
1046 | // Setting priority message |
||
1047 | if (isset($optionList[$defaultValueId]) && |
||
1048 | isset($optionList[$defaultValueId]['priority']) |
||
1049 | ) { |
||
1050 | |||
1051 | if (!empty($optionList[$defaultValueId]['priority'])) { |
||
1052 | $priorityId = $optionList[$defaultValueId]['priority']; |
||
1053 | $option = new ExtraFieldOption($this->type); |
||
1054 | $messageType = $option->getPriorityMessageType($priorityId); |
||
1055 | $form->addElement( |
||
1056 | 'label', |
||
1057 | null, |
||
1058 | Display::return_message( |
||
1059 | $optionList[$defaultValueId]['priority_message'], |
||
1060 | $messageType |
||
1061 | ) |
||
1062 | ); |
||
1063 | } |
||
1064 | } |
||
1065 | } |
||
1066 | |||
1067 | // chzn-select doesn't work for sessions?? |
||
1068 | $form->addElement( |
||
1069 | 'select', |
||
1070 | 'extra_' . $field_details['variable'], |
||
1071 | $field_details['display_text'], |
||
1072 | $options, |
||
1073 | array('id' => 'extra_'.$field_details['variable']) |
||
1074 | ); |
||
1075 | |||
1076 | /* Enable this when field_loggeable is introduced as a table field (2.0) |
||
1077 | if ($optionsExists && $field_details['field_loggeable'] && !empty($defaultValueId)) { |
||
1078 | |||
1079 | $form->addElement( |
||
1080 | 'textarea', |
||
1081 | 'extra_' . $field_details['variable'] . '_comment', |
||
1082 | $field_details['display_text'] . ' ' . get_lang('Comment') |
||
1083 | ); |
||
1084 | |||
1085 | $extraFieldValue = new ExtraFieldValue($this->type); |
||
1086 | $repo = $app['orm.em']->getRepository($extraFieldValue->entityName); |
||
1087 | $repoLog = $app['orm.em']->getRepository('Gedmo\Loggable\Entity\LogEntry'); |
||
1088 | $newEntity = $repo->findOneBy( |
||
1089 | array( |
||
1090 | $this->handlerEntityId => $itemId, |
||
1091 | 'fieldId' => $field_details['id'] |
||
1092 | ) |
||
1093 | ); |
||
1094 | // @todo move this in a function inside the class |
||
1095 | if ($newEntity) { |
||
1096 | $logs = $repoLog->getLogEntries($newEntity); |
||
1097 | if (!empty($logs)) { |
||
1098 | $html = '<b>' . get_lang('LatestChanges') . '</b><br /><br />'; |
||
1099 | |||
1100 | $table = new HTML_Table(array('class' => 'data_table')); |
||
1101 | $table->setHeaderContents(0, 0, get_lang('Value')); |
||
1102 | $table->setHeaderContents(0, 1, get_lang('Comment')); |
||
1103 | $table->setHeaderContents(0, 2, get_lang('ModifyDate')); |
||
1104 | $table->setHeaderContents(0, 3, get_lang('Username')); |
||
1105 | $row = 1; |
||
1106 | foreach ($logs as $log) { |
||
1107 | $column = 0; |
||
1108 | $data = $log->getData(); |
||
1109 | $fieldValue = isset($data['fieldValue']) ? $data['fieldValue'] : null; |
||
1110 | $comment = isset($data['comment']) ? $data['comment'] : null; |
||
1111 | |||
1112 | $table->setCellContents($row, $column, $fieldValue); |
||
1113 | $column++; |
||
1114 | $table->setCellContents($row, $column, $comment); |
||
1115 | $column++; |
||
1116 | $table->setCellContents($row, $column, api_get_local_time($log->getLoggedAt()->format('Y-m-d H:i:s'))); |
||
1117 | $column++; |
||
1118 | $table->setCellContents($row, $column, $log->getUsername()); |
||
1119 | $row++; |
||
1120 | } |
||
1121 | $form->addElement('label', null, $html.$table->toHtml()); |
||
1122 | } |
||
1123 | } |
||
1124 | } |
||
1125 | */ |
||
1126 | |||
1127 | if (!$admin_permissions) { |
||
1128 | if ($field_details['visible'] == 0) { |
||
1129 | $form->freeze('extra_' . $field_details['variable']); |
||
1130 | } |
||
1131 | } |
||
1132 | break; |
||
1133 | case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: |
||
1134 | $options = array(); |
||
1135 | foreach ($field_details['options'] as $option_id => $option_details) { |
||
1136 | $options[$option_details['option_value']] = $option_details['display_text']; |
||
1137 | } |
||
1138 | $form->addElement( |
||
1139 | 'select', |
||
1140 | 'extra_'.$field_details['variable'], |
||
1141 | $field_details['display_text'], |
||
1142 | $options, |
||
1143 | array('multiple' => 'multiple', 'id' => 'extra_'.$field_details['variable']) |
||
1144 | ); |
||
1145 | if (!$admin_permissions) { |
||
1146 | if ($field_details['visible'] == 0) { |
||
1147 | $form->freeze('extra_'.$field_details['variable']); |
||
1148 | } |
||
1149 | } |
||
1150 | break; |
||
1151 | View Code Duplication | case ExtraField::FIELD_TYPE_DATE: |
|
1152 | $form->addDatePicker('extra_'.$field_details['variable'], $field_details['display_text']); |
||
1153 | if (!$admin_permissions) { |
||
1154 | if ($field_details['visible'] == 0) { |
||
1155 | $form->freeze('extra_'.$field_details['variable']); |
||
1156 | } |
||
1157 | } |
||
1158 | |||
1159 | $form->applyFilter('theme', 'trim'); |
||
1160 | break; |
||
1161 | case ExtraField::FIELD_TYPE_DATETIME: |
||
1162 | $form->addDateTimePicker( |
||
1163 | 'extra_'.$field_details['variable'], |
||
1164 | $field_details['display_text'] |
||
1165 | ); |
||
1166 | |||
1167 | $defaults['extra_'.$field_details['variable']] = api_get_local_time(); |
||
1168 | if (!isset($form->_defaultValues['extra_'.$field_details['variable']])) { |
||
1169 | $form->setDefaults($defaults); |
||
1170 | } |
||
1171 | if (!$admin_permissions) { |
||
1172 | if ($field_details['visible'] == 0) { |
||
1173 | $form->freeze('extra_'.$field_details['variable']); |
||
1174 | } |
||
1175 | } |
||
1176 | $form->applyFilter('theme', 'trim'); |
||
1177 | break; |
||
1178 | case ExtraField::FIELD_TYPE_DOUBLE_SELECT: |
||
1179 | $first_select_id = 'first_extra_'.$field_details['variable']; |
||
1180 | $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1'; |
||
1181 | |||
1182 | $jquery_ready_content .= ' |
||
1183 | $("#'.$first_select_id.'").on("change", function() { |
||
1184 | var id = $(this).val(); |
||
1185 | if (id) { |
||
1186 | $.ajax({ |
||
1187 | url: "'.$url.'&a=get_second_select_options", |
||
1188 | dataType: "json", |
||
1189 | data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id, |
||
1190 | success: function(data) { |
||
1191 | $("#second_extra_'.$field_details['variable'].'").empty(); |
||
1192 | $.each(data, function(index, value) { |
||
1193 | $("#second_extra_'.$field_details['variable'].'").append($("<option/>", { |
||
1194 | value: index, |
||
1195 | text: value |
||
1196 | })); |
||
1197 | }); |
||
1198 | $("#second_extra_'.$field_details['variable'].'").selectpicker("refresh"); |
||
1199 | }, |
||
1200 | }); |
||
1201 | } else { |
||
1202 | $("#second_extra_'.$field_details['variable'].'").empty(); |
||
1203 | } |
||
1204 | });'; |
||
1205 | |||
1206 | $first_id = null; |
||
1207 | if (!empty($extraData)) { |
||
1208 | if (isset($extraData['extra_'.$field_details['variable']])) { |
||
1209 | $first_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable']]; |
||
1210 | } |
||
1211 | } |
||
1212 | |||
1213 | $options = ExtraField::extra_field_double_select_convert_array_to_ordered_array( |
||
1214 | $field_details['options'] |
||
1215 | ); |
||
1216 | $values = array('' => get_lang('Select')); |
||
1217 | |||
1218 | $second_values = array(); |
||
1219 | if (!empty($options)) { |
||
1220 | foreach ($options as $option) { |
||
1221 | foreach ($option as $sub_option) { |
||
1222 | if ($sub_option['option_value'] == '0') { |
||
1223 | $values[$sub_option['id']] = $sub_option['display_text']; |
||
1224 | } else { |
||
1225 | if ($first_id === $sub_option['option_value']) { |
||
1226 | $second_values[$sub_option['id']] = $sub_option['display_text']; |
||
1227 | } |
||
1228 | } |
||
1229 | } |
||
1230 | } |
||
1231 | } |
||
1232 | $group = array(); |
||
1233 | $group[] = $form->createElement( |
||
1234 | 'select', |
||
1235 | 'extra_'.$field_details['variable'], |
||
1236 | null, |
||
1237 | $values, |
||
1238 | array('id' => $first_select_id) |
||
1239 | ); |
||
1240 | $group[] = $form->createElement( |
||
1241 | 'select', |
||
1242 | 'extra_'.$field_details['variable'].'_second', |
||
1243 | null, |
||
1244 | $second_values, |
||
1245 | array('id' => 'second_extra_'.$field_details['variable']) |
||
1246 | ); |
||
1247 | $form->addGroup( |
||
1248 | $group, |
||
1249 | 'extra_'.$field_details['variable'], |
||
1250 | $field_details['display_text'], |
||
1251 | ' ' |
||
1252 | ); |
||
1253 | |||
1254 | if (!$admin_permissions) { |
||
1255 | if ($field_details['visible'] == 0) { |
||
1256 | $form->freeze('extra_'.$field_details['variable']); |
||
1257 | } |
||
1258 | } |
||
1259 | break; |
||
1260 | case ExtraField::FIELD_TYPE_DIVIDER: |
||
1261 | $form->addElement( |
||
1262 | 'static', |
||
1263 | $field_details['variable'], |
||
1264 | '<br /><strong>'.$field_details['display_text'].'</strong>' |
||
1265 | ); |
||
1266 | break; |
||
1267 | case ExtraField::FIELD_TYPE_TAG: |
||
1268 | $variable = $field_details['variable']; |
||
1269 | $field_id = $field_details['id']; |
||
1270 | |||
1271 | $tagsSelect = $form->addSelect( |
||
1272 | "extra_{$field_details['variable']}", |
||
1273 | $field_details['display_text'] |
||
1274 | ); |
||
1275 | |||
1276 | if ($useTagAsSelect == false) { |
||
1277 | $tagsSelect->setAttribute('class', null); |
||
1278 | } |
||
1279 | |||
1280 | $tagsSelect->setAttribute('id', "extra_{$field_details['variable']}"); |
||
1281 | $tagsSelect->setMultiple(true); |
||
1282 | |||
1283 | if ($this->type == 'user') { |
||
1284 | // The magic should be here |
||
1285 | $user_tags = UserManager::get_user_tags($itemId, $field_details['id']); |
||
1286 | |||
1287 | if (is_array($user_tags) && count($user_tags) > 0) { |
||
1288 | foreach ($user_tags as $tag) { |
||
1289 | $tagsSelect->addOption( |
||
1290 | $tag['tag'], |
||
1291 | $tag['tag'], |
||
1292 | ['selected' => 'selected', 'class' => 'selected'] |
||
1293 | ); |
||
1294 | } |
||
1295 | } |
||
1296 | $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php'; |
||
1297 | } else { |
||
1298 | $em = Database::getManager(); |
||
1299 | |||
1300 | $fieldTags = $em |
||
1301 | ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag') |
||
1302 | ->findBy([ |
||
1303 | 'fieldId' => $field_id, |
||
1304 | 'itemId' => $itemId |
||
1305 | ]); |
||
1306 | foreach ($fieldTags as $fieldTag) { |
||
1307 | $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId()); |
||
1308 | |||
1309 | if (empty($tag)) { |
||
1310 | continue; |
||
1311 | } |
||
1312 | $tagsSelect->addOption( |
||
1313 | $tag->getTag(), |
||
1314 | $tag->getTag(), |
||
1315 | ['selected' => 'selected', 'class' => 'selected'] |
||
1316 | ); |
||
1317 | } |
||
1318 | |||
1319 | if ($useTagAsSelect) { |
||
1320 | |||
1321 | $fieldTags = $em |
||
1322 | ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag') |
||
1323 | ->findBy([ |
||
1324 | 'fieldId' => $field_id |
||
1325 | ]); |
||
1326 | $tagsAdded = []; |
||
1327 | foreach ($fieldTags as $fieldTag) { |
||
1328 | $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId()); |
||
1329 | |||
1330 | if (empty($tag)) { |
||
1331 | continue; |
||
1332 | } |
||
1333 | |||
1334 | $tagText = $tag->getTag(); |
||
1335 | |||
1336 | if (in_array($tagText, $tagsAdded)) { |
||
1337 | continue; |
||
1338 | } |
||
1339 | |||
1340 | $tagsSelect->addOption( |
||
1341 | $tag->getTag(), |
||
1342 | $tag->getTag(), |
||
1343 | [] |
||
1344 | ); |
||
1345 | |||
1346 | $tagsAdded[] = $tagText; |
||
1347 | } |
||
1348 | |||
1349 | } |
||
1350 | |||
1351 | $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php'; |
||
1352 | } |
||
1353 | |||
1354 | if ($useTagAsSelect == false) { |
||
1355 | $complete_text = get_lang('StartToType'); |
||
1356 | |||
1357 | //if cache is set to true the jquery will be called 1 time |
||
1358 | |||
1359 | $jquery_ready_content .= <<<EOF |
||
1360 | $("#extra_$variable").fcbkcomplete({ |
||
1361 | json_url: "$url?a=search_tags&field_id=$field_id&type={$this->type}", |
||
1362 | cache: false, |
||
1363 | filter_case: true, |
||
1364 | filter_hide: true, |
||
1365 | complete_text:"$complete_text", |
||
1366 | firstselected: false, |
||
1367 | filter_selected: true, |
||
1368 | newel: true |
||
1369 | }); |
||
1370 | EOF; |
||
1371 | } |
||
1372 | break; |
||
1373 | View Code Duplication | case ExtraField::FIELD_TYPE_TIMEZONE: |
|
1374 | $form->addElement( |
||
1375 | 'select', |
||
1376 | 'extra_'.$field_details['variable'], |
||
1377 | $field_details['display_text'], |
||
1378 | api_get_timezones(), |
||
1379 | '' |
||
1380 | ); |
||
1381 | if ($field_details['visible'] == 0) { |
||
1382 | $form->freeze( |
||
1383 | 'extra_'.$field_details['variable'] |
||
1384 | ); |
||
1385 | } |
||
1386 | break; |
||
1387 | case ExtraField::FIELD_TYPE_SOCIAL_PROFILE: |
||
1388 | // get the social network's favicon |
||
1389 | $extra_data_variable = isset($extraData['extra_'.$field_details['variable']]) ? $extraData['extra_'.$field_details['variable']] : null; |
||
1390 | $field_default_value = isset($field_details['field_default_value']) ? $field_details['field_default_value'] : null; |
||
1391 | $icon_path = UserManager::get_favicon_from_url( |
||
1392 | $extra_data_variable, |
||
1393 | $field_default_value |
||
1394 | ); |
||
1395 | // special hack for hi5 |
||
1396 | $leftpad = '1.7'; |
||
1397 | $top = '0.4'; |
||
1398 | $domain = parse_url($icon_path, PHP_URL_HOST); |
||
1399 | if ($domain == 'www.hi5.com' or $domain == 'hi5.com') { |
||
1400 | $leftpad = '3'; |
||
1401 | $top = '0'; |
||
1402 | } |
||
1403 | // print the input field |
||
1404 | $form->addElement( |
||
1405 | 'text', |
||
1406 | 'extra_'.$field_details['variable'], |
||
1407 | $field_details['display_text'], |
||
1408 | array( |
||
1409 | 'size' => 60, |
||
1410 | 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; ' |
||
1411 | ) |
||
1412 | ); |
||
1413 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
1414 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
1415 | if ($field_details['visible'] == 0) { |
||
1416 | $form->freeze('extra_'.$field_details['variable']); |
||
1417 | } |
||
1418 | break; |
||
1419 | View Code Duplication | case ExtraField::FIELD_TYPE_MOBILE_PHONE_NUMBER: |
|
1420 | $form->addElement( |
||
1421 | 'text', |
||
1422 | 'extra_'.$field_details[1], |
||
1423 | $field_details[3]." (".get_lang('CountryDialCode').")", |
||
1424 | array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx') |
||
1425 | ); |
||
1426 | $form->applyFilter('extra_'.$field_details[1], 'stripslashes'); |
||
1427 | $form->applyFilter('extra_'.$field_details[1], 'trim'); |
||
1428 | $form->applyFilter('extra_'.$field_details[1], 'mobile_phone_number_filter'); |
||
1429 | $form->addRule( |
||
1430 | 'extra_'.$field_details[1], |
||
1431 | get_lang('MobilePhoneNumberWrong'), |
||
1432 | 'mobile_phone_number' |
||
1433 | ); |
||
1434 | if ($field_details['visible'] == 0) { |
||
1435 | $form->freeze('extra_'.$field_details['variable']); |
||
1436 | } |
||
1437 | break; |
||
1438 | View Code Duplication | case ExtraField::FIELD_TYPE_INTEGER: |
|
1439 | $form->addElement( |
||
1440 | 'number', |
||
1441 | 'extra_'.$field_details['variable'], |
||
1442 | $field_details['display_text'], |
||
1443 | array('class' => 'span1', 'step' => 1) |
||
1444 | ); |
||
1445 | |||
1446 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
1447 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
1448 | $form->applyFilter('extra_'.$field_details['variable'], 'intval'); |
||
1449 | |||
1450 | if (!$admin_permissions) { |
||
1451 | if ($field_details['visible'] == 0) { |
||
1452 | $form->freeze( |
||
1453 | 'extra_'.$field_details['variable'] |
||
1454 | ); |
||
1455 | } |
||
1456 | } |
||
1457 | break; |
||
1458 | case ExtraField::FIELD_TYPE_FILE_IMAGE: |
||
1459 | $fieldVariable = "extra_{$field_details['variable']}"; |
||
1460 | |||
1461 | $fieldTexts = [ |
||
1462 | $field_details['display_text'] |
||
1463 | ]; |
||
1464 | |||
1465 | View Code Duplication | if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) { |
|
1466 | |||
1467 | if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) { |
||
1468 | $fieldTexts[] = Display::img( |
||
1469 | api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable], |
||
1470 | $field_details['display_text'], |
||
1471 | ['width' => '300'] |
||
1472 | ); |
||
1473 | } |
||
1474 | } |
||
1475 | |||
1476 | if ($fieldTexts[0] === 'Image') { |
||
1477 | $fieldTexts[0] = get_lang($fieldTexts[0]); |
||
1478 | } |
||
1479 | |||
1480 | $form->addElement( |
||
1481 | 'file', |
||
1482 | $fieldVariable, |
||
1483 | $fieldTexts, |
||
1484 | ['accept' => 'image/*', 'id' => 'extra_image'] |
||
1485 | ); |
||
1486 | |||
1487 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
1488 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
1489 | |||
1490 | $allowed_picture_types = ['jpg', 'jpeg', 'png', 'gif']; |
||
1491 | $form->addRule( |
||
1492 | 'extra_'.$field_details['variable'], |
||
1493 | get_lang('OnlyImagesAllowed') . ' ('.implode(',', $allowed_picture_types).')', |
||
1494 | 'filetype', |
||
1495 | $allowed_picture_types |
||
1496 | ); |
||
1497 | |||
1498 | if (!$admin_permissions) { |
||
1499 | if ($field_details['visible'] == 0) { |
||
1500 | $form->freeze( |
||
1501 | 'extra_'.$field_details['variable'] |
||
1502 | ); |
||
1503 | } |
||
1504 | } |
||
1505 | break; |
||
1506 | View Code Duplication | case ExtraField::FIELD_TYPE_FLOAT: |
|
1507 | $form->addElement( |
||
1508 | 'number', |
||
1509 | 'extra_'.$field_details['variable'], |
||
1510 | $field_details['display_text'], |
||
1511 | array('class' => 'span1', 'step' => '0.01') |
||
1512 | ); |
||
1513 | |||
1514 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
1515 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
1516 | $form->applyFilter('extra_'.$field_details['variable'], 'floatval'); |
||
1517 | |||
1518 | if (!$admin_permissions) { |
||
1519 | if ($field_details['visible'] == 0) { |
||
1520 | $form->freeze( |
||
1521 | 'extra_'.$field_details['variable'] |
||
1522 | ); |
||
1523 | } |
||
1524 | } |
||
1525 | break; |
||
1526 | case ExtraField::FIELD_TYPE_FILE: |
||
1527 | $fieldVariable = "extra_{$field_details['variable']}"; |
||
1528 | $fieldTexts = array( |
||
1529 | $field_details['display_text'] |
||
1530 | ); |
||
1531 | |||
1532 | if (is_array($extraData) && |
||
1533 | array_key_exists($fieldVariable, $extraData) |
||
1534 | ) { |
||
1535 | View Code Duplication | if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) { |
|
1536 | $fieldTexts[] = Display::url( |
||
1537 | api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable], |
||
1538 | api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable], |
||
1539 | array( |
||
1540 | 'title' => $field_details['display_text'], |
||
1541 | 'target' => '_blank' |
||
1542 | ) |
||
1543 | ); |
||
1544 | } |
||
1545 | } |
||
1546 | |||
1547 | $form->addElement( |
||
1548 | 'file', |
||
1549 | $fieldVariable, |
||
1550 | $fieldTexts, |
||
1551 | array() |
||
1552 | ); |
||
1553 | |||
1554 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
1555 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
1556 | |||
1557 | if (!$admin_permissions) { |
||
1558 | if ($field_details['visible'] == 0) { |
||
1559 | $form->freeze( |
||
1560 | 'extra_'.$field_details['variable'] |
||
1561 | ); |
||
1562 | } |
||
1563 | } |
||
1564 | break; |
||
1565 | case ExtraField::FIELD_TYPE_VIDEO_URL: |
||
1566 | $form->addUrl( |
||
1567 | "extra_{$field_details['variable']}", |
||
1568 | $field_details['display_text'], |
||
1569 | false, |
||
1570 | ['placeholder' => 'https://'] |
||
1571 | ); |
||
1572 | break; |
||
1573 | View Code Duplication | case ExtraField::FIELD_TYPE_LETTERS_ONLY: |
|
1574 | $form->addTextLettersOnly( |
||
1575 | "extra_{$field_details['variable']}", |
||
1576 | $field_details['display_text'] |
||
1577 | ); |
||
1578 | $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes'); |
||
1579 | |||
1580 | if (!$admin_permissions) { |
||
1581 | if ($field_details['visible'] == 0) { |
||
1582 | $form->freeze( |
||
1583 | 'extra_' . $field_details['variable'] |
||
1584 | ); |
||
1585 | } |
||
1586 | } |
||
1587 | break; |
||
1588 | View Code Duplication | case ExtraField::FIELD_TYPE_ALPHANUMERIC: |
|
1589 | $form->addTextAlphanumeric( |
||
1590 | "extra_{$field_details['variable']}", |
||
1591 | $field_details['display_text'] |
||
1592 | ); |
||
1593 | $form->applyFilter( |
||
1594 | 'extra_' . $field_details['variable'], |
||
1595 | 'stripslashes' |
||
1596 | ); |
||
1597 | if (!$admin_permissions) { |
||
1598 | if ($field_details['visible'] == 0) { |
||
1599 | $form->freeze( |
||
1600 | 'extra_' . $field_details['variable'] |
||
1601 | ); |
||
1602 | } |
||
1603 | } |
||
1604 | break; |
||
1605 | View Code Duplication | case ExtraField::FIELD_TYPE_LETTERS_SPACE: |
|
1606 | $form->addTextLettersAndSpaces( |
||
1607 | "extra_{$field_details['variable']}", |
||
1608 | $field_details['display_text'] |
||
1609 | ); |
||
1610 | $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes'); |
||
1611 | |||
1612 | if (!$admin_permissions) { |
||
1613 | if ($field_details['visible'] == 0) { |
||
1614 | $form->freeze( |
||
1615 | 'extra_' . $field_details['variable'] |
||
1616 | ); |
||
1617 | } |
||
1618 | } |
||
1619 | break; |
||
1620 | View Code Duplication | case ExtraField::FIELD_TYPE_ALPHANUMERIC_SPACE: |
|
1621 | $form->addTextAlphanumericAndSpaces( |
||
1622 | "extra_{$field_details['variable']}", |
||
1623 | $field_details['display_text'] |
||
1624 | ); |
||
1625 | $form->applyFilter( |
||
1626 | 'extra_' . $field_details['variable'], |
||
1627 | 'stripslashes' |
||
1628 | ); |
||
1629 | if (!$admin_permissions) { |
||
1630 | if ($field_details['visible'] == 0) { |
||
1631 | $form->freeze( |
||
1632 | 'extra_' . $field_details['variable'] |
||
1633 | ); |
||
1634 | } |
||
1635 | } |
||
1636 | break; |
||
1637 | case ExtraField::FIELD_TYPE_GEOLOCALIZATION: |
||
1638 | $dataValue = isset($extraData['extra_'.$field_details['variable']]) |
||
1639 | ? $extraData['extra_'.$field_details['variable']] |
||
1640 | : ''; |
||
1641 | $form->addElement( |
||
1642 | 'text', |
||
1643 | 'extra_'.$field_details['variable'], |
||
1644 | $field_details['display_text'], |
||
1645 | ['id' => 'extra_'.$field_details['variable']] |
||
1646 | ); |
||
1647 | $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes'); |
||
1648 | $form->applyFilter('extra_'.$field_details['variable'], 'trim'); |
||
1649 | if (!$admin_permissions) { |
||
1650 | if ($field_details['visible'] == 0) { |
||
1651 | $form->freeze( |
||
1652 | 'extra_'.$field_details['variable'] |
||
1653 | ); |
||
1654 | } |
||
1655 | } |
||
1656 | |||
1657 | $form->addHtml( |
||
1658 | '<script> |
||
1659 | $(document).ready(function() { |
||
1660 | |||
1661 | var address = "' . $dataValue . '"; |
||
1662 | initializeGeo'.$field_details['variable'].'(address, false); |
||
1663 | |||
1664 | $("#geolocalization_extra_'.$field_details['variable'].'").on("click", function() { |
||
1665 | var address = $("#extra_'.$field_details['variable'].'").val(); |
||
1666 | initializeGeo'.$field_details['variable'].'(address, false); |
||
1667 | return false; |
||
1668 | }); |
||
1669 | |||
1670 | $("#myLocation_extra_'.$field_details['variable'].'").on("click", function() { |
||
1671 | myLocation'.$field_details['variable'].'(); |
||
1672 | return false; |
||
1673 | }); |
||
1674 | |||
1675 | $("#extra_'.$field_details['variable'].'").keypress(function (event) { |
||
1676 | if (event.which == 13) { |
||
1677 | $("#geolocalization_extra_'.$field_details['variable'].'").click(); |
||
1678 | return false; |
||
1679 | } |
||
1680 | }); |
||
1681 | }); |
||
1682 | |||
1683 | function myLocation'.$field_details['variable'].'() { |
||
1684 | if (navigator.geolocation) { |
||
1685 | var geoPosition = function(position) { |
||
1686 | var lat = position.coords.latitude; |
||
1687 | var lng = position.coords.longitude; |
||
1688 | var latLng = new google.maps.LatLng(lat, lng); |
||
1689 | initializeGeo'.$field_details['variable'].'(false, latLng) |
||
1690 | }; |
||
1691 | |||
1692 | var geoError = function(error) { |
||
1693 | alert("Geocode ' . get_lang('Error') . ': " + error); |
||
1694 | }; |
||
1695 | |||
1696 | var geoOptions = { |
||
1697 | enableHighAccuracy: true |
||
1698 | }; |
||
1699 | |||
1700 | navigator.geolocation.getCurrentPosition(geoPosition, geoError, geoOptions); |
||
1701 | } |
||
1702 | } |
||
1703 | |||
1704 | function initializeGeo'.$field_details['variable'].'(address, latLng) { |
||
1705 | var geocoder = new google.maps.Geocoder(); |
||
1706 | var latlng = new google.maps.LatLng(-34.397, 150.644); |
||
1707 | var myOptions = { |
||
1708 | zoom: 15, |
||
1709 | center: latlng, |
||
1710 | mapTypeControl: true, |
||
1711 | mapTypeControlOptions: { |
||
1712 | style: google.maps.MapTypeControlStyle.DROPDOWN_MENU |
||
1713 | }, |
||
1714 | navigationControl: true, |
||
1715 | mapTypeId: google.maps.MapTypeId.ROADMAP |
||
1716 | }; |
||
1717 | |||
1718 | map_'.$field_details['variable'].' = new google.maps.Map(document.getElementById("map_extra_'.$field_details['variable'].'"), myOptions); |
||
1719 | |||
1720 | var parameter = address ? { "address": address } : latLng ? { "latLng": latLng } : false; |
||
1721 | |||
1722 | if (geocoder && parameter) { |
||
1723 | geocoder.geocode(parameter, function(results, status) { |
||
1724 | if (status == google.maps.GeocoderStatus.OK) { |
||
1725 | if (status != google.maps.GeocoderStatus.ZERO_RESULTS) { |
||
1726 | map_'.$field_details['variable'].'.setCenter(results[0].geometry.location); |
||
1727 | if (!address) { |
||
1728 | $("#extra_'.$field_details['variable'].'").val(results[0].formatted_address); |
||
1729 | } |
||
1730 | var infowindow = new google.maps.InfoWindow({ |
||
1731 | content: "<b>" + $("#extra_'.$field_details['variable'].'").val() + "</b>", |
||
1732 | size: new google.maps.Size(150, 50) |
||
1733 | }); |
||
1734 | |||
1735 | var marker = new google.maps.Marker({ |
||
1736 | position: results[0].geometry.location, |
||
1737 | map: map_'.$field_details['variable'].', |
||
1738 | title: $("#extra_'.$field_details['variable'].'").val() |
||
1739 | }); |
||
1740 | google.maps.event.addListener(marker, "click", function() { |
||
1741 | infowindow.open(map_'.$field_details['variable'].', marker); |
||
1742 | }); |
||
1743 | } else { |
||
1744 | alert("' . get_lang("NotFound") . '"); |
||
1745 | } |
||
1746 | |||
1747 | } else { |
||
1748 | alert("Geocode ' . get_lang('Error') . ': " + status); |
||
1749 | } |
||
1750 | }); |
||
1751 | } |
||
1752 | } |
||
1753 | </script> |
||
1754 | '); |
||
1755 | $form->addHtml(' |
||
1756 | <div class="form-group"> |
||
1757 | <label for="geolocalization_extra_'.$field_details['variable'].'" class="col-sm-2 control-label"></label> |
||
1758 | <div class="col-sm-8"> |
||
1759 | <button class="null btn btn-default " id="geolocalization_extra_'.$field_details['variable'].'" name="geolocalization_extra_'.$field_details['variable'].'" type="submit"><em class="fa fa-map-marker"></em> '.get_lang('Geolocalization').'</button> |
||
1760 | <button class="null btn btn-default " id="myLocation_extra_'.$field_details['variable'].'" name="myLocation_extra_'.$field_details['variable'].'" type="submit"><em class="fa fa-crosshairs"></em> '.get_lang('MyLocation').'</button> |
||
1761 | </div> |
||
1762 | </div> |
||
1763 | '); |
||
1764 | |||
1765 | $form->addHtml(' |
||
1766 | <div class="form-group"> |
||
1767 | <label for="map_extra_'.$field_details['variable'].'" class="col-sm-2 control-label"> |
||
1768 | '.$field_details['display_text'].' - '.get_lang('Map').' |
||
1769 | </label> |
||
1770 | <div class="col-sm-8"> |
||
1771 | <div name="map_extra_'.$field_details['variable'].'" id="map_extra_'.$field_details['variable'].'" style="width:100%; height:300px;"> |
||
1772 | </div> |
||
1773 | </div> |
||
1774 | </div> |
||
1775 | '); |
||
1776 | break; |
||
1777 | } |
||
1778 | } |
||
1779 | } |
||
1780 | |||
1781 | $return = array(); |
||
1782 | $return['jquery_ready_content'] = $jquery_ready_content; |
||
1783 | |||
1784 | return $return; |
||
1785 | } |
||
1786 | |||
2638 |
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
DateTime
object 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 returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.