Exemplo n.º 1
0
/**
 * Create an object based from the given $modelName and $params
 *
 * @param string $className The ClassName
 * @param array $config Some configuration
 */
function zbase_object_factory($className, $config = [])
{
    $object = new $className();
    if (!empty($config)) {
        foreach ($config as $k => $v) {
            $method = zbase_string_camel_case('set_' . $k);
            if (method_exists($object, $method)) {
                $object->{$method}($v);
            }
        }
    }
    if (method_exists($object, 'setZbase')) {
        $object->setZbase(zbase());
    }
    return $object;
}
Exemplo n.º 2
0
 /**
  * Return the HTML Id
  * @return string
  */
 public function getHtmlId()
 {
     $id = method_exists($this, 'id') ? $this->id() : null;
     $prefix = property_exists($this, 'htmlPrefix') ? $this->htmlPrefix : null;
     return zbase_string_camel_case($prefix . '_' . $id);
 }
Exemplo n.º 3
0
 /**
  * UI Factory
  * @param array $configuration
  * @return \Zbase\Ui\UiInterface
  */
 public static function factory($configuration)
 {
     // $configuration = zbase_data_get($configuration);
     if (!is_array($configuration)) {
         $configuration = zbase_data_get($configuration);
         if (empty($configuration)) {
             return null;
         }
     }
     $type = !empty($configuration['type']) ? $configuration['type'] : 'ui';
     $prefix = '';
     if (!empty(preg_match('/component./', $type))) {
         $prefix = '\\Component';
         $type = zbase_string_camel_case(str_replace('component.', '', $type));
     }
     if (!empty(preg_match('/data./', $type))) {
         $prefix = '\\Data';
         $type = zbase_string_camel_case(str_replace('data.', '', $type));
     }
     $id = !empty($configuration['id']) ? $configuration['id'] : null;
     if (is_null($id)) {
         throw new Exceptions\ConfigNotFoundException('Index:id is not set on Ui Factory');
     }
     if (!empty($type)) {
         $className = zbase_model_name(null, 'class.ui.' . strtolower($type), '\\Zbase\\Ui' . $prefix . '\\' . ucfirst($type));
         $element = new $className($configuration);
         return $element;
     }
     return null;
 }
Exemplo n.º 4
0
 /**
  * Return a usable clean Prefix
  *
  * @return string
  */
 public function getWidgetPrefix($tag = null)
 {
     return zbase_string_camel_case(str_replace('_', '', $this->id()) . (!empty($tag) ? '_' . $tag : ''));
 }
Exemplo n.º 5
0
 public function postNodeWidgetController($method, $action, $data, \Zbase\Widgets\Widget $widget)
 {
     if (method_exists($widget, 'nodeWidgetController')) {
         return $this->nodeWidgetController($method, $action, $data, $widget);
     }
     $methodName = zbase_string_camel_case('node_' . $method . '_' . $action . '_widget_controller');
     if (zbase_is_dev()) {
         zbase()->json()->addVariable(__METHOD__ . ' : ' . $this->postTableName(), $methodName);
     }
     if (method_exists($this, $methodName)) {
         return $this->{$methodName}($method, $action, $data, $widget);
     }
     try {
         if ($action == 'index') {
             return;
         }
         if ($action == 'create' && strtolower($method) == 'post') {
             zbase_db_transaction_start();
             $this->postRowCreate($data);
             $this->postLog($this->postTableName() . '_' . $action);
             zbase_db_transaction_commit();
             return true;
         }
         if ($action == 'update' && strtolower($method) == 'post') {
             zbase_db_transaction_start();
             $this->postRowUpdate($data);
             $this->postLog($this->postTableName() . '_' . $action);
             zbase_db_transaction_commit();
             return true;
         }
         if ($action == 'upload' && strtolower($method) == 'post') {
             zbase_db_transaction_start();
             $options = ['postObject' => $this, 'upload_dir' => $this->postFileUploadFolder(), 'image_versions' => false, 'script_url' => $this->postFileUrl()];
             $uploadHandler = new \Zbase\Utility\FileUploadHandler($options);
             $this->postLog($this->postTableName() . '_' . $action);
             zbase()->json()->addVariable('_postHtmlId', $this->postHtmlId());
             zbase_db_transaction_commit();
             return true;
         }
         if ($action == 'delete' && strtolower($method) == 'post') {
             zbase_db_transaction_start();
             $this->postRowDelete();
             $this->postLog($this->postTableName() . '_' . $action);
             zbase_db_transaction_commit();
             return true;
         }
     } catch (\Zbase\Exceptions\RuntimeException $e) {
         zbase_db_transaction_rollback();
     }
     return false;
 }
Exemplo n.º 6
0
/**
 * Create ServiceName based on Module and Widget
 *
 * @param Module $module
 * @param Widget $widget
 * @return string;
 */
function zbase_angular_module_servicename($module, $widget)
{
    if (!$module instanceof \Zbase\Module\Module) {
        $module = zbase()->module($module);
    }
    if (!$widget instanceof \Zbase\Widgets\Widget) {
        $widget = zbase()->widget($widget);
    }
    if ($module instanceof \Zbase\Module\Module && $widget instanceof \Zbase\Widgets\Widget) {
        $serviceName = ucfirst(zbase_string_camel_case($module->id() . '_datatable_service'));
        return $serviceName;
    }
    return null;
}
Exemplo n.º 7
0
 /**
  * __call
  *
  * @param string $name
  * @param mixed $arguments
  * @return mixed
  */
 public function __call($name, $arguments = null)
 {
     if (zbase_string_starts_with($name, 'get')) {
         $names = str_replace('get_', '', zbase_string_snake_case($name));
         return $this->__get(zbase_string_camel_case($names));
     }
     if (zbase_string_starts_with($name, 'set')) {
         $names = str_replace('set_', '', zbase_string_snake_case($name));
         return $this->__set(zbase_string_camel_case($names), $arguments);
     }
     // throw new \Zbase\Exceptions\RuntimeException($name . ' method or property doesn\'t exists');
 }
Exemplo n.º 8
0
 /**
  * Check if $type property exists
  *
  * @param string $type
  * @return boolean
  * @throws Exceptions\PropertyNotFound
  */
 protected function _hasKey($type)
 {
     $typeName = zbase_string_camel_case($type);
     if (!property_exists($this, $typeName)) {
         throw new Exceptions\PropertyNotFoundException('Property "' . $type . '" not found in ' . __CLASS__);
     }
     return true;
 }
Exemplo n.º 9
0
 /**
  *
  * @param string $method The property or method to be called
  * @param string $name The name of the relationship index
  * @param array $config The config of the relationship
  * @return HasOne|BelongsTo
  */
 protected function _relationship($method, $name, $config)
 {
     $rType = zbase_value_get($config, 'type', false);
     if (!empty($rType)) {
         $rMethod = zbase_value_get($config, 'class.method', zbase_string_camel_case($name));
         $isFetching = preg_match('/fetch/', $method);
         if ($isFetching) {
             $method = zbase_string_camel_case(str_replace('fetch', '', $method));
         }
         if ($rMethod == $method) {
             $rEntity = zbase_value_get($config, 'entity', false);
             $rInverse = zbase_value_get($config, 'inverse', false);
             $model = zbase_entity($rEntity);
             $lKey = zbase_value_get($config, 'keys.local', null);
             $fKey = zbase_value_get($config, 'keys.foreign', null);
             if (!empty($rEntity)) {
                 switch (strtolower($rType)) {
                     case 'onetoone':
                         if (!empty($rInverse)) {
                             $relObj = new BelongsTo($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey, $name);
                         } else {
                             $relObj = new HasOne($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey);
                         }
                         break;
                     case 'onetomany':
                         if (!empty($rInverse)) {
                             $relObj = new BelongsTo($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey, $name);
                         } else {
                             return new HasMany($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey);
                         }
                         break;
                     case 'manytomany':
                         $pivot = zbase_value_get($config, 'pivot', null);
                         return new BelongsToMany($model->newQuery(), $this, $pivot, $fKey, $lKey, $name);
                     case 'hasmanythrough':
                         break;
                     case 'morphto':
                         break;
                     case 'morphtomany':
                         break;
                     case 'belongsto':
                         return new BelongsTo($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey, $name);
                     default:
                 }
                 if (!empty($relObj)) {
                     if ($this->relationshipMode == 'result') {
                         return zbase_cache($this->cacheKey($relObj), function () use($relObj) {
                             return $relObj->getResults();
                         }, [$this->getTable()]);
                     }
                     return $relObj;
                 }
             }
         }
     }
     return false;
 }
Exemplo n.º 10
0
 /**
  * Test __toString with conditions
  *
  * @return void
  * @test
  * @coversDefaultClass __toString
  */
 public function testToStringWithConditions()
 {
     $config = ['id' => 'style', 'rel' => 'stylesheet', 'href' => 'style.css', 'html' => ['conditions' => 'lte IE 8', 'attributes' => null]];
     $view = new Zbase\Models\View\HeadLink($config);
     $this->assertEquals($view->__toString(), '<!--[lte IE 8]><link id="' . zbase_string_camel_case('headlink-style') . '" href="style.css" rel="stylesheet" type="text/css" /><![endif]-->');
 }