Esempio n. 1
0
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        // inizializzazione
        _cmd = master.check_cmd(qry_val("cmd"));

        // elab requests & cmds
        if (this.IsPostBack)
        {
            return;
        }

        try {
            notes ob = new notes();

            // upload file
            if (json_request.there_file(this))
            {
                json_result res = new json_result(json_result.type_result.ok);

                try {
                    if (Request.Files.Count > 0)
                    {
                        HttpFileCollection files = Request.Files;
                        foreach (string key in files)
                        {
                            HttpPostedFile file    = files[key];
                            string         tp_file = key.Split(new char[] { '_' })[0];
                            if (tp_file == "task-file")
                            {
                                int    task_id = int.Parse(key.Split(new char[] { '_' })[2]);
                                byte[] content;
                                using (var streamReader = new MemoryStream()) {
                                    file.InputStream.CopyTo(streamReader);
                                    content = streamReader.ToArray();
                                }
                                add_att(ob, task_id, Path.GetFileName(file.FileName), content);
                            }
                            else
                            {
                                throw new Exception("tipo file upload '" + tp_file + "' non supportato!");
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("nessun file da caricare!");
                    }
                } catch (Exception ex) { log.log_err(ex); res = new json_result(json_result.type_result.error, ex.Message); }

                write_response(res);

                return;
            }

            // js request
            if (json_request.there_request(this))
            {
                json_result res = new json_result(json_result.type_result.ok);

                try {
                    json_request jr = new json_request(this);

                    // base
                    if (base_elab_action(jr, res))
                    {
                        // fatta
                    }
                    // add_att
                    else if (jr.action == "add_att")
                    {
                        add_att(ob, jr.val_int("task_id"), jr.val_str("name"));
                    }
                    // task_state
                    else if (jr.action == "task_state")
                    {
                        List <free_label> fl = ob.load_free_labels();

                        string folder_path;
                        ob.update_task(jr.val_int("id"), out folder_path, fl, stato: jr.val_str("stato"));
                        List <task_stato> stati = db_conn.dt_table(core.parse_query("menu-states")).Rows.Cast <DataRow>()
                                                  .Select(r => new task_stato(db_provider.str_val(r["stato"]), 0, "", ""
                                                                              , db_provider.str_val(r["title_singolare"]))).ToList();
                        res.html_element = parse_task(ob.load_task(jr.val_int("id")), folder_path, stati);
                    }
                    // set_filter_id
                    else if (jr.action == "set_filter_id")
                    {
                        set_cache_var("active-task-filter", jr.val_str("filter_id"));
                    }
                    // remove_task
                    else if (jr.action == "remove_task")
                    {
                        ob.remove_task(jr.val_int("id"));
                    }
                    // update_task
                    else if (jr.action == "update_task")
                    {
                        List <free_label> fl = ob.load_free_labels();

                        if (!nome_valido(jr.val_str("title")))
                        {
                            throw new Exception("nome '" + jr.val_str("title") + "' non valido!");
                        }

                        string folder_path;
                        ob.update_task(jr.val_int("id"), out folder_path, fl, title: jr.val_str("title"), assegna: jr.val_str("assegna")
                                       , priorita: jr.val_str("priorita"), stima: jr.val_str("stima"), tipo: jr.val_str("tipo"));
                        List <task_stato> stati = db_conn.dt_table(core.parse_query("menu-states")).Rows.Cast <DataRow>()
                                                  .Select(r => new task_stato(db_provider.str_val(r["stato"]), 0, "", ""
                                                                              , db_provider.str_val(r["title_singolare"]))).ToList();
                        res.html_element = parse_task(ob.load_task(jr.val_int("id")), folder_path, stati);
                    }
                    // ren_task
                    else if (jr.action == "ren_task")
                    {
                        if (!nome_valido(jr.val_str("title")))
                        {
                            throw new Exception("nome '" + jr.val_str("title") + "' non valido!");
                        }
                        ob.ren_task(jr.val_int("id"), jr.val_str("title"));
                    }
                    // add_task
                    else if (jr.action == "add_task")
                    {
                        if (!nome_valido(jr.val_str("title")))
                        {
                            throw new Exception("nome '" + jr.val_str("title") + "' non valido!");
                        }

                        ob.add_task(jr.val_int("synch_folder_id"), jr.val_int("folder_id"), jr.val_int_null("search_id"), jr.val_str("stato")
                                    , jr.val_str("title"), jr.val_str("assegna"), jr.val_str("priorita"), jr.val_str("tipo"), jr.val_str("stima"));
                    }
                    // remove_att
                    else if (jr.action == "remove_att")
                    {
                        ob.remove_att(jr.val_int("id"));
                    }
                    // ren_att
                    else if (jr.action == "ren_att")
                    {
                        ob.ren_file(jr.val_int("file_id"), jr.val_str("name"));
                    }
                    // add_folder
                    else if (jr.action == "add_folder")
                    {
                        if (!nome_valido(jr.val_str("title")))
                        {
                            throw new Exception("nome '" + jr.val_str("title") + "' non valido!");
                        }

                        ob.add_folder(jr.val_int("synch_folder_id"), jr.val_int("folder_id"), jr.val_str("title"));
                    }
                    // ren_folder
                    else if (jr.action == "ren_folder")
                    {
                        if (!nome_valido(jr.val_str("title")))
                        {
                            throw new Exception("nome '" + jr.val_str("title") + "' non valido!");
                        }
                        ob.ren_folder(jr.val_int("synch_folder_id"), jr.val_int("folder_id"), jr.val_str("title"));
                    }
                    // del_folder
                    else if (jr.action == "del_folder")
                    {
                        ob.del_folder(jr.val_int("synch_folder_id"), jr.val_int("folder_id"));
                        res.contents = master.url_cmd("tasks");
                    }
                    // cut_element
                    else if (jr.action == "cut_element")
                    {
                        int    f_id = jr.val_int("element_id"), sf_id = jr.val_int("synch_folder_id");
                        string tp = jr.val_str("tp_element"); bool?added = null;
                        if (tp == "folder" || tp == "synch-folder")
                        {
                            if (sf_id > 0 && tp == "synch-folder")
                            {
                                foreach (int id in ob.ids_childs_folders(sf_id))
                                {
                                    added = set_element_cut(id, element_cut.element_cut_type.folder, jr.val_bool("copy")); res.list.Add(id.ToString());
                                }
                            }
                            else
                            {
                                added = set_element_cut(f_id, element_cut.element_cut_type.folder, jr.val_bool("copy")); res.list.Add(f_id.ToString());
                            }
                        }
                        else if (tp == "task")
                        {
                            added = set_element_cut(f_id, element_cut.element_cut_type.task, jr.val_bool("copy"));
                        }
                        else if (tp == "att")
                        {
                            added = set_element_cut(f_id, element_cut.element_cut_type.attachment, jr.val_bool("copy"));
                        }

                        res.set_var("added", added.HasValue ? (added.Value ? "true" : "false") : "none");
                    }
                    // paste_elements
                    else if (jr.action == "paste_elements")
                    {
                        string tp_paste = jr.val_str("tp");
                        int    f_id = jr.val_int("folder_id"), sf_id = jr.val_int("synch_folder_id");
                        bool   paste = false;
                        if (elements_cut.Count > 0)
                        {
                            string err = ""; List <element_cut> ecs = new List <element_cut>();
                            if (tp_paste == "task")
                            {
                                DataRow dr = ob.get_task_info(f_id);
                                if (db_provider.int_val(dr["file_id"]) > 0)
                                {
                                    throw new Exception("questo task non può contenere un allegato!");
                                }
                                string path_task = Path.Combine(db_provider.str_val(dr["synch_local_path"])
                                                                , db_provider.str_val(dr["folder_path"]).Length > 0 ? db_provider.str_val(dr["folder_path"]).Substring(1) : "");
                                foreach (element_cut ec in elements_cut)
                                {
                                    try {
                                        if (ec.tp == element_cut.element_cut_type.attachment)
                                        {
                                            paste = true;
                                            if (!ec.copy)
                                            {
                                                ob.move_file(ec.id, db_provider.int_val(dr["folder_id"]), db_provider.int_val(dr["synch_folder_id"]), path_task);
                                            }
                                            else
                                            {
                                                ob.copy_file(ec.id, db_provider.int_val(dr["folder_id"]), db_provider.int_val(dr["synch_folder_id"]), path_task);
                                            }
                                        }
                                    } catch (Exception ex) {
                                        ecs.Add(ec);
                                        if (err == "")
                                        {
                                            err = ex.Message;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (sf_id <= 0)
                                {
                                    sf_id = ob.get_synch_folder_id(f_id);
                                }
                                foreach (element_cut ec in elements_cut)
                                {
                                    try {
                                        if (ec.tp == element_cut.element_cut_type.folder)
                                        {
                                            ob.move_folder(ec.id, sf_id, f_id > 0 ? f_id : (int?)null); paste = true;
                                        }
                                        else if (ec.tp == element_cut.element_cut_type.task)
                                        {
                                            ob.move_task(ec.id, sf_id, f_id > 0 ? f_id : (int?)null); paste = true;
                                        }
                                    } catch (Exception ex) {
                                        ecs.Add(ec);
                                        if (err == "")
                                        {
                                            err = ex.Message;
                                        }
                                    }
                                }
                            }
                            res.message = err;
                            elements_cut.Clear();
                            foreach (element_cut ec2 in ecs)
                            {
                                elements_cut.Add(ec2);
                            }
                        }

                        if (!paste)
                        {
                            throw new Exception("non è stato incollato nessun elemento!");
                        }
                    }
                    // get_notes
                    else if (jr.action == "get_details")
                    {
                        res.contents = ob.get_task_notes(jr.val_int("task_id"));
                        string html_allegati = "";
                        foreach (DataRow dr in ob.get_task_allegati(jr.val_int("task_id"), jr.val_int_null("search_id")).Rows)
                        {
                            bool cut = there_element_cut(db_provider.int_val(dr["file_id"]), element_cut.element_cut_type.attachment)
                            , found = db_provider.int_val(dr["found_file"]) > 0, file_task = db_provider.int_val(dr["file_task"]) > 0;
                            string style = cut ? "badge-warning" : (found ? "badge-danger" : (file_task ? "badge-primary" : "badge-light"));
                            html_allegati += master.client_key == "" ? core.parse_html_block("task-allegato", new string[, ] {
                                { "file-id", db_provider.str_val(dr["file_id"]) }
                                , { "http-path", db_provider.str_val(dr["http_path"]) }, { "file-name", db_provider.str_val(dr["file_name"]) }, { "style", style }, { "tp_att", style }
                            })
                : core.parse_html_block("task-allegato-client", new string[, ] {
                                { "file-id", db_provider.str_val(dr["file_id"]) }, { "file-name", db_provider.str_val(dr["file_name"]) }, { "style", style }, { "tp_att", style }
                                , { "user-id", ob.user_id.ToString() }, { "user-name", ob.user_name }
                            });
                        }
                        res.html_element = html_allegati != "" ? core.parse_html_block("task-allegati", new string[, ] {
                            { "html-allegati", html_allegati }
                        }) : "";
                    }
                    // save_task_notes
                    else if (jr.action == "save_task_notes")
                    {
                        synch s = ob.get_synch(jr.val_int("user_id"), jr.val_str("user_name"));
                        s.save_task_notes(jr.val_int("task_id"), jr.val_str("text"));
                    }
                    else if (jr.action == "synch_folders")
                    {
                        synch s = ob.get_synch(jr.val_int("user_id"), jr.val_str("user_name"));
                        s.synch_event += s_synch_event;
                        synch_results rf = s.reload_folders(force: true);
                        res.data     = rf;
                        res.contents = _synch_events;
                    }
                } catch (Exception ex) { log.log_err(ex); res = new json_result(json_result.type_result.error, ex.Message); }

                write_response(res);

                return;
            }

            // synch
            if (_cmd != null && _cmd.action == "synch")
            {
                content.InnerHtml = "<p>sincronizzazione cartelle...</p>";
                ClientScript.RegisterStartupScript(GetType(), "__action"
                                                   , "var __action_page = '" + _cmd.action + "';\r\n"
                                                   + "var __user_id = " + this.user.id + ";\r\n"
                                                   + "var __user_name = '" + this.user.name + "';\r\n", true);
            }

            // tasks
            else if (_cmd != null && ((_cmd.action == "view" && _cmd.obj == "tasks") ||
                                      (_cmd.action == "search" && _cmd.obj == "task")))
            {
                int?fi        = qry_val("idt") != "" ? qry_int("idt") : (qry_val("id") != "" ? qry_int("id") : (int?)null)
                , sfi         = qry_val("sft") != "" ? qry_int("sft") : (qry_val("sf") != "" ? qry_int("sf") : (int?)null);
                string search = _cmd.action == "search" ? _cmd.sub_obj() : "";

                // ricerca testo
                int search_cc = 0;
                int?search_id = search != "" ? ob.search_task(search, this.Session.SessionID, out search_cc) : (int?)null;
                search_id_active.Value = search_id.HasValue ? search_id.Value.ToString() : "";

                // filtro attivo
                List <task_filter> tfs = db_conn.dt_table(core.parse_query("filters-tasks")).Rows
                                         .Cast <DataRow>().Select(r => new task_filter(db_provider.int_val(r["task_filter_id"])
                                                                                       , db_provider.str_val(r["filter_title"]), db_provider.str_val(r["filter_notes"])
                                                                                       , db_provider.str_val(r["filter_def"]), db_provider.str_val(r["filter_class"]))).ToList();
                task_filter tf = tfs.FirstOrDefault(x => x.id == int.Parse(get_cache_var("active-task-filter", "1")));
                if (tf == null || search_id.HasValue)
                {
                    tf = new task_filter(0, "elenco completo delle attività", "", "", "");
                }

                ob.load_objects(fi, sfi, tf, search_id);

                menu.InnerHtml  = parse_menu(ob.synch_folders, sfi.HasValue || fi.HasValue, qry_val("cmd"), search_id);
                folder_id.Value = qry_val("id");

                folder       f     = fi.HasValue ? ob.find_folder(fi.Value) : null;
                synch_folder sf    = sfi.HasValue ? ob.find_synch_folder(sfi.Value) : null;
                string       title = f != null ? f.folder_name : (sf != null ? sf.title : "");

                List <task_stato> stati = db_conn.dt_table(core.parse_query("menu-states")).Rows.Cast <DataRow>()
                                          .Select(r => new task_stato(db_provider.str_val(r["stato"]), 0, "", "", db_provider.str_val(r["title_singolare"]))).ToList();

                content.InnerHtml = "<div style='display:none'>virtuale</div>"
                                    + parse_tasks(ob, stati, tf, title, f != null ? ob.find_synch_folder(f.synch_folder_id).title + f.path : "", tfs, search: search_id.HasValue ? search : "");

                ClientScript.RegisterStartupScript(GetType(), "__task_states"
                                                   , "var __action_page = '';\r\n"
                                                   + "var __task_priorita = " + JsonConvert.SerializeObject(db_conn.dt_table(core.parse_query("task-priorita"))) + ";\r\n"
                                                   + "var __task_stime = " + JsonConvert.SerializeObject(db_conn.dt_table(core.parse_query("task-stime"))) + ";\r\n"
                                                   + "var __task_tipi = " + JsonConvert.SerializeObject(db_conn.dt_table(core.parse_query("task-tipi"))) + ";\r\n"
                                                   + "var __task_assegna = " + JsonConvert.SerializeObject(db_conn.dt_table(core.parse_query("task-assegna"))) + ";\r\n"
                                                   + "var __user_id = " + this.user.id + ";\r\n"
                                                   + "var __user_name = '" + this.user.name + "';\r\n", true);
            }
            else
            {
                throw new Exception("COMANDO NON RICONOSCIUTO!");
            }
        } catch (Exception ex) { log.log_err(ex); if (!json_request.there_request(this))
                                 {
                                     master.err_txt(ex.Message);
                                 }
        }
    }