Beispiel #1
0
	/**
	 * Executes direct processing of state and activate internal methods.
	 * Must be called only at the very end of a controller method.
	 */
	public function process()
	{
		if(!isset($this->_access))
			$this->initAccess();
		$this->loadPk();
		if(!$this->testConf())
			throw new AAException(Yii::t('AutoAdmin.errors', 'Incorrect configuration'));
		$this->_viewData = array_merge($this->_viewData, array(
			'rights'	=> $this->_access->exportRights(),
			'pk'		=> $this->_data->pk,
			'partialViews'	=> $this->partialViews,
			'clientData'	=> $this->clientViewData,
			'interface'		=> $this->_interface,
			'bindKeys'		=> Yii::app()->request->getParam('bk', array()),
			'bindKeysParent'=> Yii::app()->request->getParam('bkp', array()),
			'manageAction'	=> &$this->_manageAction,
		));

		switch($this->_manageAction)
		{
			case 'add':	//Form for data adding
			{
				if(!$this->_access->checkRight('add'))
					$this->blockAccess();
				$this->editMode();
				break;
			}
			case 'edit':	//Form for data editing
			{
				if(!$this->_access->checkRight('edit'))
					$this->blockAccess();
				$this->editMode();
				break;
			}
			case 'insert':	//Process inserting data in DB
			{
				if(!$this->_access->checkRight('add'))
					$this->blockAccess();
				$affected = $this->insert();
				$this->resultMode(array('msg'=>Yii::t('AutoAdmin.messages', ($affected ? 'The record was added' : 'The record was not added'))));
				break;
			}
			case 'update':	//Process updating data in DB
			{
				if(!$this->_access->checkRight('edit'))
					$this->blockAccess();
				$affected = $this->update();
				$this->resultMode(array('msg'=>Yii::t('AutoAdmin.messages', ($affected ? 'The record has been changed' : 'The record has not been changed'))));
				break;
			}
			case 'delete':	//Process deletion data in DB
			{
				if(!$this->_access->checkRight('delete'))
					$this->blockAccess();
				$deletingRow = $this->_db->getCurrentRow();
				if(!$deletingRow)
					throw new CHttpException(404, Yii::t('AutoAdmin.errors', 'Can\'t find the record'));

				if($this->confirmDelete && !Yii::app()->request->getPost('sure', false))
				{
					$confirmUrl = AAHelperUrl::update(Yii::app()->request->requestUri, array('action'), array('action'=>'delete'));
					$cancelUrl = AAHelperUrl::stripParam(Yii::app()->request->requestUri, array('action'));
					foreach($this->_data->pk as $pkField=>$value)
						$cancelUrl = AAHelperUrl::stripParam($cancelUrl, "id[{$pkField}]");
					foreach($this->_data->fields as $k=>&$field)
						$field->value = $deletingRow->fields[$k]->value;
					$this->_controller->render($this->viewsPath.'confirmDelete', array('confirmUrl'=>$confirmUrl, 'cancelUrl'=>$cancelUrl, 'fields'=>$this->_data->fields));
					Yii::app()->end();
				}
				else
				{
					$affected = $this->delete($deletingRow);
					$this->resultMode(array('msg'=>Yii::t('AutoAdmin.messages', ($affected ? 'The record was deleted' : 'The record has not been deleted'))));
				}
				break;
			}
			case 'upload':	//Uploading a file for field
			{
				if(!$this->_access->checkRight('edit'))
					$this->blockAccess();
				$viewData = array('field'=>Yii::app()->request->getParam('field'));
				if(!empty($_POST[self::INPUT_PREFIX]['upload']) && !empty($_FILES[self::INPUT_PREFIX]['name']['uploadFile']))
				{
					$upload = $_POST[self::INPUT_PREFIX]['upload'];
					if(preg_match('/'.self::INPUT_PREFIX.'\[([^\]]+)\]$/', $upload['field'], $matches))
					{
						$field = $this->_data->getFieldByName($matches[1]);
						if(!isset($field->options['directoryPath']))
							throw new AAException(Yii::t('AutoAdmin.errors', 'The parameter "{paramName}" must be set for the field {fieldName}', array('parameter'=>'directoryPath', '{fieldName}'=>$field->name)));
						$uploadDir = $field->options['directoryPath'];
						if($field->options['subDirectoryPath'])
							$uploadDir .= '/'.$field->options['subDirectoryPath'];
						
						$viewData['uploadedFileName'] = AAHelperFile::uploadFile('uploadFile', $uploadDir);
						$viewData['uploadedFileAbs'] = Yii::app()->basePath.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.Yii::app()->modules['autoadmin']['wwwDirName'].str_replace('/', DIRECTORY_SEPARATOR, $uploadDir).DIRECTORY_SEPARATOR.$viewData['uploadedFileName'];
						$viewData['uploadedFileSrc'] = "{$uploadDir}/{$viewData['uploadedFileName']}";
						$viewData['alt'] = !empty($upload['alt']) ? $upload['alt'] : '';
						$viewData['fieldName'] = $field->formInputName();
					}
				}
				$this->_controller->layout = 'ext.autoadmin.views.layouts.fileUpload';
				$this->_controller->render($this->viewsPath.'fileUpload', $viewData);
				break;
			}
			case 'empty':	//Interface which is programmed directly by user
			{
				if(!$this->_access->checkRight('read'))
					$this->blockAccess();
				$this->_controller->render($this->viewsPath.'empty', $this->_viewData);
				break;
			}
			default:	//Data list show (default mode)
			{
				if(!$this->_access->checkRight('read'))
					$this->blockAccess();
				$this->listMode();
				break;
			}
		}
	}
Beispiel #2
0
	public function loadFromForm($formData)
	{
		if(!empty($formData[$this->name]) && is_array($formData[$this->name]) && !empty($formData[$this->name]['del']))
		{
			$this->value = null;
		}
		elseif(!empty($_FILES[AutoAdmin::INPUT_PREFIX]['tmp_name']["{$this->name}_new"]))
		{
			$uploadDir = $this->options['directoryPath'];
			if($this->options['subDirectoryPath'])
				$uploadDir .= '/'.$this->options['subDirectoryPath'];
			$this->value = ($this->options['subDirectoryPath'] ? $this->options['subDirectoryPath'].'/' : '').AAHelperFile::uploadFile("{$this->name}_new", $uploadDir);
		}
	}