HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/class.views.php
<?php
/**
 * This file holds the iwp_views class
 *
 * @package IWP
**/

/**
 * The iwp_views class to be used for views of sections within IWP
 *
 * @package IWP
**/
if(!class_exists('iwp_views', false)) {
	class iwp_views extends iwp_base {
		/**
		 * To hold the type of view this is
		 *
		 * @var String
		**/
		private $type;

		/**
		 * Will be an array containing the list of known types
		 *
		 * @var Array
		**/
		private $typeList = array('content', 'contenttype', 'feedback', 'group', 'module', 'user');

		/**
		 * Holds all data for this view
		 *
		 * @var Array
		**/
		private $data;

		/**
		 * All possible information to be saved for this view
		 *
		 * @var Array
		**/
		private $dataTypes = array('viewid', 'type', 'name', 'vars');

		/**
		 * The list of different types of options with views
		 *
		 * @var Array
		**/
		private $viewOptions = array('begins', 'ends', 'doescontain', 'doesnotcontain', 'checked', 'above', 'below', 'equals', 'memberof');

		/**
		 * Will construct the object with the provided type
		 *
		 * @param String $type The type of the view
		 * @throws iwp_exception_view
		**/
		public function __construct($type) {
			if(!$this->isValidViewType($type)) {
				throw new iwp_exception_view(GetLang('ViewTypeInvalid'));
			}

			$this->type = $type;
			$this->data['type'] = $type;
		}

		/**
		 * Will be used for checking values before setting the variables along with checking the var actually exists
		 *
		 * @param String $var The variable to be set
		 * @param Mixed $val The value for the chosen variable
		 * @throws iwp_exception_view
		**/
		public function Set($var, $val) {
			if(!$this->isValidDataType($var)) {
				$this->data['vars'][$var] = $val;
			} else {
				$this->data[$var] = $val;
			}
		}

		/**
		 * Will retrieve the specific variable
		 *
		 * @return Mixed
		**/
		public function Get($var) {
			if(!$this->isValidDataType($var) && isset($this->data['vars'][$var])) {
				return $this->data['vars'][$var];
			}

			if($var !== 'vars' && isset($this->data[$var])) {
				return $this->data[$var];
			}
			return '';
		}

		/**
		 * Will check if a provided type is valid or not
		 *
		 * @param String $type The type to check for
		 * @return Boolean
		**/
		public function isValidViewType($type) {
			if(!in_array($type, $this->typeList)) {
				return false;
			}
			return true;
		}

		/**
		 * Will check if the provided type is a valid data to be used for this view
		 *
		 * @param String $type The name of the data type to check for validity
		 * @return Boolean
		**/
		public function isValidDataType($type) {
			if(!in_array($type, $this->dataTypes)) {
				return false;
			}
			return true;
		}

		/**
		 * Will take a request for saving a view and complete it. Intended to be called in response to an AJAX save request, will check POST parameters directly. This function was moved from iwp_admin_users to here so it could be shared by iwp_admin_groups.
		 *
		 * @param String $type The type of view to save.
		 */
		public function RemoteSave ($type)
		{
			if(!isset($_POST['viewid']) || !isset($_POST['name']) || !isset($_POST['match']) || !isset($_POST['saveexit']) || !isset($_POST['filtName']) || !isset($_POST['typeName'])) {
				$this->xml->writeElement('status', 0);
				$this->xml->writeElement('message', GetLang('InvalidParameters'));

				$this->xml->outputXML();
				die();
			}

			$viewid = (int)$_POST['viewid'];
			$this->Set('viewid', $viewid);
			$this->Set('name', $_POST['name']);
			$this->Set('match', $_POST['match']);
			$this->Set('type', $type);
			$this->Set('orderby', $_POST['orderby']);
			$this->Set('perpage', $_POST['perpage']);

			while(list($i,$v) = each($_POST['filtName'])) {
				if(strlen($_POST['filtName'][$i]) < 1 || !isset($_POST['typeName'][$i]) || !isset($_POST['filterEntry'][$i])) {
					continue;
				}

				$this->Set('Filter' . $i, array("name"=>$v, "type"=>$_POST['typeName'][$i], "filter"=>$_POST['filterEntry'][$i]));
			}

			try {
				$this->SaveView();
			} catch(iwp_exception_view $e) {
				// Oh noes!
				$this->xml->writeElement('status', 0);
				$this->xml->writeElement('message', GetLang('ViewUnableSave') . $e);

				$this->xml->outputXML();
				die();
			}

			$this->xml->writeElement('status', 1);

			if ($viewid) {
				$message = GetLang('ViewSaveSuccess');
			} else {
				$message = GetLang('ViewCreateSuccess');
			}

			$this->xml->writeElement('viewid', $this->Get('viewid'));

			if ($_POST['saveexit'] == 'true') {
				iwp_session::Set('ViewSaveSuccess', $message);
				$this->xml->writeElement('redirect', 'index.php?section=user&action=view&viewid='. $this->Get('viewid'));
			} else {
				$this->xml->writeElement('message', $message);
			}

			$this->xml->outputXML();
			die();
		}

		/**
		 * This function will save the current data into the db for use later on
		 *
		 * @throws iwp_exception_view
		**/
		public function SaveView() {
			while(list(,$v) = each($this->dataTypes)) {
				if ($v == 'viewid') {
					//	skip viewid
					continue;
				}

				if (!isset($this->data[$v]) || (!is_array($this->data[$v]) && strlen($this->data[$v]) < 1)) {
					throw new iwp_exception_view(GetLang('ViewIncompleteData'));
				}

				//	check filter fields to make sure the 3rd column has been entered
				if ($v == 'vars' && is_array($this->data[$v])) {
					foreach ($this->data[$v] as $key => $filter) {
						if (!$filter['type']) {
							throw new iwp_exception_view(GetLang('ViewIncompleteData'));
						}

						if (substr($key, 0, 6) != 'Filter' || $filter['type'] == 'equals') {
							continue;
						}

						if (!isset($filter['filter']) || $filter['filter'] == '') {
							throw new iwp_exception_view(GetLang('ViewIncompleteData'));
						}
					}
				}
			}
			reset($this->dataTypes);

			$this->data['vars'] = http_build_query($this->data['vars']);

			// All data should be good to go
			if(isset($this->data['viewid']) && is_int($this->data['viewid']) && $this->data['viewid'] > 0) {
				// Remove the viewid so it isn't a part of the query
				$viewid = $this->data['viewid'];
				unset($this->data['viewid']);

				// Run the update for the view
				if(!$this->db->UpdateQuery(IWP_TABLE_VIEWS, $this->data, "`viewid`='" . $viewid . "'")) {
					throw new iwp_exception_view(GetLang('ViewUnableUpdate') . $this->db->GetErrorMsg());
				}

				$this->data['viewid'] = $viewid;
			} else {
				// Make sure there's no bogus viewid going to be used
				if(isset($this->data['viewid'])) {
					unset($this->data['viewid']);
				}

				// Save the view
				if(!$this->db->InsertQuery(IWP_TABLE_VIEWS, $this->data)) {
					throw new iwp_exception_view(GetLang('ViewUnableSave') . $this->db->GetErrorMsg());
				}

				$this->data['viewid'] = $this->db->LastId();
			}
		}

		/**
		 * Will load the view with the specified ID
		 *
		 * @param Integer $id The ID of the view to load
		 * @throws iwp_exception_view
		**/
		public function LoadView($id) {
			// An ID needs to be an Integer
			if(!is_int($id)) {
				throw new iwp_exception_view(GetLang('ViewIdNotInt'));
			}

			// Run the query to check the ID is valid
			$sql = 'SELECT SQL_CALC_FOUND_ROWS type, name, vars FROM ' . IWP_TABLE_VIEWS . ' WHERE `viewid`=\'' . $id . '\'';
			$result = $this->db->Query($sql);

			$count = $this->db->FetchOne("SELECT found_rows()");
			// Check if the view exists or not
			if($count != 1) {
				throw new iwp_exception_view(GetLang('ViewNonExist'));
			}

			$row = $this->db->Fetch($result);
			if($this->type === $row['type']) {
				$this->data['type'] = $row['type'];
				$this->data['name'] = $row['name'];
				parse_str($row['vars'], $this->data['vars']);
				$this->data['viewid'] = $id;
			} else {
				// The view doesn't match the type, so we die
				throw new iwp_exception_view(GetLang('ViewInvalid'));
			}
		}

		/**
		 * Will retrieve an array of the different types of actions that can be used with a view
		 *
		 * @return Array
		**/
		public function LoadViewOptions() {
			return $this->viewOptions;
		}

		/**
		 * Will load a set of specific views and return an array of them
		 *
		 * @param String $type The type of views to return
		 * @return Array
		 * @throws iwp_exception_view
		**/
		public function LoadViewList($type) {
			if(!$this->isValidViewType($type)) {
				throw new iwp_exception_view(GetLang('ViewTypeInvalid'));
			}

			// $type doesn't need quoting as it has been filtered from above
			$sql = "SELECT SQL_CALC_FOUND_ROWS viewid, name, vars FROM " . IWP_TABLE_VIEWS . " WHERE `type`='" . $type . "'";
			$result = $this->db->Query($sql);
			$TotalResult = $this->db->FetchOne("SELECT found_rows()");

			// No views to display
			if(!$result || $TotalResult < 1) {
				return array(0, false);
			}

			// Load the views and then return them
			$viewList = array();
			while(($row = $this->db->Fetch($result))) {
				$viewList[] = array(
					'viewid' => $row['viewid'],
					'name' => $row['name'],
					'vars' => $row['vars'],
					'type' => $type,
				);
			}
			return array($TotalResult, $viewList);
		}

		/**
		 * Will delete the specified view
		 *
		 * @param Integer $id The ID of the view to be deleted
		 * @throws iwp_exception_view
		**/
		public function DeleteView($id) {
			if(!is_int($id)) {
				throw new iwp_exception_view(GetLang('ViewIdNotInt'));
			}

			if(!$this->db->Query("DELETE FROM " . IWP_TABLE_VIEWS . " WHERE `viewid`='" . $id . "'")) {
				throw new iwp_exception_view(GetLang('ViewUnableDelete') . $this->db->GetErrorMsg());
			}

			$this->data = array();
			$this->type = '';
		}

		/**
		 * This will return the form to create a view
		 *
		 * @return Array
		**/
		public function CreateForm($type, $fields) {
			if(!in_array($type, $this->typeList)) {
				throw new iwp_exception_form('Not a valid view type');
			}

			$form = new iwp_form();

			$form->AddGroup(GetLang('ViewDetails'));

			$form->AddField('textbox', 'name')->AddValidation('IsNotBlank');

			$form->AddField('select', 'orderby');
			while(list(,$field) = each($fields)) {
				$form->Field('orderby')->AddFieldOption($field['dbfield'] . " ASC", GetLang(ucfirst($type) . "_" . $field['name']) . " (" . GetLang('Ascending') . ")");
				$form->Field('orderby')->AddFieldOption($field['dbfield'] . " DESC", GetLang(ucfirst($type) . "_" . $field['name']) . " (" . GetLang('Descending') . ")");
			}
			reset($fields);

			$form->AddField('hidden', 'type')->Value($type);

			$form->AddField('radio', 'match')->AddFieldOption('UseAND', '')->AddFieldOption('UseOR', '')->DisableLabel();

			$form->AddField('select', 'perpage')->AddFieldOption(array(
				'5'		=> '5',
				'10'	=> '10',
				'20'	=> '20',
				'50'	=> '50',
				'100'	=> '100',
				'200'	=> '200',
				'500'	=> '500',
				'all'	=> 'All',
			));

			$form->AddGroup(GetLang('ViewOptions'));

			$form->AddField('view', 'filter1')->AddValidation('IsValidView')->AddFields($fields)->AddViewType($type);

			if (isset($this->data['viewid'])) {
				$this->template->Assign('ViewID', $this->data['viewid']);
			}

			// Need to add in support for being able to pre-fill the form with the member variables
			if(isset($this->data['name']) && isset($this->data['vars']) && isset($this->data['vars']['match'])) {
				// Theoretically, we should be fine with pre-filling now
				$form->Field('name')->Value($this->data['name']);
				$form->Field('match')->Value($this->data['vars']['match']);
				$form->Field('orderby')->Value($this->data['vars']['orderby']);
				$form->Field('perpage')->Value($this->data['vars']['perpage']);

				// Grab the list of different filter items and then let them all through

			}

			return array(
				$form->GetOutput(),
				$form->GetFieldNamesJavascript(),
				$form->GetJSFieldValidation()
			);
		}

		/**
		 * Compute the number of filters and return the count along with the IDs
		 *
		 * @return Array The count and array of all filter IDs
		**/
		public function getFilterCount() {
			$count = 0;
			$entries = array();
			while(list($n,$v) = each($this->data['vars'])) {
				if(is_array($v) && strpos($n, "Filter") === 0) {
					$count++;
					$entries[] = substr($n, 6);
				}
			}
			reset($this->data['vars']);

			return array($count, $entries);
		}
	}
}