<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class Stories_AdminSettingsController extends Core_Controller_Action_Admin
{

	public function indexAction()
	{

		$db = Engine_Db_Table::getDefaultAdapter();

		$this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('stories_admin_main', array(), 'stories_admin_main_settings');

		// Check ffmpeg path for correctness
		if (function_exists('exec')) {
			$ffmpeg_path = Engine_Api::_()->getApi('settings', 'core')->video_ffmpeg_path;

			$output = null;
			$return = null;
			if (!empty($ffmpeg_path)) {
				exec($ffmpeg_path . ' -version', $output, $return);
			}
			// Try to auto-guess ffmpeg path if it is not set correctly
			$ffmpeg_path_original = $ffmpeg_path;
			if (empty($ffmpeg_path) || $return > 0 || stripos(join('', $output), 'ffmpeg') === false) {
				$ffmpeg_path = null;
				// Windows
				if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
					// @todo
				}
				// Not windows
				else {
					$output = null;
					$return = null;
					@exec('which ffmpeg', $output, $return);
					if (0 == $return) {
						$ffmpeg_path = array_shift($output);
						$output = null;
						$return = null;
						exec($ffmpeg_path . ' -version 2>&1', $output, $return);
						if ($output == null) {
							$ffmpeg_path = null;
						}
					}
				}
			}
			if ($ffmpeg_path != $ffmpeg_path_original) {
				Engine_Api::_()->getApi('settings', 'core')->video_ffmpeg_path = $ffmpeg_path;
			}
		}

		$this->view->form = $form = new Stories_Form_Admin_Settings_Global();

		if ($this->getRequest()->isPost() && $form->isValid($this->_getAllParams())) {
			$values = $form->getValues();
			// Check ffmpeg path
			if (!empty($values['video_ffmpeg_path'])) {
				if (function_exists('exec')) {
					$ffmpeg_path = $values['video_ffmpeg_path'];
					$output = null;
					$return = null;
					exec($ffmpeg_path . ' -version', $output, $return);

					if ($return > 0 && $output != NULL) {
						$form->video_ffmpeg_path->addError('FFMPEG path is not valid or does not exist');
						$values['video_ffmpeg_path'] = '';
					}
				} else {
					$form->video_ffmpeg_path->addError('The exec() function is not available. The ffmpeg path has not been saved.');
					$values['video_ffmpeg_path'] = '';
				}
			}

			include_once APPLICATION_PATH . "/application/modules/Stories/controllers/License.php";
			if (Engine_Api::_()->getApi('settings', 'core')->getSetting('stories.pluginactivated')) {
				foreach ($values as $key => $value) {
					if ($value != '')
						Engine_Api::_()->getApi('settings', 'core')->setSetting($key, $value);
				}
				$form->addNotice('Your changes have been saved.');
				if($error)
					$this->_helper->redirector->gotoRoute(array());
			}
		}
	}

	public function uploadBackgrounds()
	{
		$backgroundTable = Engine_Api::_()->getDbtable('backgrounds', 'stories');
		$PathFile = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'Stories' . DIRECTORY_SEPARATOR . "externals" . DIRECTORY_SEPARATOR . "images" . DIRECTORY_SEPARATOR . "backgrounds" . DIRECTORY_SEPARATOR;

		$file_display = array('jpg', 'jpeg', 'png', 'gif');
		if (file_exists($PathFile)) {
			$dir_contents = scandir($PathFile);
			foreach ($dir_contents as $file) {
				$explode = explode('.', @$file);
				$class = end($explode);
				$file_type = strtolower($class);
				if (($file !== '.') && ($file !== '..') && (engine_in_array($file_type, $file_display))) {
					$images = explode('.', $file);
					//$db = Engine_Db_Table::getDefaultAdapter();
					//$db->beginTransaction();
					// If we're here, we're done
					try {
						$item = $backgroundTable->createRow();
						$values['enabled'] = 1;
						$values['starttime'] = date('Y-m-d');
						$values['enableenddate'] = 1;

						$item->setFromArray($values);
						$item->save();
						$item->order = $item->background_id;
						$item->save();
						if (!empty($file)) {
							$file_ext = pathinfo($file);
							$file_ext = $file_ext['extension'];
							$storage = Engine_Api::_()->getItemTable('storage_file');
							$pngFile = $PathFile . $file;
							$storageObject = $storage->createFile($pngFile, array(
								'parent_id' => $item->getIdentity(),
								'parent_type' => $item->getType(),
								'user_id' => Engine_Api::_()->user()->getViewer()->getIdentity(),
							));
							// Remove temporary file
							////@unlink($file['tmp_name']);
							$item->file_id = $storageObject->file_id;
							$item->save();
						}
						//$db->commit();
					} catch (Exception $e) {
						//$db->rollBack();
						//throw $e;
					}
				}
			}
		}
	}
}
