55use Chamilo \CoreBundle \Entity \ExtraFieldOptions ;
66use Chamilo \CoreBundle \Framework \Container ;
77use Chamilo \CoreBundle \Component \Utils \ActionIcon ;
8+ use Chamilo \CoreBundle \Repository \ExtraFieldOptionsRepository ;
89
910/**
1011 * Handles the extra fields for various objects (users, sessions, courses).
@@ -25,6 +26,9 @@ class ExtraFieldOption extends Model
2526 public $ extraField ;
2627 public $ fieldId ;
2728
29+ /** @var ExtraFieldOptionsRepository */
30+ public $ repo ;
31+
2832 /**
2933 * Gets the table for the type of object for which we are using an extra field.
3034 *
@@ -38,6 +42,7 @@ public function __construct($type)
3842 $ this ->extraField = $ extraField ;
3943 $ this ->table = Database::get_main_table (TABLE_EXTRA_FIELD_OPTIONS );
4044 $ this ->tableExtraField = Database::get_main_table (TABLE_EXTRA_FIELD );
45+ $ this ->repo = Database::getManager ()->getRepository (ExtraFieldOptions::class);
4146 }
4247
4348 /**
@@ -128,12 +133,13 @@ public function delete_all_options_by_field_id($field_id)
128133 */
129134 public function saveOptions ($ params , $ showQuery = false )
130135 {
131- $ optionInfo = $ this ->get_field_option_by_field_and_option (
132- $ params ['field_id ' ],
133- $ params ['option_value ' ]
136+ $ optionInfo = $ this ->repo ->getFieldOptionByFieldAndOption (
137+ (int ) $ params ['field_id ' ],
138+ (string ) $ params ['option_value ' ],
139+ $ this ->extraField ->getItemType ()
134140 );
135141
136- if (false == $ optionInfo ) {
142+ if (! $ optionInfo ) {
137143 $ optionValue = api_replace_dangerous_char ($ params ['option_value ' ]);
138144 $ order = $ this ->get_max_order ($ params ['field_id ' ]);
139145 $ newParams = [
@@ -327,13 +333,17 @@ public function save($params, $showQuery = false)
327333 $ list = explode ('; ' , $ params ['field_options ' ]);
328334
329335 foreach ($ list as $ option ) {
330- $ option_info = $ this ->get_field_option_by_field_and_option ($ field_id , $ option );
336+ $ option_info = $ this ->repo ->getFieldOptionByFieldAndOption (
337+ $ field_id ,
338+ $ option ,
339+ $ this ->extraField ->getItemType ()
340+ );
331341
332342 // Use URLify only for new items
333343 $ optionValue = api_replace_dangerous_char ($ option );
334344 $ option = trim ($ option );
335345
336- if (false != $ option_info ) {
346+ if ($ option_info ) {
337347 continue ;
338348 }
339349
@@ -366,7 +376,7 @@ public function save($params, $showQuery = false)
366376 */
367377 public function save_one_item ($ params , $ show_query = false , $ insert_repeated = true )
368378 {
369- $ field_id = intval ( $ params ['field_id ' ]) ;
379+ $ field_id = ( int ) $ params ['field_id ' ];
370380 if (empty ($ field_id )) {
371381 return false ;
372382 }
@@ -391,49 +401,19 @@ public function save_one_item($params, $show_query = false, $insert_repeated = t
391401 if ($ insert_repeated ) {
392402 parent ::save ($ params , $ show_query );
393403 } else {
394- $ check = $ this ->get_field_option_by_field_and_option (
404+ $ check = $ this ->repo -> getFieldOptionByFieldAndOption (
395405 $ field_id ,
396- $ params ['option_value ' ]
406+ $ params ['option_value ' ],
407+ $ this ->extraField ->getItemType ()
397408 );
398- if (false == $ check ) {
409+ if (! $ check ) {
399410 parent ::save ($ params , $ show_query );
400411 }
401412 }
402413
403414 return true ;
404415 }
405416
406- /**
407- * Get the complete row of a specific option of a specific field.
408- *
409- * @param int $field_id
410- * @param string $option_value Value of the option
411- *
412- * @return mixed The row on success or false on failure
413- * @assert (0,'') === false
414- */
415- public function get_field_option_by_field_and_option ($ field_id , $ option_value )
416- {
417- $ field_id = (int ) $ field_id ;
418- $ option_value = Database::escape_string ($ option_value );
419- $ extraFieldType = $ this ->getExtraField ()->getItemType ();
420-
421- $ sql = "SELECT s.* FROM {$ this ->table } s
422- INNER JOIN {$ this ->tableExtraField } sf
423- ON (s.field_id = sf.id)
424- WHERE
425- field_id = $ field_id AND
426- option_value = ' " .$ option_value ."' AND
427- sf.item_type = $ extraFieldType
428- " ;
429- $ result = Database::query ($ sql );
430- if (Database::num_rows ($ result ) > 0 ) {
431- return Database::store_result ($ result , 'ASSOC ' );
432- }
433-
434- return false ;
435- }
436-
437417 /**
438418 * Get the complete row of a specific option's display text of a specific field.
439419 *
0 commit comments