Example #1
0
 /**
  * Create the links for resources in the resource root
  * @return array the links, name => config
  */
 protected function createLinks()
 {
     $app = \Yii::app();
     /* @var \Restyii\Web\Application $app */
     $controller = $this->getController();
     $module = $controller->getModule();
     if (!$module) {
         $module = $app;
     }
     $controllers = $app->getSchema()->getControllerInstances($module);
     /* @var \Restyii\Controller\Base[]|\CController[] $controllers */
     $links = array('self' => array('title' => $module->name, 'href' => trim($app->getBaseUrl(), '/') . '/'));
     foreach ($controllers as $id => $controller) {
         if ($id === $module->defaultController) {
             continue;
         }
         $links[$id] = array('title' => method_exists($controller, 'classLabel') ? $controller->classLabel(true) : String::pluralize(String::humanize(substr(get_class($controller), 0, -10))), 'href' => $controller->createUrl('search'));
         if (method_exists($controller, 'classDescription')) {
             $links[$id]['description'] = $controller->classDescription();
         }
         if (isset($controller->modelClass)) {
             $links[$id]['profile'] = array($controller->modelClass);
         }
     }
     return $links;
 }
Example #2
0
 /**
  * Returns the label for this resource type.
  *
  * @param bool $plural whether or not to pluralize the label, e.g. 'Users' instead of 'User'
  *
  * @return string the resource label
  */
 public function classLabel($plural = false)
 {
     if ($plural) {
         $label = String::humanize(String::pluralize(get_class($this)));
     } else {
         $label = get_class($this);
     }
     return $this->generateAttributeLabel($label);
 }
Example #3
0
 /**
  * Parses a URL based on this rule.
  *
  * @param CUrlManager $manager the URL manager
  * @param CHttpRequest $request the request object
  * @param string $pathInfo path info part of the URL (URL suffix is already removed based on {@link CUrlManager::urlSuffix})
  * @param string $rawPathInfo path info that contains the potential URL suffix
  *
  * @return mixed the route that consists of the controller ID and action ID. False if this rule does not apply.
  */
 public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)
 {
     $params = array();
     $segments = array();
     if (preg_match('/\\.([\\w|-]+)$/', $pathInfo, $matches)) {
         $params['urlSuffix'] = $matches[1];
         $pathInfo = mb_substr($pathInfo, 0, -strlen($matches[0]));
     }
     $parts = explode('/', $pathInfo);
     $partCount = count($parts);
     if (!$partCount) {
         return false;
     }
     $module = \Yii::app();
     /* @var Application $module */
     $controllerID = null;
     $actionID = null;
     $expectModule = true;
     $expectController = true;
     $expectId = false;
     $expectAction = false;
     $expectRelation = false;
     $expectRelationId = false;
     $expectRelationAction = false;
     while ($part = array_shift($parts)) {
         if ($this->useDashes) {
             $p = String::dashesToCamelCase($part);
         } else {
             $p = $part;
         }
         if ($expectModule) {
             if ($module->hasModule($p)) {
                 $segments[] = $p;
                 $module = $module->getModule($p);
                 continue;
             } else {
                 $expectModule = false;
             }
         }
         if ($expectController) {
             $expectController = false;
             $expectModule = false;
             if (!is_numeric($p) && preg_match('/^(\\w+)$/', $p)) {
                 $controllerID = $p;
                 $expectAction = true;
                 $expectId = true;
                 continue;
             }
         }
         if ($expectAction && $p[0] == '_') {
             $expectAction = false;
             $actionID = substr($p, 1);
             continue;
         }
         if ($expectRelationAction && $p[0] == '_') {
             $expectRelationAction = false;
             $expectRelationId = false;
             $actionID = substr($p, 1) . 'Related';
             continue;
         }
         if ($expectRelation) {
             $expectRelation = false;
             if (preg_match('/^(\\w+)$/', $p)) {
                 $params['relation'] = $p;
                 $expectRelationAction = true;
                 continue;
             }
         }
         if ($expectId) {
             $params[$this->idName] = $part;
             $expectId = false;
             $expectAction = true;
             $expectRelation = true;
             continue;
         }
         if ($expectRelationId) {
             $params[$this->relationIdName] = $part;
             $expectRelationId = false;
             continue;
         }
         return false;
     }
     if ($controllerID === null) {
         $controllerID = $module->defaultController;
     }
     if ($actionID === null) {
         $requestType = $request->getRequestType();
         if (isset($params['relation'])) {
             if (isset($params[$this->relationIdName])) {
                 $actionID = isset($this->itemVerbMap[$requestType]) ? $this->itemVerbMap[$requestType] . 'Related' : $this->defaultItemAction . 'Related';
             } else {
                 $actionID = isset($this->collectionVerbMap[$requestType]) ? $this->collectionVerbMap[$requestType] . 'Related' : $this->defaultCollectionAction . 'Related';
             }
         } else {
             if (isset($params[$this->idName])) {
                 $actionID = isset($this->itemVerbMap[$requestType]) ? $this->itemVerbMap[$requestType] : $this->defaultItemAction;
             } else {
                 $actionID = isset($this->collectionVerbMap[$requestType]) ? $this->collectionVerbMap[$requestType] : $this->defaultCollectionAction;
             }
         }
     }
     $segments[] = $controllerID;
     $segments[] = $actionID;
     foreach ($params as $key => $value) {
         $_GET[$key] = $value;
     }
     return implode('/', $segments);
 }
Example #4
0
 /**
  * Return a label for the controller
  * @param bool $plural true if the plural form should be returned
  *
  * @return string the label
  */
 public function classLabel($plural = false)
 {
     $humanized = String::humanize(substr(get_class($this), 0, -10));
     return $plural ? String::pluralize($humanized) : $humanized;
 }
Example #5
0
 /**
  * Return a short description of the action.
  * @return string a short description of the action.
  */
 public function description()
 {
     $model = $this->staticModel();
     return \Yii::t('resource', '{pluralActionName} {collectionLabel}', array('{pluralActionName}' => String::pluralize($model->generateAttributeLabel($this->getId())), '{collectionLabel}' => $model->classLabel(true)));
 }