public function insertUpdateDocument($data, $isInsert = true) { $db = JFactory::getDbo(); $iconDir = JPATH_ROOT . '/' . JUDownloadFrontHelper::getDirectory('document_icon_directory', 'media/com_judownload/images/document/'); $originalDir = $iconDir . 'original/'; $newMainCatId = $data['main_cat']; $gallery = $data['gallery']; $files = $data['files']; $fieldsData = $data['fieldsData']; $relatedDocs = $data['related_docs']; $data = $data['data']; $messages = array(); if (!$isInsert) { $docObj = JUDownloadHelper::getDocumentById($data['id']); if ($docObj->cat_id != $newMainCatId) { $oldFieldGroup = JUDownloadHelper::getCategoryById($docObj->cat_id); $newFieldGroup = JUDownloadHelper::getCategoryById($newMainCatId); if ($oldFieldGroup->fieldgroup_id != $newFieldGroup->fieldgroup_id) { $query = $db->getQuery(true); $query->select("field.*"); $query->from("#__judownload_fields AS field"); $query->select("plg.folder"); $query->join("", "#__judownload_plugins AS plg ON field.plugin_id = plg.id"); $query->join("", "#__judownload_categories AS c ON (c.fieldgroup_id = field.group_id AND field.group_id != 1)"); $query->join("", "#__judownload_documents_xref AS dxref ON (dxref.cat_id = c.id AND dxref.main = 1)"); $query->join("", "#__judownload_documents AS d ON dxref.doc_id = d.id"); $query->where("d.id = " . $data['id']); $query->group('field.id'); $query->order('field.ordering'); $db->setQuery($query); $fields = $db->loadObjectList(); foreach ($fields as $field) { $fieldObj = JUDownloadFrontHelperField::getField($field, $data['id']); $fieldObj->onDelete(); } } } } $iconPath = ''; $iconFieldId = $this->field_name_id_array['icon']; if (!empty($data['icon'])) { $iconPath = $data['icon']; unset($data['icon']); } if (!empty($fieldsData[$iconFieldId])) { $iconPath = $fieldsData[$iconFieldId]; unset($fieldsData[$iconFieldId]); } $table = JTable::getInstance("Document", "JUDownloadTable"); if (!$table->bind($data) || !$table->check() || !$table->store()) { return array('doc_id' => 0, 'messages' => $table->getErrors()); } $docId = $table->id; $categoriesField = new JUDownloadFieldCore_categories(null, $docId); $categoriesField->is_new_doc = $isInsert; $result = $categoriesField->storeValue($fieldsData[$this->field_name_id_array['cat_id']]); if (!$result) { $table->delete($docId); return array('doc_id' => 0, 'messages' => $db->getErrorMsg()); } $query = $db->getQuery(true); $query->select("field.*"); $query->from("#__judownload_fields AS field"); $query->select("plg.folder"); $query->join("", "#__judownload_plugins AS plg ON field.plugin_id = plg.id"); $query->join("", "#__judownload_categories AS c ON (c.fieldgroup_id = field.group_id OR field.group_id = 1)"); $query->join("", "#__judownload_documents_xref AS dxref ON (dxref.cat_id = c.id AND dxref.main = 1)"); $query->join("", "#__judownload_documents AS d ON dxref.doc_id = d.id"); $query->where("d.id = {$docId}"); $query->where("field.field_name != 'id'"); $query->where("field.field_name != 'cat_id'"); $query->group('field.id'); $query->order('ordering ASC'); $db->setQuery($query); $fields = $db->loadObjectList(); $docObj = JUDownloadHelper::getDocumentById($docId); foreach ($fields as $field) { if (isset($fieldsData[$field->id])) { $fieldObj = JUDownloadFrontHelperField::getField($field, $docObj); $fieldObj->fields_data = $fieldsData; $fieldValue = $fieldsData[$field->id]; $fieldObj->is_new_doc = $isInsert; $fieldValue = $fieldObj->onImportDocument($fieldValue); $fieldObj->storeValue($fieldValue); } } if ($iconPath) { $iconPath = JUDownloadHelper::getPhysicalPath($iconPath); if (!$iconPath) { $messages[] = JText::sprintf('COM_JUDOWNLOAD_CSV_PROCESS_FILE_S_NOT_FOUND', $iconPath); } else { if ($data['id'] > 0) { if ($table->icon) { if (JFile::exists($iconDir . $table->icon)) { JFile::delete($iconDir . $table->icon); JFile::delete($originalDir . $table->icon); } } } $iconName = basename($iconPath); $iconName = $docId . "_" . JUDownloadHelper::fileNameFilter($iconName); if (JFile::copy($iconPath, $originalDir . $iconName) && JUDownloadHelper::renderImages($originalDir . $iconName, $iconDir . $iconName, 'document_icon', true, null, $data['id'])) { $table->icon = $iconName; $table->store(); } } } $imageTable = JTable::getInstance("Image", "JUDownloadTable"); if (!empty($gallery)) { $dir_document_ori = JPATH_SITE . "/" . JUDownloadFrontHelper::getDirectory("document_original_image_directory", "media/com_judownload/images/gallery/original/") . $docId . "/"; $dir_document_small = JPATH_SITE . "/" . JUDownloadFrontHelper::getDirectory("document_small_image_directory", "media/com_judownload/images/gallery/small/") . $docId . "/"; $dir_document_big = JPATH_SITE . "/" . JUDownloadFrontHelper::getDirectory("document_big_image_directory", "media/com_judownload/images/gallery/big/") . $docId . "/"; if (!JFolder::exists($dir_document_ori)) { $file_index = $dir_document_ori . 'index.html'; $buffer = "<!DOCTYPE html><title></title>"; JFile::write($file_index, $buffer); } if (!JFolder::exists($dir_document_small)) { $file_index = $dir_document_small . 'index.html'; $buffer = "<!DOCTYPE html><title></title>"; JFile::write($file_index, $buffer); } if (!JFolder::exists($dir_document_big)) { $file_index = $dir_document_big . 'index.html'; $buffer = "<!DOCTYPE html><title></title>"; JFile::write($file_index, $buffer); } $image_ordering = 1; $date = JFactory::getDate(); foreach ($gallery as $imagePath) { $imagePath = JUDownloadHelper::getPhysicalPath($imagePath); if (!$imagePath) { $messages[] = JText::sprintf('COM_JUDOWNLOAD_CSV_PROCESS_FILE_S_NOT_FOUND', $imagePath); continue; } $imageName = basename($imagePath); $imageName = JUDownloadHelper::generateImageNameByDocument($docId, $imageName); if (JFile::copy($imagePath, $dir_document_ori . $imageName) && JUDownloadHelper::renderImages($dir_document_ori . $imageName, $dir_document_small . $imageName, 'document_small', true, null, $data['id']) && JUDownloadHelper::renderImages($dir_document_ori . $imageName, $dir_document_big . $imageName, 'document_big', true, null, $data['id'])) { $imageObj = new stdClass(); $imageObj->id = 0; $imageObj->file_name = $imageName; $imageObj->doc_id = $docId; $imageObj->ordering = $image_ordering; $imageObj->created = $date->toSql(); $imageObj->published = 1; if (!$imageTable->bind($imageObj) || !$imageTable->check() || !$imageTable->store()) { $messages[] = implode(' | ', $imageTable->getErrors()); JFile::delete($dir_document_ori . $imageName); JFile::delete($dir_document_small . $imageName); JFile::delete($dir_document_big . $imageName); } else { $image_ordering++; } } else { $messages[] = JText::sprintf('COM_JUDOWNLOAD_CSV_PROCESS_CAN_NOT_COPY_FILE_FROM_S_TO_S', $imagePath, $dir_document_ori . $imageName); } } } $fileTable = JTable::getInstance("File", "JUDownloadTable"); if (!empty($files)) { $file_directory = JPATH_SITE . "/" . JUDownloadFrontHelper::getDirectory("file_directory", "media/com_judownload/files/"); if (!JFolder::exists($file_directory . $docId . "/")) { $file_index = $file_directory . $docId . "/index.html"; $buffer = "<!DOCTYPE html><title></title>"; JFile::write($file_index, $buffer); } $file_ordering = 1; foreach ($files as $filePath) { $filePath = JUDownloadHelper::getPhysicalPath($filePath); if (!$filePath) { $messages[] = JText::sprintf('COM_JUDOWNLOAD_CSV_PROCESS_FILE_S_NOT_FOUND', $filePath); continue; } $query = $db->getQuery(true); $query->select('COUNT(*)')->from('#__judownload_files')->where('doc_id = ' . $docId)->where('`rename` = ' . $db->quote(basename($filePath))); $db->setQuery($query); $count = $db->loadResult(); if ($count) { $messages[] = JText::sprintf('COM_JUDOWNLOAD_CSV_PROCESS_EXISTED_FILE_S', $filePath); continue; } $fileObject = array(); $fileObject['id'] = 0; $fileObject['doc_id'] = $docId; $fileObject['ordering'] = $file_ordering; $fileObject['rename'] = basename($filePath); $fileObject['file_name'] = md5($fileObject['rename'] . JUDownloadHelper::generateRandomString(10)) . "." . JFile::getExt($fileObject['rename']); $fileObject['published'] = 1; $this->addFileInfo($fileObject, $filePath, $file_directory); $dest = $file_directory . $docId . "/" . $fileObject['file_name']; if ($fileTable->bind($fileObject) && $fileTable->check() && $fileTable->store()) { if (!JFile::copy($filePath, $dest)) { $fileTable->delete($fileTable->id); $messages[] = JText::sprintf('COM_JUDOWNLOAD_CSV_PROCESS_CAN_NOT_COPY_FILE_FROM_S_TO_S', $filePath, $dest); } $file_ordering++; } else { $messages[] = implode(' | ', $fileTable->getErrors()); } } } if (!empty($relatedDocs)) { $ordering = 1; foreach ($relatedDocs as $relatedDocId) { $document = JUDownloadHelper::getDocumentById($relatedDocId); if ($document) { $relatedDocObj = new stdClass(); $relatedDocObj->id = 0; $relatedDocObj->doc_id = $docId; $relatedDocObj->doc_id_related = $relatedDocId; $relatedDocObj->ordering = $ordering; $query = $db->getQuery(true); $query->select('id')->from('#__judownload_documents_relations')->where('doc_id = ' . $docId)->where('doc_id_related = ' . $relatedDocId); $db->setQuery($query); $existedId = $db->loadResult(); if ($existedId) { $relatedDocObj->id = $existedId; $db->updateObject('#__judownload_documents_relations', $relatedDocObj, 'id'); } else { $db->insertObject('#__judownload_documents_relations', $relatedDocObj); } $relationId = $db->insertid(); if ($relationId > 0) { $ordering++; } else { $messages[] = $db->getErrorMsg(); } } else { $messages[] = JText::sprintf('COM_JUDOWNLOAD_CSV_PROCESS_INVALID_RELATED_DOCUMENT_N', $relatedDocId); } } } return array('doc_id' => $docId, 'messages' => $messages); }