Exemplo n.º 1
0
        public void InitFromContext(SynkContext context)
        {
            className = this.GetType().Name.ToLower();

            InitFields();
            context.site.ProcessEntitySchema(this);

            if (tableName == null)
            {
                tableName = className + "_data";
            }

            if (dbName == null)
            {
                dbName = context.config.GetFieldValue("database");
            }

            insertion_date = DateTime.Now.ToTimestamp();

            foreach (var field in this.fields)
            {
                if (!field.writable)
                {
                    continue;
                }

                var fieldName    = field.name;
                var defaultValue = field.GetDefaultValue(context);
                this._currentValues[fieldName]  = defaultValue;
                this._originalValues[fieldName] = defaultValue;
            }
        }
Exemplo n.º 2
0
        private bool GenerateTableForEntity(SynkContext context, Entity entity)
        {
            if (entity == null || !entity.isWritable)
            {
                return(false);
            }

            var dbFields = new Dictionary <string, string>();

            foreach (var field in entity.fields)
            {
                if (!field.writable)
                {
                    continue;
                }

                var fieldType = field.dbType;

                var fieldName = field.name;

                entity.SetFieldValue(fieldName, field.GetDefaultValue(context));

                //$this->$fieldName = $field->defaultValue;

                dbFields[fieldName] = fieldType;
            }

            return(context.database.createTable(entity.dbName, entity.tableName, dbFields));
        }
Exemplo n.º 3
0
        public bool LoadFromRow(SynkContext context, Dictionary <string, string> row)
        {
            if (row == null)
            {
                return(false);
            }

            Int64.TryParse(row["id"], out this.id);
            long.TryParse(row["insertion_date"], out this.insertion_date);

            foreach (var field in _fields.Values)
            {
                var fieldName = field.name;

                if (row.ContainsKey(fieldName))
                {
                    var rowVal = row[fieldName];
                    if (field.dbType.Contains("tinyint"))
                    {
                        rowVal = rowVal.Equals("true", StringComparison.InvariantCultureIgnoreCase) ? "1" : "0";
                    }
                    SetFieldValue(fieldName, rowVal);
                }
                else
                {
                    SetFieldValue(fieldName, field.GetDefaultValue(context));
                }

                _originalValues[fieldName] = GetFieldValue(fieldName);
            }

            this.exists = true;
            return(true);
        }
Exemplo n.º 4
0
        private bool GenerateSummary(SynkContext context, Entity obj, Field field)
        {
            var srcName  = field.target;
            var srcValue = obj.GetFieldValue(srcName).StripHTML();

            obj.SetFieldValue(field.name, srcValue.Summary(20));
            return(true);
        }
Exemplo n.º 5
0
        public string GetDefaultValue(SynkContext context)
        {
            if (defaultValueCallback != null && context != null)
            {
                return(defaultValueCallback(context));
            }

            return(defaultValue);
        }
Exemplo n.º 6
0
        public void paginate(SynkContext context)
        {
            int page;

            int.TryParse(context.request.GetVariable("page"), out page);
            context.request.session.Set("page", page.ToString());
            //context.page = page;

            this.render(context);
        }
Exemplo n.º 7
0
        public virtual void GrabContext(SynkContext context)
        {
            if (!initialized)
            {
                initialized = true;
                log.Info("Initializing " + this.localPath + " site");

                var assembly = this.GetType().Assembly;
                var types    = assembly.GetTypes();
                var entities = new List <Entity>();

                entities.Add(new Model.File());
                entities.Add(new Model.Enum());
                entities.Add(new Model.User());

                foreach (var type in types)
                {
                    if (type.IsSubclassOf(typeof(Entity)))
                    {
                        var entity = (Entity)Activator.CreateInstance(type);
                        entities.Add(entity);
                    }
                }

                foreach (var entity in entities)
                {
                    entity.InitFromContext(context);
                    if (GenerateTableForEntity(context, entity))
                    {
                        log.Info("Generated table: " + entity.tableName);
                    }
                }

                var userTotal = context.database.GetEntityCount <User>();
                if (userTotal == 0)
                {
                    var user = context.database.CreateEntity <User>();
                    user.InitFromContext(context);
                    context.config.InitFirstUser(context, user);

                    user.Save(context);

                    bool instanced = config.GetFieldBool("instanced");
                    if (instanced)
                    {
                        context.database.createDatabase(user.GetFieldValue("database"));
                    }
                }


                context.CreateEnum("country", countryList);
                //this.createEnum("product_type", array('product', 'service', 'other'));
            }
        }
Exemplo n.º 8
0
 public void progress(SynkContext context)
 {
     /*$progressFile = "tmp/".session_id(). ".bar";
      * if (file_exists($progressFile))
      * {
      *      $progress = file_get_contents($progressFile);
      * }
      * else
      * {
      * $progress = '0';
      * }
      *
      * echo $progress;*/
 }
Exemplo n.º 9
0
        public bool Save(SynkContext context, bool useTriggers = true)
        {
            if (!this.isWritable)
            {
                return(false);
            }


            if (useTriggers)
            {
                /*if (!VerifyFields(context))
                 * {
                 *  return false;
                 * }*/

                ApplyGenerators(context);
            }

            var dbFields = this.GetFields();

            if (this.exists)
            {
                context.WriteLog("Saving into " + this.tableName + ": " + this.ToString());

                context.database.saveObject(dbName, tableName, dbFields, "id", this.id.ToString());
            }
            else
            {
                context.WriteLog("Inserting into " + this.tableName + ": " + this.ToString());

                dbFields["insertion_date"] = this.insertion_date.ToString();
                this.id     = context.database.insertObject(dbName, tableName, dbFields);
                this.exists = true;
            }

            if (useTriggers)
            {
                ApplyTriggers(context);
            }

            foreach (var val in this._currentValues)
            {
                context.WriteLog(val.Key + ": " + val.Value);
                _originalValues[val.Key] = val.Value;
            }

            context.site.InvalidateCache(this.className);

            return(true);
        }
Exemplo n.º 10
0
        public T GetFieldAsEntity <T>(SynkContext context, string fieldName) where T : Entity
        {
            var field = FindField(fieldName);

            if (string.IsNullOrEmpty(field.entity))
            {
                return(default(T));
            }

            long ID;

            long.TryParse(GetFieldValue(fieldName), out ID);
            return(context.database.FetchEntityByID <T>(ID));
        }
Exemplo n.º 11
0
        private bool GenerateHash(SynkContext context, Entity obj, Field field)
        {
            var srcName  = field.target;
            var srcValue = obj.GetFieldValue(srcName);

            if (!string.IsNullOrEmpty(srcValue))
            {
                var fieldValue = context.database.getPasswordHash(srcValue);
                obj.SetFieldValue(field.name, fieldValue);
                //var_dump("password :"******" hash: ".$fieldValue);die();
                return(true);
            }

            return(false);
        }
Exemplo n.º 12
0
        /*protected bool VerifyFields(SynkRequest context)
         * {
         *  foreach (var field in _fields.Values)
         *  {
         *      var fieldName = field.name;
         *
         *      if (field.validator != null)
         *      {
         *          if (!field.validator(context, this, field))
         *          {
         *              if (string.IsNullOrEmpty(context.error) && !context.WaitingForConfirmation())
         *              {
         *                  context.error = "Entity field '"+context.Translate("entity_"+this.GetType().Name.ToLower()+"_"+ field.name) +"' failed validation";
         *                  context.WriteLog(context.error);
         *              }
         *              return false;
         *          }
         *      }
         *  }
         *
         *  return true;
         * }*/

        protected void ApplyGenerators(SynkContext context)
        {
            foreach (var field in _fields.Values)
            {
                var fieldName = field.name;

                if (field.autoGenerator != null)
                {
                    if (field.autoGenerator(context, this, field))
                    {
                        context.WriteLog("Generating field " + field.name + " in " + this.ToString());
                    }
                }
            }
        }
Exemplo n.º 13
0
        protected void ApplyTriggers(SynkContext context)
        {
            foreach (var field in _fields.Values)
            {
                if (string.IsNullOrEmpty(field.entity))
                {
                    continue;
                }

                var fieldName  = field.name;
                var fieldValue = GetFieldValue(fieldName);
                var oldValue   = _originalValues[field.name];

                if (!string.IsNullOrEmpty(field.fieldLink) && !fieldValue.Equals(oldValue))
                {
                    var old     = GetRelationshipIDsFromList(oldValue);
                    var current = GetRelationshipIDsFromList(fieldValue);

                    var deleted = old.Except(current);
                    var added   = current.Except(old);

                    //DEBUGGER
                    context.WriteLog("TRIGGER: RELATIONSHIP CHANGES in " + field.name);
                    context.WriteLog("OLD: " + oldValue);
                    context.WriteLog("NEW: " + fieldValue);

                    foreach (var id in deleted)
                    {
                        var other = context.database.FetchEntityByID(field.entity, id);
                        if (other.exists)
                        {
                            other.RemoveRelationship(field.fieldLink, this);
                            other.Save(context, false);
                        }
                    }

                    foreach (var id in added)
                    {
                        var other = context.database.FetchEntityByID(field.entity, id);
                        if (other.exists)
                        {
                            other.AddRelationship(field.fieldLink, this);
                            other.Save(context, false);
                        }
                    }
                }
            }
        }
Exemplo n.º 14
0
        public PermissonMode HasPermissions(SynkContext context)
        {
            if (this.hidden)
            {
                return(PermissonMode.Hidden);
            }

            if (permissionDelegate != null)
            {
                return(permissionDelegate(context));
            }
            else
            {
                return(PermissonMode.Writable);
            }
        }
Exemplo n.º 15
0
        public bool Remove(SynkContext context)
        {
            if (!this.exists)
            {
                return(false);
            }

            var cond = Condition.Equal("id", this.id.ToString());

            context.database.deleteAll(dbName, tableName, cond);

            this.exists = false;
            this.id     = 0;

            return(true);
        }
Exemplo n.º 16
0
        public virtual List <Entity> Search(SynkContext context, string entityClass, string term)
        {
            List <Entity> entities = null;
            var           template = context.database.CreateEntity(entityClass);

            if (template != null)
            {
                var cond = template.GetSearch(term);

                if (cond != null)
                {
                    entities = context.database.FetchAllEntities(entityClass, cond);
                }
            }
            return(entities);
        }
Exemplo n.º 17
0
        public MySQLDatabase(SynkContext context) : base(context)
        {
            var server   = context.config.GetFieldValue("sqlHost");
            var user     = context.config.GetFieldValue("sqlUser");
            var password = context.config.GetFieldValue("sqlPass");

            var connectionString = "SERVER=" + server + ";" + "UID=" + user + ";" + "PASSWORD="******"; CharSet=utf8;";

            connection = new MySqlConnection(connectionString);

            try
            {
                connection.Open();
            }
            catch (MySqlException e)
            {
                this.fail("Unable to connect to db: " + e.Message);
                return;
            }
        }
Exemplo n.º 18
0
        public bool render(SynkContext context)
        {
            var viewFile = context.targetView;
            var viewPath = context.currentModule.name + "/" + viewFile;

            if (!System.IO.File.Exists("views/" + viewPath + ".html"))
            {
                viewPath = "common/" + viewFile;


                if (!System.IO.File.Exists("views/" + viewPath + ".html"))
                {
                    context.kill("Could not load view '" + context.targetView + "' for " + context.currentModule.name);
                    return(false);
                }
            }

            context.PushTemplate(viewPath);
            context.Render();
            return(true);
        }
Exemplo n.º 19
0
 public virtual void OnInvalidAction(SynkContext context, string action)
 {
     context.kill("Invalid action " + action + " on module " + this.name);
 }
Exemplo n.º 20
0
 public Database(SynkContext context)
 {
     this.context = context;
 }
Exemplo n.º 21
0
 public virtual void beforeRender(SynkContext context)
 {
 }
Exemplo n.º 22
0
 public virtual void afterRender(SynkContext context, string layoutTemplate)
 {
 }
Exemplo n.º 23
0
 public string translateField(SynkContext context, Field field)
 {
     return(context.Translate("entity_" + this.className.ToLower() + "_" + field.name));
 }
Exemplo n.º 24
0
 public virtual bool CheckPermissions(SynkContext context, User user, string action)
 {
     return(true);
 }
Exemplo n.º 25
0
 public string getTitle(SynkContext context)
 {
     return(context.Translate("module_" + this.name));
 }
Exemplo n.º 26
0
 public virtual PermissonMode CheckPermissions(SynkContext context, User user)
 {
     return(PermissonMode.Writable);
 }
Exemplo n.º 27
0
        public void Expand(SynkContext context)
        {
            var dbName = context.dbName;

            foreach (var field in _fields.Values)
            {
                var fieldName = field.name;

                if (field.formType.Equals("file"))
                {
                    //
                    long fieldValue;
                    long.TryParse(GetFieldValue(fieldName), out fieldValue);

                    if (fieldValue != 0)
                    {
                        var upload = context.database.FetchEntityByID <File>(fieldValue);
                        //var_dump($upload->thumb); die();

                        SetFieldValue(fieldName + "_source", upload.GetFieldValue("local_name"));
                        SetFieldValue(fieldName + "_thumb", upload.GetFieldValue("thumb"));

                        /*var thumbFile = upload.GetFieldValue("thumb");
                         * var thumbBytes =
                         * var thumbData = Utility.Base64Decode()
                         * SetFieldValue(fieldName+"_thumb", "data:image/png;base64,"+thumbData);*/
                    }
                    else
                    {
                        SetFieldValue(fieldName + "_data", null);
                        SetFieldValue(fieldName + "_thumb", null);
                    }

                    continue;
                }


                if (field.formType.Equals("date"))
                {
                    long fieldValue;
                    long.TryParse(GetFieldValue(fieldName), out fieldValue);

                    var time = fieldValue.ToDateTime();

                    /*if (field.name.Equals("end_date"))
                     * {
                     *  int dur;
                     *  int.TryParse(GetFieldValue("duration"), out dur);
                     *
                     *  long.TryParse(GetFieldValue("start_date"), out fieldValue);
                     *  time = fieldValue.ToDateTime();
                     *
                     *  if (dur>1)
                     *  {
                     *      time = time.AddDays(dur);
                     *  }
                     *  SetFieldValue(fieldName, time.ToTimestamp().ToString());
                     *
                     *  this.Save(context);
                     * }*/

                    SetFieldValue(fieldName + "_year", time.Year.ToString());
                    SetFieldValue(fieldName + "_month", time.Month.ToString());
                    SetFieldValue(fieldName + "_day", time.Day.ToString());

                    var desc = context.Translate("month_" + time.Month.ToString()) + " " + time.Day.ToString("00") + ", " + time.Year.ToString();
                    SetFieldValue(fieldName + "_text", desc);
                    continue;
                }

                if (field.dbType.Contains("varchar") || field.dbType.Contains("text"))
                {
                    var fieldValue = GetFieldValue(fieldName);
                    SetFieldValue(fieldName + "_html", fieldValue.FixNewLines());
                    continue;
                }
            }
        }