Exemple #1
0
 /**
  * Get an entity
  * @param string $entity
  * @return entity|false
  */
 public function getEntity($entity)
 {
     if (isset($this->model[$entity])) {
         return $this->model[$entity];
     } else {
         if (is_file('modules/' . $this->name . '/model/' . $entity . '.' . \app::$config['dev']['serialization'])) {
             if (!class_exists($this->name . '\\model\\' . $entity)) {
                 include 'modules/' . $this->name . '/model/' . $entity . '.php';
             }
             $this->model[$entity] = \tools::unserialize('modules/' . $this->name . '/model/' . $entity);
             return $this->model[$entity];
         } else {
             throw new \Exception(t('Entity doesn\'t exist', FALSE) . ' : ' . s($entity));
         }
     }
 }
Exemple #2
0
    /**
     * Save model
     * @return string
     */
    protected function saveModelAction($module, $list, $oldSchema)
    {
        $schema = json_decode($list);
        $oldSchema = json_decode($oldSchema, TRUE);
        $tableExists = array();
        /* Get roles ids with behavior anonymous */
        $rolesBehaviorAnonymous = array();
        foreach (\app::getModule('core')->getEntity('role') as $role) {
            if ($role->permissions == 0) {
                $rolesBehaviorAnonymous[] = $role->id_role;
            }
        }
        if (is_array($schema)) {
            foreach ($schema as $tableKey => $table) {
                /* Prepare entity's properties */
                $tplProp = '';
                $tplParam = '';
                $tplAssign = '';
                $args = array();
                $matchOldNewNames = array();
                foreach ($table->properties as $fieldName => $fieldProps) {
                    list($name, $type) = explode(':', $fieldName);
                    $tplProp .= "\t" . 'protected $' . $name . ';' . PHP_EOL;
                    //generates attributes
                    $tplParam .= '\\' . $type . ' $' . $name . ',';
                    //generates the constructor parameters
                    $tplAssign .= "\t\t" . '$this->' . $name . ' = $' . $name . ";\n";
                    //generates assignments in the constructor
                    $reflectionObj = new \ReflectionClass($type);
                    $fieldProps = json_encode($fieldProps);
                    $fieldProps = json_decode($fieldProps, true);
                    if (isset($fieldProps['oldName']) && ($fieldProps['oldName'] != $name && !empty($fieldProps['oldName']))) {
                        $matchOldNewNames[$name] = $fieldProps['oldName'];
                    }
                    unset($fieldProps['oldName']);
                    $field = $reflectionObj->newInstanceArgs(array('name' => $fieldName, 'properties' => $fieldProps));
                    if (!isset($fieldProps['rights'])) {
                        /* Set rights forbidden for non admins, admins are allowed by default */
                        foreach ($rolesBehaviorAnonymous as $id_role) {
                            $field->setRights($id_role, 0);
                        }
                    }
                    $args[] = $field;
                }
                /* Prepare entity's php file */
                $tpl = '<?php
namespace ' . $module . '\\model;
/**
* Description of entity ' . $table->name . '
* @author Parsimony
* @top ' . $table->top . '
* @left ' . $table->left . '
*/
class ' . $table->name . ' extends \\entity {

' . $tplProp . '

	public function __construct(' . substr($tplParam, 0, -1) . ') {
		parent::__construct();
' . $tplAssign . '
	}
';
                $model = 'modules/' . $module . '/model/' . $table->name . '.php';
                if (!is_file($model)) {
                    $tpl .= '// DON\'T TOUCH THE CODE ABOVE ##########################################################' . PHP_EOL . '}' . PHP_EOL . '?>';
                } else {
                    $code = file_get_contents($model);
                    $tpl = preg_replace('@<\\?php(.*)}(.*)?(ABOVE ##########################################################)?@Usi', $tpl, $code);
                }
                \tools::file_put_contents($model, $tpl);
                include_once $model;
                $oldObjModel = FALSE;
                if (is_file('modules/' . $module . '/model/' . $table->oldName . '.' . \app::$config['dev']['serialization'])) {
                    $oldObjModel = \tools::unserialize('modules/' . $module . '/model/' . $table->oldName);
                }
                // Change table Name if changes
                if ($table->name !== $table->oldName) {
                    \PDOconnection::getDB()->exec('ALTER TABLE ' . PREFIX . $module . '_' . $table->oldName . ' RENAME TO ' . $module . '_' . $table->name . ';');
                    unlink('modules/' . $module . '/model/' . $table->oldName . '.php');
                    unlink('modules/' . $module . '/model/' . $table->oldName . '.' . \app::$config['dev']['serialization']);
                }
                // make a reflection object
                $reflectionObj = new \ReflectionClass($module . '\\model\\' . $table->name);
                $newObj = $reflectionObj->newInstanceArgs($args);
                $newObj->__wakeup();
                /* Set entity's properties */
                $newObj->setTitle($table->title);
                $newObj->behaviorTitle = $table->behaviorTitle;
                $newObj->behaviorDescription = $table->behaviorDescription;
                $newObj->behaviorKeywords = $table->behaviorKeywords;
                $newObj->behaviorImage = $table->behaviorImage;
                /* Set entity's rights */
                if (is_object($oldObjModel)) {
                    $newObj->setAllRights($oldObjModel->getAllRights());
                } else {
                    /* Set rights forbidden for non admins, admins are allowed by default */
                    foreach ($rolesBehaviorAnonymous as $id_role) {
                        $newObj->setRights($id_role, 0);
                    }
                }
                if ($oldObjModel !== FALSE) {
                    $nameFieldBefore = '';
                    $newObj->__wakeup();
                    /* to insert entity's ref into fields */
                    foreach ($newObj->getFields() as $field) {
                        if (isset($oldSchema[$table->name]) && isset($oldSchema[$table->name][$field->name])) {
                            $field->alterColumn($nameFieldBefore);
                        } elseif (isset($matchOldNewNames[$field->name])) {
                            $field->alterColumn($nameFieldBefore, $matchOldNewNames[$field->name]);
                        } else {
                            $field->addColumn($nameFieldBefore);
                        }
                        if ($field->type !== '') {
                            /* field_formasso */
                            $nameFieldBefore = $field->name;
                        }
                    }
                    if (isset($oldSchema[$table->name])) {
                        foreach ($oldSchema[$table->name] as $fieldName => $field) {
                            $newFields = $newObj->getFields();
                            if (!isset($newFields[$fieldName]) && !in_array($fieldName, $matchOldNewNames)) {
                                $sql = 'ALTER TABLE ' . PREFIX . $module . '_' . $table->name . ' DROP ' . $fieldName;
                                \PDOconnection::getDB()->exec($sql);
                                //$field->deleteColumn();  //removed to avoid old includes
                            }
                        }
                    }
                } else {
                    $newObj->createTable();
                }
                \tools::serialize('modules/' . $module . '/model/' . $table->name, $newObj);
                $tableExists[] = $table->name;
            }
        }
        $entities = glob('modules/' . $module . '/model/*.php');
        foreach (is_array($entities) ? $entities : array() as $filename) {
            $modelName = substr(substr(strrchr($filename, "/"), 1), 0, -4);
            if (!in_array($modelName, $tableExists)) {
                \app::getModule($module)->getEntity($modelName)->deleteTable();
            }
        }
        return ' ';
    }
Exemple #3
0
 /**
  * Serialize and Save this theme object
  * @param string $module
  * @param string $name
  */
 public static function get($module, $name)
 {
     $file = stream_resolve_include_path($module . '/themes/' . $name . '/theme.' . \app::$config['dev']['serialization']);
     if ($file) {
         return \tools::unserialize(substr($file, 0, -4));
     } else {
         $theme = new theme('container', $name, $module);
         $theme->save();
         return $theme;
     }
 }