/** * Copy an image uploaded with HTML form to the specified directory. * In view scripts one should use something like <img src="{$fileBaseDir}/{@return}"/> * * @param string $paramName Parameter name as it passed by a form. * @param string $fileBaseDir A base directory constant for the file (should be used in view scripts as prefix before $filePath from DB table). * @return string New file's name. * @throws CException */ public static function uploadFile($paramName, $fileBaseDir) { if(empty($_FILES[AutoAdmin::INPUT_PREFIX]['tmp_name'][$paramName]) || !empty($_FILES[AutoAdmin::INPUT_PREFIX]['error'][$paramName])) throw new AAException(Yii::t('AutoAdmin.errors', 'An error occured with uploading of the file for field "{field}"', array('{field}'=>$paramName))); $uploadedFileName =& $_FILES[AutoAdmin::INPUT_PREFIX]['name'][$paramName]; $newfname = ''; $toDir = self::srcToPath($fileBaseDir); $newfname = mb_strtolower(mb_substr($uploadedFileName, 0, mb_strrpos($uploadedFileName, '.'))); $newfname = AAHelperText::translite($newfname); $newfname = str_replace(' ', '_', $newfname); $newfname = preg_replace('/[^a-z\-\_0-9]/ui', '', $newfname); if(mb_strlen($newfname)>60) $newfname = mb_substr($newfname, 0, 60); $ext = mb_substr(mb_strrchr($uploadedFileName, '.'), 1); if(!is_dir($toDir)) { if(!mkdir($toDir, 0777, true)) throw new AAException(Yii::t('AutoAdmin.errors', 'The directory "{dirname}" cannot be created', array('{dirname}'=>$toDir))); } while(file_exists($toDir.DIRECTORY_SEPARATOR.$newfname.'.'.$ext)) $newfname .= '_'.rand(0, 9); $newfname .= ".{$ext}"; if(!copy($_FILES[AutoAdmin::INPUT_PREFIX]['tmp_name'][$paramName], $toDir.DIRECTORY_SEPARATOR.$newfname)) throw new AAException(Yii::t('AutoAdmin.errors', 'The file ({filename}) cannot be copied', array('{filename}'=>$newfname))); return $newfname; }
function copyImage($var, $uploadDir='/i/other') { $newFileName = ''; $newFileName = mb_strtolower(mb_substr($_FILES[$var]['name'], 0, mb_strrpos($_FILES[$var]['name'], '.'))); $newFileName = AAHelperText::translite($newFileName); $newFileName = str_replace(' ', '_', $newFileName); $newFileName = preg_replace('/[^a-z\-\_0-9]/ui', '', $newFileName); if(mb_strlen($newFileName)>60) $newFileName = mb_substr($newFileName, 0, 60); $ext = mb_strrchr($_FILES[$var]['name'], '.'); $newFileName .= $ext; $fileLinkDir = $uploadDir; $targetPath = Yii::app()->basePath.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.Yii::app()->modules['autoadmin']['wwwDirName'].str_replace('/', DIRECTORY_SEPARATOR, $uploadDir); if(!is_dir($targetPath)) { if(!mkdir($targetPath)) throw new CHttpException(406, "Указанная в настройках директория [{$fileLinkDir}] не существует и не может быть создана."); } $targetPath .= DIRECTORY_SEPARATOR.$newFileName; if(!copy($_FILES[$var]['tmp_name'], $targetPath)) throw new CHttpException(406, "Файл невозможно сохранить в указанной в настройках директории [{$fileLinkDir}]. Вероятнее всего, проблемы с правами."); if(!getimagesize($targetPath)) { throw new CHttpException(406, "Загружаемый файл не является изображением допустимого формата."); } return $fileLinkDir.'/'.$newFileName; }
public function printValue() { return AAHelperText::strip($this->value, 80); }
/** * Adds a level into the breadcrumbs, converting service parameters in the link and extracting a label in help with SQL-query that is preliminary defined by $this->query. * @param int $level Zero or negative value as step downwards. * @param string $action Controller's action to link to. * @param string $label Label for breadcrumb. * @param bool $labelNonStatic Forces to make a query and use $label as postfix for the found label. * @return string A final breadcrumb label. * @throws AAException */ public function addLevel($level, $action, $label=null, $labelNonStatic=false) { if($level > 0) return; if($labelNonStatic || is_null($label)) { if(!isset($this->query->command->from) || !isset($this->query->command->select)) throw new AAException(Yii::t('AutoAdmin.errors', 'Breadcrumbs can be generated only after AABreadcrumbs->query->select()->from() initializing. Or use call with static label.')); if(!$this->query->command->where) //otherwise means a user set where by himself. { if($level == 0) { $bk = Yii::app()->request->getParam('bk'); } else { $bkp = Yii::app()->request->getParam('bkp'); if(!isset($bkp[abs($level)-1])) throw new AAException(Yii::t('AutoAdmin.errors', 'Incorrect breadcrumbs level.')); $bk = $bkp[abs($level)-1]; } if(is_null($bk) || !is_array($bk)) throw new AAException(Yii::t('AutoAdmin.errors', 'Auto-generation of the breadcrumbs can be done only with the default inside-controller navigation.')); $where = array('AND'); $params = array(); foreach($bk as $pk=>$pkValue) { $where[] = "{$pk}=:{$pk}"; $params[":{$pk}"] = $pkValue; } $this->query->where($where, $params); $this->query->command->limit(1); } $newLabel = $this->query->command->queryScalar(); $label = ($label && $labelNonStatic) ? "{$newLabel}. {$label}" : $newLabel; } if($level == 0) { $this->controller->breadcrumbs[] = $this->controller->pageTitle = AAHelperText::ucfirst($label).'. '.$this->controller->pageTitle; } else { $params = AAHelperUrl::uriToParamsArray(Yii::app()->request->getRequestUri()); if($params) { //Removing GET-params which relate to the current page $paramsToExclude = array('page', 'sortBy', 'searchBy', 'searchQ', 'msg', 'action', 'bk', 'bkp', 'id', 'foreign'); foreach($paramsToExclude as $param) { if(array_key_exists($param, $params)) unset($params[$param]); if(!$params) break; else { foreach($params as $key=>$value) { if(preg_match("/^{$param}\[/i", $key)) { unset($params[$key]); continue 2; } } } } } $link = "../{$action}/"; $bkp = Yii::app()->request->getParam('bkp', array()); if(isset($bkp[0])) { foreach($bkp as $pkLevel=>$pk) { if($pkLevel < abs($level)-1) continue; if($pkLevel==0) $paramBase = 'bk'; else $paramBase = 'bkp['.($pkLevel-1).']'; foreach($pk as $keyField=>$keyValue) { $params[$paramBase.'['.$keyField.']'] = $keyValue; } } } if($params) { $paramStr = ''; foreach($params as $param=>$value) $paramStr .= ($paramStr ? '&' : '?').$param.'='.$value; $link .= $paramStr; } $this->insertToPosition((count($this->controller->breadcrumbs)+$level), $label, $link); } $this->query->command->reset(); return $label; }