Example #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;
            }
        }
Example #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));
        }
Example #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);
        }
Example #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);
        }
Example #5
0
        public string GetDefaultValue(SynkContext context)
        {
            if (defaultValueCallback != null && context != null)
            {
                return(defaultValueCallback(context));
            }

            return(defaultValue);
        }
Example #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);
        }
Example #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'));
            }
        }
Example #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;*/
 }
Example #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);
        }
Example #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));
        }
Example #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);
        }
Example #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());
                    }
                }
            }
        }
Example #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);
                        }
                    }
                }
            }
        }
Example #14
0
        public PermissonMode HasPermissions(SynkContext context)
        {
            if (this.hidden)
            {
                return(PermissonMode.Hidden);
            }

            if (permissionDelegate != null)
            {
                return(permissionDelegate(context));
            }
            else
            {
                return(PermissonMode.Writable);
            }
        }
Example #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);
        }
Example #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);
        }
Example #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;
            }
        }
Example #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);
        }
Example #19
0
 public virtual void OnInvalidAction(SynkContext context, string action)
 {
     context.kill("Invalid action " + action + " on module " + this.name);
 }
Example #20
0
 public Database(SynkContext context)
 {
     this.context = context;
 }
Example #21
0
 public virtual void beforeRender(SynkContext context)
 {
 }
Example #22
0
 public virtual void afterRender(SynkContext context, string layoutTemplate)
 {
 }
Example #23
0
 public string translateField(SynkContext context, Field field)
 {
     return(context.Translate("entity_" + this.className.ToLower() + "_" + field.name));
 }
Example #24
0
 public virtual bool CheckPermissions(SynkContext context, User user, string action)
 {
     return(true);
 }
Example #25
0
 public string getTitle(SynkContext context)
 {
     return(context.Translate("module_" + this.name));
 }
Example #26
0
 public virtual PermissonMode CheckPermissions(SynkContext context, User user)
 {
     return(PermissonMode.Writable);
 }
Example #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;
                }
            }
        }