Example #1
0
 /**
  * Returns a array representing the Mysql schema of this table
  */
 function schema()
 {
     if (empty($this->table)) {
         throw new ConnerException('Unknown table for schema in model ' . get_class($this));
     }
     if (empty($this->schema)) {
         $cacheKey = 'MysqlModel.' . get_class($this) . '.schema';
         if (is_null($schema = cache\file($cacheKey, null, MONTH))) {
             $schema = array();
             $res = $this->query('DESCRIBE `' . $this->table . '`');
             if (empty($res)) {
                 // false on error
                 return array();
                 // perhaps throwing an exception here may be a better way? i not sure.
             }
             while ($row = $res->fetch_row()) {
                 $schema[$row[0]] = array('key' => $row[0], 'primary' => $row[3] == 'PRI', 'default' => $row[4]);
             }
             $res->free();
             cache\file($cacheKey, $schema, MONTH);
         }
         $this->schema = $schema;
     }
     return $this->schema;
 }
Example #2
0
/**
 * Include and run an element and it's loader
 * @param $path string
 * @param $params variables to pass into element
 * @param $options
 *   pass [false]|true parent elements pass any parameters to children elements
 *   layout string|[false] use a layout file for this element
 */
function elem($path, $params = array(), $options = array())
{
    if (!is_array($path)) {
        $path = explode('/', $path);
    }
    $ret = false;
    $options = array_merge(array('layout' => false, 'cache' => false, 'cacheKey' => ''), $options);
    if ($options['cache']) {
        $cacheKey = 'Elements.' . KEY . '.' . implode(DS, $path) . '.' . $options['cacheKey'];
        $ret = cache\file($cacheKey, null, (int) $options['cache']);
    }
    $elemFile = ELEMENTS . DS . implode(DS, $path) . '.ctp';
    if (file_exists($elemFile)) {
        $loadData = array();
        global $WEB_ELEM_LEVEL, $WEB_ELEM_PARAMS;
        if (empty($WEB_ELEM_LEVEL)) {
            $WEB_ELEM_LEVEL = 0;
        }
        if (empty($WEB_ELEM_PARAMS)) {
            $WEB_ELEM_PARAMS = array($WEB_ELEM_LEVEL => array());
        }
        $WEB_ELEM_LEVEL++;
        try {
            $params = elem_loader($path, $params);
        } catch (ConnerException $e) {
        }
        if (array_key_exists($WEB_ELEM_LEVEL - 1, $WEB_ELEM_PARAMS)) {
            $params = array_merge($WEB_ELEM_PARAMS[$WEB_ELEM_LEVEL - 1], $params);
        }
        if (!empty($options['pass'])) {
            $WEB_ELEM_PARAMS[$WEB_ELEM_LEVEL] = $params;
        }
        global $WEB_GLOBAL_PARAMS;
        if (!empty($WEB_GLOBAL_PARAMS)) {
            $params = array_merge($WEB_GLOBAL_PARAMS, $params);
        }
        if (empty($options['layout'])) {
            ob_start();
            elem_file($elemFile, $params);
            $ret = ob_get_clean();
        } else {
            ob_start();
            elem_file($elemFile, $params);
            $ret = elem_layout($options['layout'], ob_get_clean(), $params);
        }
        $WEB_ELEM_PARAMS[$WEB_ELEM_LEVEL] = array();
        $WEB_ELEM_LEVEL--;
    } else {
        throw new ConnerException('Element "' . $elemFile . ' Not Found');
    }
    if ($options['cache']) {
        cache\file($cacheKey, $ret, (int) $options['cache']);
    }
    return $ret;
}