Improve writing code - refs BT#10258

1.10.x
Angel Fernando Quiroz Campos 9 years ago
parent 49d6dfed77
commit 0d0f74013f
  1. 309
      main/inc/lib/extra_field_value.lib.php

@ -79,11 +79,14 @@ class ExtraFieldValue extends Model
{ {
foreach ($params as $key => $value) { foreach ($params as $key => $value) {
$found = strpos($key, '__persist__'); $found = strpos($key, '__persist__');
if ($found) {
$tempKey = str_replace('__persist__', '', $key); if ($found === FALSE) {
if (!isset($params[$tempKey])) { continue;
$params[$tempKey] = array(); }
}
$tempKey = str_replace('__persist__', '', $key);
if (!isset($params[$tempKey])) {
$params[$tempKey] = array();
} }
} }
@ -94,169 +97,175 @@ class ExtraFieldValue extends Model
$type = $this->getExtraField()->getExtraFieldType(); $type = $this->getExtraField()->getExtraFieldType();
// Parse params. // Parse params.
foreach ($params as $key => $value) { foreach ($params as $key => $value) {
if (substr($key, 0, 6) == 'extra_' || if (
substr($key, 0, 7) == '_extra_' substr($key, 0, 6) != 'extra_' &&
substr($key, 0, 7) != '_extra_'
) { ) {
// An extra field. continue;
$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);
}
$tagValues = is_array($value) ? $value : [$value]; // An extra field.
$tags = []; $field_variable = substr($key, 6);
$extraFieldInfo = $this->getExtraField()->get_handler_field_info_by_field_variable($field_variable);
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);
}
}
foreach ($tags as $tag) { if (!$extraFieldInfo) {
$tag->setCount($tag->getCount() + 1); continue;
$em->persist($tag); }
$fieldRelTag = new Chamilo\CoreBundle\Entity\ExtraFieldRelTag(); $commentVariable = 'extra_'.$field_variable.'_comment';
$fieldRelTag->setFieldId($extraFieldInfo['id']); $comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null;
$fieldRelTag->setItemId($params['item_id']);
$fieldRelTag->setTagId($tag->getId());
$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(); $em = Database::getManager();
}
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;
}
$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)) { foreach ($currentTags as $extraFieldtag) {
mkdir($fileDir, $dirPermissions, true); $em->remove($extraFieldtag);
} }
if ($value['error'] == 0) { $tagValues = is_array($value) ? $value : [$value];
$imageExtraField = new Image($value['tmp_name']); $tags = [];
$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) { foreach ($tagValues as $tagValue) {
case 'course': $tagsResult = $em
$fileDir = api_get_path(SYS_UPLOAD_PATH)."courses/"; ->getRepository('ChamiloCoreBundle:Tag')
$fileDirStored = "courses/"; ->findBy([
break; 'tag' => $tagValue,
case 'session': 'fieldId' => $extraFieldInfo['id']
$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']); if (empty($tagsResult)) {
$fileName = ExtraField::FIELD_TYPE_FILE . "_{$params['item_id']}_$cleanedName"; $tag = new \Chamilo\CoreBundle\Entity\Tag();
if (!file_exists($fileDir)) { $tag->setCount(0);
mkdir($fileDir, $dirPermissions, true); $tag->setFieldId($extraFieldInfo['id']);
} $tag->setTag($tagValue);
if ($value['error'] == 0) { $tags[] = $tag;
moveUploadedFile($value, $fileDir . $fileName); } else {
$tags = array_merge($tags, $tagsResult);
}
}
$new_params = array( foreach ($tags as $tag) {
'item_id' => $params['item_id'], $tag->setCount($tag->getCount() + 1);
'field_id' => $extraFieldInfo['id'], $em->persist($tag);
'value' => $fileDirStored . $fileName
);
if ($this->type !== 'session' && $this->type !== 'course') { $fieldRelTag = new Chamilo\CoreBundle\Entity\ExtraFieldRelTag();
$new_params['comment'] = $comment; $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; break;
default: case 'session':
$newParams = array( $fileDir = api_get_path(SYS_UPLOAD_PATH)."sessions/";
'item_id' => $params['item_id'], $fileDirStored = "sessions/";
'field_id' => $extraFieldInfo['id'], break;
'value' => $value, case 'user':
'comment' => $comment $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);
} }
} }
} }

Loading…
Cancel
Save