<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>
<script type="application/javascript">
	//Ajax error show before form submit
	var error = false;
	var objectError;
	var counter = 0;

	function validateForm(id) {

		var errorPresent = false;
		scriptJquery('.core_formcheck input, .core_formcheck select, .core_formcheck checkbox, .core_formcheck textarea, .core_formcheck radio').each(
			function(index) {
				var input = scriptJquery(this);

				if (scriptJquery(this).closest('div').parent().css('display') != 'none' && scriptJquery(this).closest('div').parent().find('.form-label').find('label').first().hasClass('required') && scriptJquery(this).prop('type') != 'hidden' && scriptJquery(this).closest('div').parent().attr('class') != 'form-elements') {
					if (scriptJquery(this).prop('type') == 'checkbox') {
						value = '';
						if (scriptJquery('input[name="' + scriptJquery(this).attr('name') + '"]:checked').length > 0) {
							value = 1;
						};
						if (value == '')
							error = true;
						else
							error = false;
					} else if (scriptJquery(this).prop('type') == 'select-multiple') {
						if (scriptJquery(this).val() === '' || scriptJquery(this).val() == null)
							error = true;
						else
							error = false;
					} else if (scriptJquery(this).prop('type') == 'select-one' || scriptJquery(this).prop('type') == 'select') {
						if (scriptJquery(this).val() === '')
							error = true;
						else
							error = false;
					} else if (scriptJquery(this).prop('type') == 'radio') {
						if (scriptJquery("input[name='" + scriptJquery(this).attr('name').replace('[]', '') + "']:checked").val() === '')
							error = true;
						else
							error = false;
					} else if (scriptJquery(this).prop('type') == 'textarea' && scriptJquery(this).prop('id') == 'body') {
						if (tinyMCE.get('body').getContent() === '' || tinyMCE.get('body').getContent() == null)
							error = true;
						else
							error = false;
					} else if (scriptJquery(this).prop('type') == 'textarea') {
						if (scriptJquery(this).val() === '' || scriptJquery(this).val() == null)
							error = true;
						else
							error = false;
					} else {
						if (scriptJquery(this).val() === '' || scriptJquery(this).val() == null)
							error = true;
						else
							error = false;
					}
					if (error)
						errorPresent = true;
					error = false;
				}
			}
		);
		return errorPresent;
	}

	AttachEventListerSE('submit', '#file_rename', function(e) {
		e.preventDefault();
		renameFile(this);
	});

	function renameFile(formObject) {

		var validationFm = validateForm();
		var name = scriptJquery(formObject).find('#name').val();
    if(name == '') {
			if(scriptJquery('#error_text'))
				scriptJquery('#error_text').remove();
			scriptJquery('#name-element').append('<span id="error_text" class="text-danger">Title is requried field.</span>');
      return false;
    }

		if (validationFm) {

			var input = scriptJquery(formObject);
			alert('<?php echo $this->string()->escapeJavascript("Please fill the red mark fields"); ?>');
			if (typeof objectError != 'undefined') {
				var errorFirstObject = scriptJquery(objectError).parent().parent();
				scriptJquery('html, body').animate({
					scrollTop: errorFirstObject.offset().top
				}, 2000);
			}
			return false;
		} else {
			submitForm(formObject);
		}
	}

	function submitForm(formObject) {

		scriptJquery('#core_file_overlay').show();
		var formData = new FormData(formObject);
		formData.append('is_ajax', 1);
		formData.append('file_id', <?php echo $this->file_id; ?>);
		scriptJquery.ajax({
			url: "admin/files/rename/",
			type: "POST",
			contentType: false,
			processData: false,
			cache: false,
			data: formData,
			success: function(response) {

				var result = scriptJquery.parseJSON(response);
				if (result.status == 1) {

					scriptJquery('#core_file_overlay').hide();
					scriptJquery('#ajaxsmoothbox_container').html("<div class='h-100 d-flex justify-content-center align-items-center' style='padding:20px;'><div id='success_message' class='success_message d-flex gap-3 align-items-center'><i class='fa fa-check-circle'></i><span>Your file information is successfully saved.</span></div></div>");

					setTimeout(function() {
						ajaxsmoothboxclose();
						location.reload();
					}, 1000);
				}
			}
		});
	}
</script>
<?php $storage = Engine_Api::_()->getItem('storage_file', $this->file->storage_file_id);
	$path = $storage->map();
	$copyPath = ($storage->service_id == 2) ?  $path : $path; 
	?>
<div class="attachment_details_popup">
	<div class="attachment_details_popup_header">
		<?php echo $this->translate("Attachment Details"); ?>
	</div>

	<?php if (empty($this->is_ajax)) { ?>
			<form id="file_rename" name="file_rename" enctype="application/x-www-form-urlencoded" class="core_formcheck" action="<?php echo Zend_Controller_Front::getInstance()->getRouter()->assemble(array()); ?>" method="post">
				<?php $storage = Engine_Api::_()->getItem('storage_file', $this->file->storage_file_id); ?>
				<div class="attachment_details_popup_main">
					<div class="attachment_details_popup_preview">
						<div>
							<?php if($this->file->extension == 'pdf'): ?>
								<img src="application/modules/Core/externals/images/admin/file-icons/pdf.png" alt="<?php echo $this->file->alt_text; ?>">
							<?php elseif(engine_in_array($this->file->extension, array('text', 'txt'))): ?>
								<img src="application/modules/Core/externals/images/admin/file-icons/text.png" alt="<?php echo $this->file->alt_text; ?>">
							<?php elseif(engine_in_array($this->file->extension, array('mpeg', 'x-realaudio', 'wav', 'amr', 'mp3', 'ogg','midi','x-ms-wma', 'x-ms-wax', 'x-matroska'))): ?>
								<img src="application/modules/Core/externals/images/admin/file-icons/audio.png" alt="<?php echo $this->file->alt_text; ?>">
							<?php elseif(engine_in_array($this->file->extension, array('mp4', 'flv'))): ?>
								<video width="320" height="240" controls>
									<source src="<?php echo $path; ?>" type="video/mp4">
									<?php echo $this->translate("Your browser does not support the video tag."); ?>
								</video>
							<?php elseif(engine_in_array($this->file->extension, array('zip', 'tar'))): ?>
								<img src="application/modules/Core/externals/images/admin/file-icons/zip.png" alt="<?php echo $this->file->alt_text; ?>">
							<?php elseif(engine_in_array($this->file->extension, array('jpeg', 'jpg', 'gif', 'png', 'tiff','webp'))): ?>
								<img src="<?php echo $path; ?>" alt="<?php echo $this->file->alt_text; ?>" />
							<?php else: ?>
								<img src="application/modules/Core/externals/images/admin/file-icons/default.png" alt="<?php echo $this->file->alt_text; ?>">
							<?php endif; ?>
						</div>
					</div>
					<div class="attachment_details_popup_info">
						<div class="_body">
							<div id="name-wrapper" class="attachment_details_popup_info_field">
								<div id="name-label" class="_label"><label for="name" class="required"><?php echo $this->translate("Title"); ?></label></div>
								<div id="name-element" class="_element">
									<input type="text" name="name" id="name" value="<?php echo $this->file->name ?? '' ; ?>">
								</div>
							</div>
							<?php if(engine_in_array($this->file->extension, array('jpeg', 'jpg', 'gif', 'png', 'tiff','webp'))): ?>
							<div id="alt_text-wrapper" class="attachment_details_popup_info_field">
								<div id="alt_text-label" class="_label"><label for="alt_text" class="optional"><?php echo $this->translate("Alternative Text"); ?></label></div>
								<div id="alt_text-element" class="_element">
									<input type="text" name="alt_text" id="alt_text" value="<?php echo $this->file->alt_text ?? '' ; ?>">
								</div>
							</div>
							<?php endif; ?>
							<div id="storage_path-wrapper" class="attachment_details_popup_info_field">
								<div id="storage_path-label" class="_label"><label for="storage_path" class="optional"><?php echo $this->translate("File URL"); ?></label></div>
								<div id="storage_path-element" class="_element">
									<input type="text" name="storage_file_path" id="storage_file_path" value="<?php echo $path ?? '' ; ?>" readonly="readonly">
								</div>
							</div>
							<div class="attachment_details_popup_info_details">
								<div><span><?php echo $this->translate("Uploaded on:"); ?></span><span><?php echo $this->locale()->toDateTime($this->file->creation_date); ?></span></div>
								<div><span><?php echo $this->translate("Modified on:"); ?></span><span><?php echo $this->locale()->toDateTime($this->file->modified_date); ?></span></div>
								<div><span><?php echo $this->translate("File Type:"); ?></span><span><?php echo $this->translate("%s/%s", $storage->mime_major, $storage->mime_minor); ?></span></div>
								<div><span><?php echo $this->translate("File Size:"); ?></span><span><?php echo round($storage->size / (1024 * 1024), 2)?> <?php echo $this->translate(" MB"); ?></span></div>
								<?php if(engine_in_array($this->file->extension, array('jpeg', 'jpg', 'gif', 'png', 'tiff','webp'))): ?>
								<div><span><?php echo $this->translate("Dimensions:"); ?></span><span><?php echo $storage->height.'*'.$storage->width; ?></span></div>
								<?php endif; ?>
							</div>
						</div>
						<div class="_footer">
							<div>
								<a href="javascript:;" onclick="javascript:ajaxsmoothboxclose();"><?php echo $this->translate("Cancel"); ?></a>
							</div>
							<div>
								<button name="submit" id="submit" type="submit"><?php echo $this->translate("Save"); ?></button>
							</div>
						</div>
					</div>
				</div>
			</form>
	<?php } ?>
</div>
<style>
.attachment_details_popup > form{
	height:calc(100vh - 70px);
}
</style>
<?php die; ?>