diff --git a/main/inc/lib/extra_field_value.lib.php b/main/inc/lib/extra_field_value.lib.php index 9950369e2d..34b82010eb 100755 --- a/main/inc/lib/extra_field_value.lib.php +++ b/main/inc/lib/extra_field_value.lib.php @@ -79,11 +79,14 @@ class ExtraFieldValue extends Model { foreach ($params as $key => $value) { $found = strpos($key, '__persist__'); - if ($found) { - $tempKey = str_replace('__persist__', '', $key); - if (!isset($params[$tempKey])) { - $params[$tempKey] = array(); - } + + if ($found === FALSE) { + continue; + } + + $tempKey = str_replace('__persist__', '', $key); + if (!isset($params[$tempKey])) { + $params[$tempKey] = array(); } } @@ -94,169 +97,175 @@ class ExtraFieldValue extends Model $type = $this->getExtraField()->getExtraFieldType(); // Parse params. foreach ($params as $key => $value) { - if (substr($key, 0, 6) == 'extra_' || - substr($key, 0, 7) == '_extra_' + if ( + substr($key, 0, 6) != 'extra_' && + substr($key, 0, 7) != '_extra_' ) { - // An extra field. - $field_variable = substr($key, 6); - $extraFieldInfo = $this->getExtraField()->get_handler_field_info_by_field_variable($field_variable); - - if ($extraFieldInfo) { - $commentVariable = 'extra_'.$field_variable.'_comment'; - $comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null; - - switch ($extraFieldInfo['field_type']) { - - case ExtraField::FIELD_TYPE_TAG: - if ($type == EntityExtraField::USER_FIELD_TYPE) { - - UserManager::delete_user_tags( - $params['item_id'], - $extraFieldInfo['id'] - ); - - UserManager::process_tags( - $value, - $params['item_id'], - $extraFieldInfo['id'] - ); - } else { - $em = Database::getManager(); - - $currentTags = $em - ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag') - ->findBy([ - 'fieldId' => $extraFieldInfo['id'], - 'itemId' => $params['item_id'] - ]); - - foreach ($currentTags as $extraFieldtag) { - $em->remove($extraFieldtag); - } + continue; + } - $tagValues = is_array($value) ? $value : [$value]; - $tags = []; - - foreach ($tagValues as $tagValue) { - $tagsResult = $em->getRepository('ChamiloCoreBundle:Tag')->findBy([ - 'tag' => $tagValue, - 'fieldId' => $extraFieldInfo['id'] - ]); - - if (empty($tagsResult)) { - $tag = new \Chamilo\CoreBundle\Entity\Tag(); - $tag->setCount(0); - $tag->setFieldId($extraFieldInfo['id']); - $tag->setTag($tagValue); - - $tags[] = $tag; - } else { - $tags = array_merge($tags, $tagsResult); - } - } + // An extra field. + $field_variable = substr($key, 6); + $extraFieldInfo = $this->getExtraField()->get_handler_field_info_by_field_variable($field_variable); - foreach ($tags as $tag) { - $tag->setCount($tag->getCount() + 1); - $em->persist($tag); + if (!$extraFieldInfo) { + continue; + } - $fieldRelTag = new Chamilo\CoreBundle\Entity\ExtraFieldRelTag(); - $fieldRelTag->setFieldId($extraFieldInfo['id']); - $fieldRelTag->setItemId($params['item_id']); - $fieldRelTag->setTagId($tag->getId()); + $commentVariable = 'extra_'.$field_variable.'_comment'; + $comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null; - $em->persist($fieldRelTag); - } + switch ($extraFieldInfo['field_type']) { + case ExtraField::FIELD_TYPE_TAG: + if ($type == EntityExtraField::USER_FIELD_TYPE) { + UserManager::delete_user_tags( + $params['item_id'], + $extraFieldInfo['id'] + ); + + UserManager::process_tags( + $value, + $params['item_id'], + $extraFieldInfo['id'] + ); + break; + } - $em->flush(); - } - break; - case ExtraField::FIELD_TYPE_FILE_IMAGE: - $dirPermissions = api_get_permissions_for_new_directories(); - switch ($this->type) { - case 'course': - $fileDir = api_get_path(SYS_UPLOAD_PATH)."courses/"; - $fileDirStored = "courses/"; - break; - case 'session': - $fileDir = api_get_path(SYS_UPLOAD_PATH)."sessions/"; - $fileDirStored = "sessions/"; - break; - case 'user': - $fileDir = UserManager::getUserPathById($params['item_id'], 'system'); - $fileDirStored = UserManager::getUserPathById($params['item_id'], 'last'); - break; - } + $em = Database::getManager(); - $fileName = ExtraField::FIELD_TYPE_FILE_IMAGE . "_{$params['item_id']}.png"; + $currentTags = $em + ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag') + ->findBy([ + 'fieldId' => $extraFieldInfo['id'], + 'itemId' => $params['item_id'] + ]); - if (!file_exists($fileDir)) { - mkdir($fileDir, $dirPermissions, true); - } + foreach ($currentTags as $extraFieldtag) { + $em->remove($extraFieldtag); + } - if ($value['error'] == 0) { - $imageExtraField = new Image($value['tmp_name']); - $imageExtraField->send_image($fileDir . $fileName, -1, 'png'); - $newParams = array( - 'item_id' => $params['item_id'], - 'field_id' => $extraFieldInfo['id'], - 'value' => $fileDirStored . $fileName, - 'comment' => $comment - ); - - self::save($newParams); - } - break; - case ExtraField::FIELD_TYPE_FILE: - $dirPermissions = api_get_permissions_for_new_directories(); + $tagValues = is_array($value) ? $value : [$value]; + $tags = []; - switch ($this->type) { - case 'course': - $fileDir = api_get_path(SYS_UPLOAD_PATH)."courses/"; - $fileDirStored = "courses/"; - break; - case 'session': - $fileDir = api_get_path(SYS_UPLOAD_PATH)."sessions/"; - $fileDirStored = "sessions/"; - break; - case 'user': - $fileDir = UserManager::getUserPathById($params['item_id'], 'system'); - $fileDirStored = UserManager::getUserPathById($params['item_id'], 'last'); - break; - } + foreach ($tagValues as $tagValue) { + $tagsResult = $em + ->getRepository('ChamiloCoreBundle:Tag') + ->findBy([ + 'tag' => $tagValue, + 'fieldId' => $extraFieldInfo['id'] + ]); - $cleanedName = api_replace_dangerous_char($value['name']); - $fileName = ExtraField::FIELD_TYPE_FILE . "_{$params['item_id']}_$cleanedName"; - if (!file_exists($fileDir)) { - mkdir($fileDir, $dirPermissions, true); - } + if (empty($tagsResult)) { + $tag = new \Chamilo\CoreBundle\Entity\Tag(); + $tag->setCount(0); + $tag->setFieldId($extraFieldInfo['id']); + $tag->setTag($tagValue); - if ($value['error'] == 0) { - moveUploadedFile($value, $fileDir . $fileName); + $tags[] = $tag; + } else { + $tags = array_merge($tags, $tagsResult); + } + } - $new_params = array( - 'item_id' => $params['item_id'], - 'field_id' => $extraFieldInfo['id'], - 'value' => $fileDirStored . $fileName - ); + foreach ($tags as $tag) { + $tag->setCount($tag->getCount() + 1); + $em->persist($tag); - if ($this->type !== 'session' && $this->type !== 'course') { - $new_params['comment'] = $comment; - } + $fieldRelTag = new Chamilo\CoreBundle\Entity\ExtraFieldRelTag(); + $fieldRelTag->setFieldId($extraFieldInfo['id']); + $fieldRelTag->setItemId($params['item_id']); + $fieldRelTag->setTagId($tag->getId()); - self::save($new_params); - } + $em->persist($fieldRelTag); + } + + $em->flush(); + break; + case ExtraField::FIELD_TYPE_FILE_IMAGE: + $dirPermissions = api_get_permissions_for_new_directories(); + switch ($this->type) { + case 'course': + $fileDir = api_get_path(SYS_UPLOAD_PATH)."courses/"; + $fileDirStored = "courses/"; break; - default: - $newParams = array( - 'item_id' => $params['item_id'], - 'field_id' => $extraFieldInfo['id'], - 'value' => $value, - 'comment' => $comment - ); + case 'session': + $fileDir = api_get_path(SYS_UPLOAD_PATH)."sessions/"; + $fileDirStored = "sessions/"; + break; + case 'user': + $fileDir = UserManager::getUserPathById($params['item_id'], 'system'); + $fileDirStored = UserManager::getUserPathById($params['item_id'], 'last'); + break; + } + + $fileName = ExtraField::FIELD_TYPE_FILE_IMAGE . "_{$params['item_id']}.png"; - self::save($newParams); + if (!file_exists($fileDir)) { + mkdir($fileDir, $dirPermissions, true); } - } + + if ($value['error'] == 0) { + $imageExtraField = new Image($value['tmp_name']); + $imageExtraField->send_image($fileDir . $fileName, -1, 'png'); + $newParams = array( + 'item_id' => $params['item_id'], + 'field_id' => $extraFieldInfo['id'], + 'value' => $fileDirStored . $fileName, + 'comment' => $comment + ); + + self::save($newParams); + } + break; + case ExtraField::FIELD_TYPE_FILE: + $dirPermissions = api_get_permissions_for_new_directories(); + + switch ($this->type) { + case 'course': + $fileDir = api_get_path(SYS_UPLOAD_PATH)."courses/"; + $fileDirStored = "courses/"; + break; + case 'session': + $fileDir = api_get_path(SYS_UPLOAD_PATH)."sessions/"; + $fileDirStored = "sessions/"; + break; + case 'user': + $fileDir = UserManager::getUserPathById($params['item_id'], 'system'); + $fileDirStored = UserManager::getUserPathById($params['item_id'], 'last'); + break; + } + + $cleanedName = api_replace_dangerous_char($value['name']); + $fileName = ExtraField::FIELD_TYPE_FILE . "_{$params['item_id']}_$cleanedName"; + if (!file_exists($fileDir)) { + mkdir($fileDir, $dirPermissions, true); + } + + if ($value['error'] == 0) { + moveUploadedFile($value, $fileDir . $fileName); + + $new_params = array( + 'item_id' => $params['item_id'], + 'field_id' => $extraFieldInfo['id'], + 'value' => $fileDirStored . $fileName + ); + + if ($this->type !== 'session' && $this->type !== 'course') { + $new_params['comment'] = $comment; + } + + self::save($new_params); + } + break; + default: + $newParams = array( + 'item_id' => $params['item_id'], + 'field_id' => $extraFieldInfo['id'], + 'value' => $value, + 'comment' => $comment + ); + + self::save($newParams); } } }