ACC SHELL

Path : /srv/www/vhosts/alfa-com/engine/
File Upload :
Current File : /srv/www/vhosts/alfa-com/engine/View.php

<?php
	// Author: Jakub Macek, CZ; Copyright: Poski.com s.r.o.; Code is 100% my work. Do not copy.
	
	class View
	{
		private static $current = null;
		
		public static function current()
		{
			if (self::$current === null)
				self::$current = new View();
			return self::$current;
		}

		public $templates = array();

		public function getTemplate($id)
		{
			if (isset($this->templates[$id]))
				return $this->templates[$id];
			else
				return null;
		}
		
		public function getTemplateOrCreate($id, $data, $engine = null, $source = null, $type = 0)
		{
			if (!isset($this->templates[$id]))
				$this->templates[$id] = new Template($id, $data, $engine, $source, $type);
			return $this->templates[$id];
		}
		
		public function getTemplateOfType($type)
		{
			foreach ($this->templates as $block)
				if ($block->type & $type)
					return $block;
			return null;
		}
		
		public function getTemplateOfTypeOrCreate($type, $id, $data, $engine = null, $source = null)
		{
			foreach ($this->templates as $template)
				if ($template->type & $type)
					return $template;
			return ($this->templates[$id] = new Template($id, $data, $engine, $source, $type));
		}
		
		public function getTemplatesOfType($type)
		{
			$result = array();
			foreach ($this->templates as $template)
				if ($template->type & $type)
					$result[$template->id] = $template;
			return $result;
		}
		
		public function removeTemplate($id)
		{
			unset($this->templates[$id]);
		}
		
		public function removeTemplatesOfType($type)
		{
			$ids = array_keys($this->templates);
			foreach ($ids as $id)
				if ($this->templates[$id]->type & $type)
					unset($this->templates[$id]);
		}
		
		public function render()
		{
			return $this->getTemplateOfType(Template::TYPE_DEFAULT)->render();
		}
		
		public function __toString()
		{
			return $this->render();
		}
		
		public static function frame($file = null)
		{
			view()->templates[0] = new Template(0, array('CoreTemplates', 'html'), 'callback', 'php', Template::TYPE_DEFAULT | Template::TYPE_HTML);
			if (is_string($file))
				view()->templates['main'] = new Template('main', $file, null, null, Template::TYPE_MAIN);
			else if (is_object($file))
				view()->templates['main'] = $file;

			if (!view()->getTemplateOfType(Template::TYPE_FRAME))
			{
				$frame_prefix = 'web/';
				foreach (site()->base as $base)
					foreach (array(L.'/', '') as $locale_part)
						if (is_file($base . $frame_prefix . $locale_part . '@frame.php'))
						{
							view()->templates['frame'] = new Template('frame', $frame_prefix . $locale_part . '@frame.php', 'file', 'php', Template::TYPE_FRAME);
							break 2;
						}
			}
		}

		public static function title($part = '')
		{
			$prepared = page()->get('titlePrepared', false);
			if (!$prepared)
				foreach ($GLOBALS['modules'] as $module)
					if (!$prepared)
						$prepared = $prepared || $module->page();
			page()->set('titlePrepared', $prepared);

			$breadcrumbs = $temp = page()->breadcrumbs;
			$group = null; $title = null;
			foreach ($temp as $temp2 => $temp1)
				if (($group === null) && !isset($temp1['no-title']))
				{
					$group = $temp1['text'];
					unset($temp[$temp2]);
				}
			foreach (array_reverse($temp) as $temp2 => $temp1)
				if (($title === null) && !isset($temp1['no-title']))
					$title = $temp1['text'];

			page()->title = $title . ($title ? page()->get('titleSeparator') : '') . $group;

			$result = '';
			if ($part == 'breadcrumbs-text')
			{
				$result = array();
				foreach ($breadcrumbs as $i => $breadcrumb)
					$result[] = HTML::e($breadcrumb['text']);
				$result = '<span class="breadcrumbs-text">' . implode(page()->breadcrumbSeparator, $result) . '</span>';
			}
			else if (($part == 'breadcrumbs') || ($part == 'breadcrumbs-header'))
			{
				$result = array();
				foreach ($breadcrumbs as $i => $breadcrumb)
				{
					$class = 'level' . $i;
					if ($i == 0)
						$class .= ' first';
					if ($i == (count($breadcrumbs) - 1))
						$class .= ' last';
					if ($breadcrumb['url'])
						$result[] = '<a class="' . $class . '" href="' . HTML::e($breadcrumb['url']) . '">' . HTML::e($breadcrumb['text']) . '</a>';
					else
						$result[] = '<span class="' . $class . '">' . HTML::e($breadcrumb['text']) . '</span>';
				}
				$result = implode(page()->breadcrumbSeparator, $result);
				if ($part == 'breadcrumbs-header')
					$result = '<h2>' . $result . '</h2>';
				else
					$result = '<span class="breadcrumbs">' . $result . '</span>';
			}
			else
			{
				$title = HTML::e($title);
				$group = HTML::e($group);
				$style_with_modifiers = page()->get('titleStyle');
				$sep = ($title ? page()->breadcrumbSeparator : '');
				$style = trim($style_with_modifiers, '=0');
				switch ($style)
				{
					case 'none':
						break;
					case 'title':
						$result .= "<h2>$title</h2>";
						break;
					case 'separate':
						$result .= "<h2>$group</h2>";
						$result .= "<h3>$title</h3>";
						break;
					case 'plain':
						$result .= "$group$sep$title";
						break;
					case 'strong-span':
						$result .= "<h2><strong>$group</strong><span>$sep$title</span></h2>";
						break;
					case 'span-strong':
						$result .= "<h2><span><strong>$group$sep</strong>$title</span></h2>";
						break;
					case 'sprintf':
						$result .= sprintf(page()->get('titleTemplate'), $group, $title, $sep);
						break;
					case 'pseudoSmarty':
						$result .= Template::pseudoSmarty(page()->get('titleTemplate'), array(
								'group' => $group,
								'title' => $title,
								'separator' => $sep,
								'group|urlize' => U::urlize($group),
								'title|urlize' => U::urlize($title),
							)); 
						break;
					case 'default':
					default:
						$result .= "<h2>$group$sep$title</h2>";
						break;
				}
				if (strpos($style_with_modifiers, '=') === false)
					$result = '<div class="h2">' . $result . '</div>';
				page()->h2 = $result;
				if (strpos($style_with_modifiers, '0') !== false)
					$result = '';
			}
			return $result;
		}

		public static function common($part)
		{
			$args = func_get_args(); array_shift($args);
			$RESULT = '';
			if ($part == 'messages')
			{
				if ($invocation = i())
					foreach ($invocation->messages as $message)
						$RESULT .= '<div class="'.$message['type'].'">'.$message['text'].'</div>';
			}
			else if ($part == 'output')
			{
				if ($invocation = i())
					foreach (view()->getTemplatesOfType(Template::TYPE_INVOCATION) as $template)
					{
						$RESULT .= '<div class="module_action module_'.$invocation->module()->id.' action_'.$invocation->action()->id.' module_'.$invocation->module()->id.'_action_'.$invocation->action()->id.'">' . "\n";
						$RESULT .= $template->render();
						$RESULT .= "\n" . '</div>';				
					}
			}
			else if ($part == 'messages-output')
			{
				$RESULT .= '<div class="messages"' . (isset($args[0]) ? ' id="messages"' : '') . '>';
				$RESULT .= self::common('messages');
				$RESULT .= '</div>';
				$RESULT .= self::common('output');
			}
			else if ($part == 'messages-output-0')
			{
				if ($invocation = i())
					foreach ($invocation->messages as $message)
						$RESULT .= $message['type'] . ': ' . $message['text'] . "\n";
				foreach (view()->getTemplatesOfType(Template::TYPE_INVOCATION) as $template)
					$RESULT .= $template->render();
			}
			else if ($part == 'pages')
			{
				$pager = $args[0];
				if ($pager && ($links = $pager->getLinks()) && $links['all'])
				{
					$RESULT .= '<div class="pages pages_' . (isset($args[1]) ? $args[1] : '') . '">';
					$RESULT .= $pager->selectBox;
					$RESULT .= $links['all'];
					$RESULT .= '</div>';
				}
			}
			else if ($part == 'thumbnail')
			{
				$image = (is_object($args[0]) ? $args[0]->image : $args[0]);
				$text = @$args[1];
				$url = @$args[2];
				$suffix = @$args[3];
				if ($suffix === null)
					$suffix = '.thumb';
				$fallback = @$args[4];
				$lightbox = (string) @$args[5];
				$counter = (string) @$args[6];
				if (is_object($args[0]) && ($text === null) && isset($args[0]->title))
					$text = $args[0]->title;
				if (is_object($args[0]) && ($text === null))
					$text = $args[0]->otitle;
				if (($url !== false) && is_object($args[0]) && isset($args[0]->url))
					$url .= $args[0]->url;
				if (is_object($args[0]) && ($lightbox === null))
					$lightbox = '[' . md5($args[0]->oid) . ']';
				else if ($lightbox)
					$lightbox = '[' . $lightbox . ']';

				if ($counter)
					Counters::increase($counter);
					
				$RESULT .= '<div class="thumbnail ' . Counters::toClass($counter) . '">';
				if (!$image && $fallback)
				{
					if ($url)
						$RESULT .= '<a href="' . HTML::e($url) . '"><img src="' . PATH . $fallback . '" alt="' . HTML::e($text) . '" title="' . HTML::e($text) . '" /></a>';
					else
						$RESULT .= '<img src="' . PATH . $fallback . '" alt="' . HTML::e($text) . '" title="' . HTML::e($text) . '" />';
				}
				else if ($image)
				{
					if ($url)
						$RESULT .= '<a href="' . HTML::e($url) . '">';
					else
					{
						$fullsuffix = '.full';
						if (!is_file(DATA . 'blob/' . $image . $fullsuffix))
							$fullsuffix = '';
						$RESULT .= '<a href="' . PATH . 'data/blob/' . $image . $fullsuffix . '" rel="prettyPhoto'.$lightbox.'" title="' . HTML::e($text) . '">';
					}
					$RESULT .= '<img src="' . PATH . 'data/blob/' . $image . $suffix . '" alt="' . HTML::e($text) . '" title="' . HTML::e($text) . '" />';
					$RESULT .= '</a>';
				}
				$RESULT .= '</div>';
			}
			else if ($part == 'images')
			{
				$images = array();
				if (is_array($args[0]))
					$images = $args[0];
				else if (is_object($args[0]) && isset($args[0]->images))
					$images = $args[0]->images;
				$id = @$args[1];
				$url = @$args[2];
				$suffix = @$args[3];
				$fallback = @$args[4];
				$lightbox = @$args[5];
				if ($lightbox === null)
					$lightbox = '[' . mt_rand(1, 1000000) . ']';
				$layout = @$args[6];
				if ($layout === null)
					$layout = 'div';
				$layout_arg = @$args[7];
				if ($layout_arg === null)
					$layout_arg = 3;
				$skip = @$args[8];

				if ($id)
					$images = (isset($images[$id]) ? array($images[$id]) : array());
				$temp = $images; $images = array();
				foreach ($temp as $k => $image)
					if ((substr($image['type'], 0, 6) == 'image/') && ($skip != $image['blob']))
					{
						if (!@$image['text'.LL] && is_object($args[0]) && isset($args[0]->title))
							$text = $args[0]->title;
						else if (!@$image['text'.LL] && is_object($args[0]))
							$text = $args[0]->otitle;
						else
							$text = @$image['text'.LL];
						$image['text'] = $text;
						$images[] = $image;
					}

				$RESULT .= '<'.$layout.' class="attachments attachments_images images">';

				$counter = U::randomString(8);
				
				if ($layout == 'div')
				{
					foreach ($images as $image)
						$RESULT .= self::common('thumbnail', @$image['blob'], $image['text'], $url, $suffix, $fallback, $lightbox, $counter);
				}
				else
				{
					$table = array();
					$temp0 = (int) ceil(count($images) / $layout_arg); 
					reset($images);
					for ($y = 0; $y < $temp0; $y++)
					{
						$RESULT .= '<tr class="images">';
						for ($x = 0; $x < $layout_arg; $x++)
						{
							$RESULT .= '<td>';
							$image = @$images[$y * $layout_arg + $x];
							if ($image)
								$RESULT .= self::common('thumbnail', @$image['blob'], $image['text'], $url, $suffix, $fallback, $lightbox, $counter);
							$RESULT .= '</td>';
						}
						$RESULT .= '</tr>';
						
						$RESULT .= '<tr class="texts">';
						for ($x = 0; $x < $layout_arg; $x++)
						{
							$RESULT .= '<td>';
							$image = @$images[$y * $layout_arg + $x];
							if ($image)
								$RESULT .= $image['text'];
							$RESULT .= '</td>';
						}
						$RESULT .= '</tr>';
					}
				}

				$RESULT .= '</'.$layout.'>';
			}
			else if ($part == 'attachments')
			{
				$blobs = array();
				if (is_array($args[0]))
					$blobs = $args[0];
				else if (is_object($args[0]) && isset($args[0]->attachments))
					$blobs = $args[0]->attachments;
				//else if (is_object($args[0]) && isset($args[0]->blobs))
				//	$blobs = $args[0]->blobs;

				$RESULT .= '<div class="attachments attachments_nonimages">';

				if ($id && isset($blobs[$id]))
					$blobs = array($blobs[$id]);
				else
					foreach ($blobs as $blobid => $blob)
						if ($id || (substr($blob['type'], 0, 6) != 'image/'))
						{
							if (!@$blob['text'.LL] && is_object($args[0]) && isset($args[0]->title))
								$text = $args[0]->title;
							else if (!@$blob['text'.LL] && is_object($args[0]))
								$text = $args[0]->otitle;
							else
								$text = @$blob['text'.LL];
							$RESULT .= '<a href="/data/blob/'.@$blob['blob'].'">' . HTML::e($text) . '</a>';
						}

				$RESULT .= '</div>';
			}
			else if ($part == 'list-tree')
			{
				extract($args[0], EXTR_SKIP);
				$RESULT .= '<ul>';
				foreach ($tree as $object)
				{
					$RESULT .= '<li>';
					$RESULT .= $list->url(array('object' => null, 'parent' => $object), $object->otitle, 7);
					if ($object->x['tree']->size())
						$RESULT .= self::common('list-tree', array('list' => $list, 'tree' => $object->x['tree']));
					$RESULT .= '</li>';
				}
				$RESULT .= '</ul>';
			}
			else if ($part == 'list')
			{
				extract($args[0], EXTR_SKIP);
				$action = $invocation->action();
				$module = $action->module();
				$object = $module->object;
				$refSelf = new Invocation($action->id, $module->id);

				$RESULT .= '<div class="actions_top">';
				foreach ($result['actions'] as $temp0)
				{
					$ref = clone($temp0);
					if (($ref->context != 'object') && $ref->prepare(array('object' => null, 'parent' => $result['parent']), false))
					{
						$temp0 = $ref->icon;
						$ref->icon = 'x';
						$RESULT .= $ref->url(array('object' => null, 'parent' => $result['parent']), null, 7);
						$ref->icon = $temp0;
					}
				}
				$RESULT .= '</div><!-- /actions_top -->';

				if ($object->get('tree'))
				{
					$RESULT .= '<div class="parents">';
					$RESULT .= $result['list']->url(array('object' => null, 'parent' => $object), l('top', '@core'), 7);
					$parents = $result['parent']->parents();
					$parents->add($result['parent']);
					foreach ($parents as $parent)
						if ($parent->oid)
							$RESULT .= ' &raquo; ' . $result['list']->url(array('object' => null, 'parent' => $parent), $parent->otitle, 7);
					$RESULT .= '</div>';
				}

				$RESULT .= '<table class="layout"><tr>';

				if ($object->get('tree'))
				{
					$RESULT .= '<td class="layout left" valign="top"><div class="tree">';
					if (isset($tree_content))
						$RESULT .= $tree_content;
					else
					{
						$get = new Invocation('get', $module->id);
						$groups = $get->dispatch(array('oid' => null, 'groups' => true), true);
						$RESULT .= self::common('list-tree', array('list' => $result['list'], 'tree' => $groups['objects']['tree']));
					}
					$RESULT .= '</div></td>';
				}

				$RESULT .= '<td class="layout right" valign="top">';

				if ($result['filter_form'])
				{
					$RESULT .= '<div class="filter">';
					if ((page()->location != 'a') && (page()->location != 'aa'))
						if ($temp = Template::prepare($module->id . '-list-filter-form'))
							$result['filter_form']->template = $temp;
					if (!$result['filter_form']->template)
						$result['filter_form']->template = new Template(null, array('CoreTemplates', 'list_filter_form'), 'callback', 'php');
					$RESULT .= $result['filter_form']->render();
					$RESULT .= '</div>';
				}

				if (count($result['order_fields']) > 1)
				{
					$o = array_values($result['order']);
					$RESULT .= '<div class="order">';
					$RESULT .= '<form action="' . HTML::e(Router::linkToCurrent()) . '"  method="post"';
					$RESULT .= '><div>';
					for ($i = 0; $i < $invocation->get('order-count', 1); $i++)
					{
						 $RESULT .= '<select name="order[]">';
						 foreach ($result['order_fields'] as $k => $v)
						 	$RESULT .= '<option value="' . $k . '"' . ((isset($o[$i]) && ($o[$i] == $k)) ? ' selected="selected"' : '') . '>' . HTML::e($v) . '</option>';
						 $RESULT .= '</select><input type="submit" value="' . l('order-submit', '@core') . '" /></div></form>';
					}
					$RESULT .= '</div>';
				}

				$RESULT .= self::common('pages', @$result['pager'], 'top');

				$columns_show = '';
				foreach ($result['columns'] as $k => $column)
					if (!$column->visible && $column->canShowHide)
						$columns_show .= ' ' .  $refSelf->url(array('column-show' => $k), U::firstUpper($column->text), 7);
				if ($columns_show)
					$RESULT .= __('column-show', '@core') . ':' . $columns_show;					

				if ($template_objects = $invocation->get('template-objects'))
					$RESULT .= $template_objects->process(array('invocation' => $invocation, 'result' => $result));
				else
				{
					$RESULT .= '<table class="list">';
					$header_row = '<tr class="header">';
					foreach ($result['columns'] as $k => $column)
						if ($column->visible)
						{
							$header_row .= '<th>'; 
							if ($column->canShowHide)
								$header_row .= ' ' .  $refSelf->url(array('column-hide' => $k), 'x', 7); 
							$header_row .= U::firstUpper($column->text);
							$header_row .= '</th>';
						}
					if ($result['actions'])
						$header_row .= '<th>' . l('actions', '@core') . '</th></tr>';
					$RESULT .= $header_row;
					$counter = 0;
					foreach ($result['objects'] as $object)
					{
						$style = $object->listStyle();
						$RESULT .= '<tr' . ($style ? (' style="'. $style . '"') : '') . ' class="list ' . ((++$counter % 2) ? 'odd' : 'even') . '">';
						foreach ($result['columns'] as $column)
							if ($column->visible)
							{
								$style = $object->listStyle($column);
								$RESULT .= '<td' . ($style ? (' style="'. $style . '"') : '') . '>';
								$RESULT .= $column->value($object);
								$RESULT .= '</td>';
							}
						if ($result['actions'])
						{
							$RESULT .= '<td class="actions">';
							foreach ($result['actions'] as $temp0)
							{
								$ref = clone($temp0);
								if (($ref->context == 'object') && $ref->prepare(array('object' => $object, 'parent' => $result['parent']), false))
									$RESULT .= $ref->url(array('object' => $object, 'parent' => $result['parent']), null, 7);
							}
							$RESULT .= '</td>';
						}
						$RESULT .= '</tr>';
					}
					$RESULT .= $header_row;
					$RESULT .= '</table>';
				}

				$RESULT .= self::common('pages', @$result['pager'], 'bottom');

				$RESULT .= '</td></tr></table>';
			}
			return $RESULT;
		}
	}
	
	function view()
	{
		return View::current();
	}
?>

ACC SHELL 2018