Esempio n. 1
0
    /**
     * Returns a list of links to model types' actions (Page types, User types, etc.)
     * By default, with no options specified, this returns a list of links to create any type of page (except core page).
     *
     * @param $model_name String The model name (can be lowercase, the Util class corrects it)
     * @param $action String The controller action
     * @param $options Array Various options that get passed to Util::list_types() and the key "link_options" can contain an array of options for the $this->html->link()
     * @return String HTML list of links
     * @see minerva\libraries\util\Util::list_types()
    */
    public function link_types($model_name='Page', $action='create', $options=array()) {
        $options += array('exclude_minerva' => true, 'link_options' => array());
        $libraries = Util::list_types($model_name, $options);
        $output = '';
        
        (count($libraries) > 0) ? $output .= '<ul>':$output .= '';
	foreach($libraries as $library) {
            if(substr($library, 0, 7) == 'minerva') {
                $model = $library;
            } else {
                $model = 'minerva\libraries\\' . $library;
            }
	    $type = current(explode('\\', $library));
	    if(strtolower($type) == 'minerva') {
		$type = null;
	    }
	    $output .= '<li>' . $this->link($model::display_name(), '/' . Inflector::pluralize($model_name) . '/' . $action . '/' . $type, $options['link_options']) . '</li>';
	}
        (count($libraries) > 0) ? $output .= '</ul>':$output .= '';
        
        return $output;
    }
 /**
  * Generic index() action that returns a list of paginated documents.
  * The trick here is that $this->calling_class and $this->calling_method will hold a string
  * reference for which extended class called this index() method. We need that in order to
  * get the proper records and access.
 */
 public function index() {
     // get the "_type" ... page_type, user_type, or block_type
     $controller = $this->request->params['controller'];
     $controller_pieces = explode('.', $controller);
     if(count($controller_pieces) > 1) {
         $controller = end($controller_pieces);
     }
     $model = Inflector::classify(Inflector::singularize($controller));
     $modelClass = 'minerva\models\\'.$model;
     $x_type = strtolower($model) . '_type';
     // or set it to "all" if there wasn't a param passed
     $type = ((isset($this->request->params[$x_type])) && (in_array($x_type, $this->library_fields))) ? $this->request->params[$x_type]:'all';
     
     // Default options for pagination, merge with URL parameters
     $defaults = array('page' => 1, 'limit' => 10, 'order' => 'created.desc');
     $params = Set::merge($defaults, $this->request->params);
     if((isset($params['page'])) && ($params['page'] == 0)) {
         $params['page'] = 1;
     }
     list($limit, $page, $order) = array($params['limit'], $params['page'], $params['order']);
     
     // If there's a page/user/block_type passed, add it to the conditions, 'all' will show all pages.
     if($type != 'all') {
         $conditions = array($type => $this->request->params[$type]);
     } else {
         $conditions = array();
     }
     
     // If a search query was provided, search all "searchable" fields (any field in the model's $search_schema property)
     // NOTE: the values within this array for "search" include things like "weight" etc. and are not yet fully implemented...But will become more robust and useful.
     // Possible integration with Solr/Lucene, etc.
     if((isset($this->request->query['q'])) && (!empty($this->request->query['q']))) {
         $search_schema = $modelClass::search_schema();
         // If the "*_type" is set to "all" then we want to get all the model type's schemas, merge them into $schema
         if($type == 'all') {
         foreach(Util::list_types($model, array('exclude_minerva' => true)) as $library) {
             $extendedModelClass = 'minerva\libraries\\' . $library;
             $search_schema += $extendedModelClass::search_schema();
         }
         }
         
         $search_conditions = array();
         // For each searchable field, adjust the conditions to include a regex
         foreach($search_schema as $k => $v) {
         // TODO: possibly factor in the weighting later. also maybe note the "type" to ensure our regex is going to work or if it has to be adjusted (string data types, etc.)
         //var_dump($k);
         $search_regex = new \MongoRegex('/' . $this->request->query['q'] . '/i');
         $conditions['$or'][] = array($k => $search_regex);
         }
         
     }
     
     // Get the documents and the total
     $documents = array();
     if((int)$params['limit'] > 0) {
         $documents = $this->getDocument(array(
             'action' => $this->calling_method,
             'request' => $this->request,
             'find_type' => 'all',
             'conditions' => $conditions,
             'limit' => (int)$params['limit'],
             'offset' => ($params['page'] - 1) * $limit,
             'order' => $params['order']
         ));
     }
     
     // Get some handy numbers
     $total = $modelClass::find('count', array(
         'conditions' => $conditions
     ));
     $page_number = $params['page'];
     $total_pages = ((int)$params['limit'] > 0) ? ceil($total / $params['limit']):0;
     
     // Set data for the view template
     $this->set(compact('documents', 'limit', 'page_number', 'total_pages', 'total'));
 }