Exemple #1
0
//-----------------------------------------------------------------------------
func (this *GridController) GetEventTypesByEventId() {
	if !sessions.CheckSession(this.Response, this.Request) {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)

		return
	}

	if !this.isAdmin() {
		http.Redirect(this.Response, this.Request, "/", http.StatusForbidden)

		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

		return
	}

	eventId, err := strconv.Atoi(request["event_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

		return
	}

	query := `SELECT event_types.id, event_types.name FROM events_types
        INNER JOIN events ON events.id = events_types.event_id
        INNER JOIN event_types ON event_types.id = events_types.type_id
        WHERE events.id = $1 ORDER BY event_types.id;`
	result := db.Query(query, []interface{}{eventId})

	utils.SendJSReply(map[string]interface{}{"result": "ok", "data": result}, this.Response)
}
Exemple #2
0
func (this *BlankController) GetBlankByRegId() {
	if !sessions.CheckSession(this.Response, this.Request) {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)
		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	regId, err := strconv.Atoi(request["reg_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	blank := new(models.BlankManager).NewPersonalBlank(true).SetRegId(regId)
	result := blank.GetBlank()

	if len(result) == 0 {
		result = blank.SetPersonal(false).GetBlank()
	}

	utils.SendJSReply(
		map[string]interface{}{
			"result": "ok",
			"data":   result,
			"role":   this.isAdmin()},
		this.Response)
}
Exemple #3
0
func (this *GridController) GetSubTable() {
	userId, err := this.CheckSid()
	if err != nil {
		http.Error(this.Response, "Unauthorized", 400)

		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		http.Error(this.Response, err.Error(), 400)

		return
	}

	model := this.GetModel(request["table"].(string))
	index, _ := strconv.Atoi(request["index"].(string))
	subModel := this.GetModel(model.GetSubTable(index))
	subModel.LoadWherePart(map[string]interface{}{model.GetSubField(): request["id"]})

	response, err := json.Marshal(map[string]interface{}{
		"name":     subModel.GetTableName(),
		"caption":  subModel.GetCaption(),
		"colnames": subModel.GetColNames(),
		"columns":  subModel.GetColumns(),
		"colmodel": subModel.GetColModel(this.isAdmin(), userId)})
	if utils.HandleErr("[GridController::GetSubTable] Marshal: ", err, this.Response) {
		return
	}

	fmt.Fprintf(this.Response, "%s", string(response))
}
Exemple #4
0
func (this *BlankController) GetHistoryRequest() {
	userId, err := this.CheckSid()
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": "Unauthorized"}, this.Response)
		return
	}

	data, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	eventId, err := strconv.Atoi(data["event_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	query := `SELECT params.id as param_id, params.name as param_name,
            param_types.name as type, param_values.value, forms.id as form_id
        FROM events
        INNER JOIN events_forms ON events_forms.event_id = events.id
        INNER JOIN forms ON events_forms.form_id = forms.id
        INNER JOIN registrations ON events.id = registrations.event_id
        INNER JOIN faces ON faces.id = registrations.face_id
        INNER JOIN users ON users.id = faces.user_id
        INNER JOIN params ON params.form_id = forms.id
        INNER JOIN param_types ON param_types.id = params.param_type_id
        INNER JOIN param_values ON param_values.param_id = params.id
            AND param_values.reg_id = registrations.id
        WHERE users.id = $1 AND events.id = $2 AND forms.personal = true;`

	utils.SendJSReply(map[string]interface{}{"result": "ok", "data": db.Query(query, []interface{}{userId, eventId})}, this.Response)
}
Exemple #5
0
func (this *BlankController) GetListHistoryEvents() {
	userId, err := this.CheckSid()
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": "Unauthorized"}, this.Response)

		return
	}

	data, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

		return
	}

	ids := map[string]interface{}{"form_id": make([]interface{}, 0)}
	if data["form_ids"] == nil || len(data["form_ids"].([]interface{})) == 0 {
		utils.SendJSReply(map[string]interface{}{"result": "Нет данных о формах анкеты"}, this.Response)

		return
	}

	for _, v := range data["form_ids"].([]interface{}) {
		ids["form_id"] = append(ids["form_id"].([]interface{}), int(v.(float64)))
	}

	eventsForms := this.GetModel("events_forms")
	events := eventsForms.
		LoadWherePart(ids).
		SetCondition(models.OR).
		Select_([]string{"event_id"})

	if len(events) == 0 {
		utils.SendJSReply(map[string]interface{}{"result": "Нет данных"}, this.Response)

		return
	}

	query := `SELECT DISTINCT events.id, events.name FROM events
        INNER JOIN events_forms ON events_forms.event_id = events.id
        INNER JOIN forms ON events_forms.form_id = forms.id
        INNER JOIN registrations ON registrations.event_id = events.id
        INNER JOIN faces ON faces.id = registrations.face_id
        INNER JOIN users ON users.id = faces.user_id
        WHERE users.id=$1 AND events.id IN (`

	var i int
	params := []interface{}{userId}

	for i = 2; i < len(events); i++ {
		query += "$" + strconv.Itoa(i) + ", "
		params = append(params, int(events[i-2].(map[string]interface{})["event_id"].(int)))
	}

	query += "$" + strconv.Itoa(i) + ")"
	params = append(params, int(events[i-2].(map[string]interface{})["event_id"].(int)))

	utils.SendJSReply(map[string]interface{}{"result": "ok", "data": db.Query(query, params)}, this.Response)
}
Exemple #6
0
//-----------------------------------------------------------------------------
func (this *BlankController) EditParams() {
	userId, err := this.CheckSid()
	if err != nil {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)
		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	date := time.Now().Format("2006-01-02T15:04:05Z00:00")

	for _, v := range request["data"].([]interface{}) {
		paramValId, err := strconv.Atoi(v.(map[string]interface{})["param_val_id"].(string))
		if err != nil {
			utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
			return
		}

		query := `SELECT params.name, params.required, params.editable
            FROM params
            INNER JOIN param_values ON param_values.param_id = params.id
            WHERE param_values.id = $1;`
		result := db.Query(query, []interface{}{paramValId})

		name := result[0].(map[string]interface{})["name"].(string)
		required := result[0].(map[string]interface{})["required"].(bool)
		editable := result[0].(map[string]interface{})["editable"].(bool)
		value := v.(map[string]interface{})["value"].(string)

		if required && utils.MatchRegexp("^[ \t\v\r\n\f]{0,}$", value) {
			utils.SendJSReply(map[string]interface{}{"result": "Заполните параметр '" + name + "'"}, this.Response)
			return
		}

		if !this.isAdmin() && !editable {
			continue
		}

		if value == "" {
			value = " "
		}

		params := map[string]interface{}{"value": value, "date": date, "user_id": userId}
		where := map[string]interface{}{"id": paramValId}
		this.GetModel("param_values").Update(this.isAdmin(), userId, params, where)
	}

	utils.SendJSReply(map[string]interface{}{"result": "Изменения сохранены"}, this.Response)
}
Exemple #7
0
func (this *UserController) SendEmailWellcomeToProfile() {
	if !this.isAdmin() {
		http.Redirect(this.Response, this.Request, "/", http.StatusForbidden)
		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if utils.HandleErr("[UserController::SendEmailWellcomeToProfile]: ", err, this.Response) {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	userId, err := strconv.Atoi(request["user_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	query := `SELECT param_values.value
        FROM param_values
        INNER JOIN registrations ON registrations.id = param_values.reg_id
        INNER JOIN params ON params.id = param_values.param_id
        INNER JOIN events ON events.id = registrations.event_id
        INNER JOIN faces ON faces.id = registrations.face_id
        INNER JOIN users ON users.id = faces.user_id
        WHERE params.id in (4, 5, 6, 7) AND users.id = $1 ORDER BY params.id;`
	data := db.Query(query, []interface{}{userId})

	if len(data) < 4 {
		utils.SendJSReply(map[string]interface{}{"result": "Нет регистрационных данных пользователя."}, this.Response)
		return
	}

	to := data[1].(map[string]interface{})["value"].(string) + " "
	to += data[2].(map[string]interface{})["value"].(string) + " "
	to += data[3].(map[string]interface{})["value"].(string)
	email := data[0].(map[string]interface{})["value"].(string)

	token := utils.GetRandSeq(HASH_SIZE)
	if !mailer.SendEmailWellcomeToProfile(to, email, token) {
		utils.SendJSReply(map[string]interface{}{"result": "Проверьте правильность email."}, this.Response)
		return
	}

	params := map[string]interface{}{"token": token, "enabled": true}
	where := map[string]interface{}{"id": userId}
	this.GetModel("users").Update(this.isAdmin(), userId, params, where)

	utils.SendJSReply(map[string]interface{}{"result": "Письмо отправлено"}, this.Response)
}
Exemple #8
0
//-----------------------------------------------------------------------------
func (this *BlankController) GetEditHistoryData() {
	data, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	regId, err := strconv.Atoi(data["reg_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	formType := data["personal"].(string)
	if formType != "true" && formType != "false" {
		utils.SendJSReply(map[string]interface{}{"result": "Invalid form type"}, this.Response)
		return
	}

	query := `SELECT params.id as param_id, forms.id as form_id, p.date as edit_date,
        array_to_string(ARRAY(
            SELECT param_values.value
                FROM events
                INNER JOIN events_forms ON events_forms.event_id = events.id
                INNER JOIN forms ON events_forms.form_id = forms.id
                INNER JOIN registrations ON events.id = registrations.event_id
                INNER JOIN faces ON faces.id = registrations.face_id
                INNER JOIN users ON users.id = faces.user_id
                INNER JOIN params ON params.form_id = forms.id
                INNER JOIN param_types ON param_types.id = params.param_type_id
                INNER JOIN param_values ON param_values.param_id = params.id
                    AND registrations.id = param_values.reg_id
                WHERE (params.id in (5, 6, 7) AND events.id = 1) and users.id = p.user_id
        ), ' ') as login
        FROM events
        INNER JOIN events_forms ON events_forms.event_id = events.id
        INNER JOIN forms ON events_forms.form_id = forms.id
        INNER JOIN registrations ON events.id = registrations.event_id
        INNER JOIN faces ON faces.id = registrations.face_id
        INNER JOIN users ON users.id = faces.user_id
        INNER JOIN params ON params.form_id = forms.id
        INNER JOIN param_types ON param_types.id = params.param_type_id
        INNER JOIN param_values as p ON p.param_id = params.id
            AND p.reg_id = registrations.id
        WHERE registrations.id = $1 AND forms.personal = $2;`

	utils.SendJSReply(map[string]interface{}{"result": "ok", "data": db.Query(query, []interface{}{regId, formType})}, this.Response)
}
Exemple #9
0
func (this *UserController) ResetPassword() {
	userId, err := this.CheckSid()
	if err != nil {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)
		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(err.Error(), this.Response)
		return
	}

	pass := request["pass"].(string)
	if !utils.MatchRegexp("^.{6,36}$", pass) {
		utils.SendJSReply(map[string]interface{}{"result": "badPassword"}, this.Response)
		return
	}

	var id int
	if request["id"] == nil {
		id = userId

	} else {
		id, err = strconv.Atoi(request["id"].(string))
		if utils.HandleErr("[UserController::ResetPassword] strconv.Atoi: ", err, this.Response) {
			utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
			return
		}
	}

	var enabled bool
	salt := strconv.Itoa(int(time.Now().Unix()))
	where := map[string]interface{}{"id": id}

	user := this.GetModel("users")
	user.LoadWherePart(where).
		SelectRow([]string{"enabled"}).
		Scan(&enabled)

	params := map[string]interface{}{"enabled": enabled, "salt": salt, "pass": utils.GetMD5Hash(pass + salt)}
	user.Update(this.isAdmin(), id, params, where)

	utils.SendJSReply(map[string]interface{}{"result": "ok"}, this.Response)
}
Exemple #10
0
func (this *BlankController) GetGroupBlank() {
	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	groupRegId, err := strconv.Atoi(request["group_reg_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	utils.SendJSReply(
		map[string]interface{}{
			"result": "ok",
			"data":   new(models.BlankManager).NewGroupBlank(false).SetGroupRegId(groupRegId).GetTeamBlank()},
		this.Response)
}
Exemple #11
0
func (this *GroupController) IsRegGroup() {
	_, err := this.CheckSid()
	if err != nil {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)
		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
	}

	groupId, err := strconv.Atoi(request["group_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
	}

	addDelFlag := !db.IsExists("group_registrations", []string{"group_id"}, []interface{}{groupId})
	utils.SendJSReply(map[string]interface{}{"result": "ok", "addDelFlag": addDelFlag}, this.Response)
}
func (this *RegistrationController) Login() {
	data, err := utils.ParseJS(this.Request, this.Response)
	if utils.HandleErr("[RegistrationController::Login]: ", err, this.Response) {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

		return
	}

	login := data["login"].(string)
	pass := data["password"].(string)

	var id int
	var enabled bool
	var passHash, salt string
	result := make(map[string]interface{}, 1)

	if err = this.GetModel("users").
		LoadWherePart(map[string]interface{}{"login": login}).
		SelectRow([]string{"id", "pass", "salt", "enabled"}).
		Scan(&id, &passHash, &salt, &enabled); err != nil {
		result["result"] = "invalidCredentials"

	} else if enabled == false {
		result["result"] = "notEnabled"

	} else if passHash != utils.GetMD5Hash(pass+salt) {
		result["result"] = "badPassword"

	} else {
		result["result"] = "ok"

		sid := utils.GetRandSeq(HASH_SIZE)
		params := map[string]interface{}{"sid": sid, "enabled": true}
		where := map[string]interface{}{"id": id}
		this.GetModel("users").Update(this.isAdmin(), id, params, where)
		sessions.SetSession(this.Response, map[string]interface{}{"sid": sid})
	}

	utils.SendJSReply(result, this.Response)
}
Exemple #13
0
//-----------------------------------------------------------------------------
func (this *GridController) JsonToExcel(tableName string) {
	if !sessions.CheckSession(this.Response, this.Request) {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)

		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		http.Error(this.Response, fmt.Sprintf(err.Error()), 400)

		return
	}

	var filters map[string]interface{}
	if request["filters"] == nil {
		filters = nil
	} else {
		filters = request["filters"].(map[string]interface{})
	}

	fields := utils.ArrayInterfaceToString(request["fields"].([]interface{}))
	data := this.GetModel(tableName).
		SetSorting(request["sord"].(string)).
		SetOrder(request["sidx"].(string)).
		Select(fields, filters)

	this.Response.Header().Set("Content-type", "text/csv")
	w := csv.NewWriter(this.Response)

	for _, obj := range data {
		var record []string

		for _, col := range obj.(map[string]interface{}) {
			fmt.Printf("type=%s\n", reflect.TypeOf(col))
			switch col.(type) {
			case int:
				record = append(record, strconv.Itoa(col.(int)))
				break
			case int64:
				record = append(record, strconv.Itoa(int(col.(int64))))
				break
			case string:
				record = append(record, col.(string))
				break
			case bool:
				record = append(record, strconv.FormatBool(col.(bool)))
				break
			case []string:
				record = append(record, col.([]string)[0])
				break
			case time.Time:
				record = append(record, col.(time.Time).Format("2006-01-02 15:04:05 07:00"))
			default:
				panic("JsonToExcel: Unexpected type.")
			}
		}

		w.Write(record)
	}

	w.Flush()
}
Exemple #14
0
func (this *GroupController) AddPerson() {
	userId, err := this.CheckSid()
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": "Unauthorized"}, this.Response)
		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	groupId, err := strconv.Atoi(request["group_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	var groupName string
	db.QueryRow("SELECT name FROM groups WHERE id = $1;", []interface{}{groupId}).Scan(&groupName)

	date := time.Now().Format("2006-01-02T15:04:05Z00:00")
	token := utils.GetRandSeq(HASH_SIZE)
	to, address, headName := "", "", ""

	query := `SELECT param_values.value
        FROM param_values
        INNER JOIN registrations ON registrations.id = param_values.reg_id
        INNER JOIN params ON params.id = param_values.param_id
        INNER JOIN events ON events.id = registrations.event_id
        INNER JOIN faces ON faces.id = registrations.face_id
        INNER JOIN users ON users.id = faces.user_id
        WHERE params.id in (5, 6, 7) AND users.id = $1 AND events.id = 1 ORDER BY params.id;`
	data := db.Query(query, []interface{}{userId})

	if len(data) < 3 {
		utils.SendJSReply(map[string]interface{}{"result": "Данные о руководителе группы отсутсвуют"}, this.Response)
		return

	} else {
		headName = data[0].(map[string]interface{})["value"].(string)
		headName += " " + data[1].(map[string]interface{})["value"].(string)
		headName += " " + data[2].(map[string]interface{})["value"].(string)
	}

	var faceId int
	this.GetModel("faces").QueryInsert("RETURNING id").Scan(&faceId)

	this.GetModel("persons").
		LoadModelData(map[string]interface{}{"face_id": faceId, "group_id": groupId, "status": false, "token": token}).
		QueryInsert("").
		Scan()

	var regId int
	this.GetModel("registrations").
		LoadModelData(map[string]interface{}{"face_id": faceId, "event_id": 1, "status": false}).
		QueryInsert("RETURNING id").
		Scan(&regId)

	var paramValueIds []string

	for _, element := range request["data"].([]interface{}) {
		paramId, err := strconv.Atoi(element.(map[string]interface{})["id"].(string))
		if err != nil {
			log.Println(err.Error())
			continue
		}

		query := `SELECT params.name FROM params WHERE params.id = $1;`
		res := db.Query(query, []interface{}{paramId})

		name := res[0].(map[string]interface{})["name"].(string)
		value := element.(map[string]interface{})["value"].(string)

		if utils.MatchRegexp("^[ \t\v\r\n\f]{0,}$", value) {
			db.QueryDeleteByIds("param_vals", strings.Join(paramValueIds, ", "))
			db.QueryDeleteByIds("registrations", strconv.Itoa(regId))
			db.QueryDeleteByIds("faces", strconv.Itoa(faceId))
			utils.SendJSReply(map[string]interface{}{"result": "Заполните параметр '" + name + "'."}, this.Response)
			return
		}

		var paramValId int
		paramValues := this.GetModel("param_values")
		err = paramValues.LoadModelData(map[string]interface{}{
			"param_id": paramId,
			"value":    value,
			"date":     date,
			"user_id":  userId,
			"reg_id":   regId}).
			QueryInsert("RETURNING id").
			Scan(&paramValId)
		if err, ok := err.(*pq.Error); ok {
			log.Println(err.Code.Name())
		}

		paramValueIds = append(paramValueIds, strconv.Itoa(paramValId))

		if paramId == 4 {
			address = value
		} else if paramId == 5 || paramId == 6 || paramId == 7 {
			to += value + " "
		}
	}

	if !mailer.InviteToGroup(to, address, token, headName, groupName) {
		utils.SendJSReply(
			map[string]interface{}{
				"result": "Вы указали неправильный email, отправить письмо-приглашенине невозможно"},
			this.Response)
		return
	}

	utils.SendJSReply(map[string]interface{}{"result": "ok"}, this.Response)
}
Exemple #15
0
func (this *GridController) ImportForms() {
	if !sessions.CheckSession(this.Response, this.Request) {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)

		return
	}

	if !this.isAdmin() {
		http.Redirect(this.Response, this.Request, "/", http.StatusForbidden)

		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

		return
	}

	eventId, err := strconv.Atoi(request["event_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

		return
	}

	for _, v := range request["event_types_ids"].([]interface{}) {
		typeId, err := strconv.Atoi(v.(string))
		if err != nil {
			utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

			return
		}

		var lastEventId int
		query := `SELECT events.id FROM events
            INNER JOIN events_types ON events_types.event_id = events.id
            INNER JOIN event_types ON event_types.id = events_types.type_id
            WHERE event_types.id = $1 AND events.id <> $2
            ORDER BY id DESC LIMIT 1;`
		db.QueryRow(query, []interface{}{typeId, eventId}).Scan(&lastEventId)

		query = `SELECT forms.id FROM forms
            INNER JOIN events_forms ON events_forms.form_id = forms.id
            INNER JOIN events ON events.id = events_forms.event_id
            WHERE events.id = $1 ORDER BY forms.id;`
		formsResult := db.Query(query, []interface{}{lastEventId})

		for i := 0; i < len(formsResult); i++ {
			formId := int(formsResult[i].(map[string]interface{})["id"].(int))

			eventsForms := this.GetModel("events_forms")

			var eventFormId int
			if err := eventsForms.
				LoadWherePart(map[string]interface{}{"event_id": eventId, "form_id": formId}).
				SelectRow([]string{"id"}).
				Scan(&eventFormId); err != sql.ErrNoRows {
				continue
			}

			eventsForms.
				LoadModelData(map[string]interface{}{"event_id": eventId, "form_id": formId}).
				QueryInsert("").
				Scan()
		}
	}

	utils.SendJSReply(map[string]interface{}{"result": "ok"}, this.Response)
}
func (this *RegistrationController) EventRegisterAction() {
	var result string
	var regId int

	data, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

		return
	}

	eventId := int(data["event_id"].(float64))

	if eventId == 1 && sessions.CheckSession(this.Response, this.Request) {
		utils.SendJSReply(map[string]interface{}{"result": "authorized"}, this.Response)

		return
	}

	if sessions.CheckSession(this.Response, this.Request) {
		userId, err := this.CheckSid()
		if err != nil {
			utils.SendJSReply(map[string]interface{}{"result": "Unauthorized"}, this.Response)

			return
		}

		var faceId int
		query := `SELECT faces.id FROM faces
            INNER JOIN registrations ON registrations.face_id = faces.id
            INNER JOIN events ON events.id = registrations.event_id
            INNER JOIN users ON users.id = faces.user_id
            WHERE users.id = $1 AND events.id = 1;`
		err = db.QueryRow(query, []interface{}{userId}).Scan(&faceId)

		if err != nil {
			utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

			return
		}

		this.GetModel("registrations").
			LoadModelData(map[string]interface{}{"face_id": faceId, "event_id": eventId, "status": false}).
			QueryInsert("RETURNING id").
			Scan(&regId)

		if err = this.InsertUserParams(userId, regId, data["data"].([]interface{})); err != nil {
			utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

			return
		}

	} else if eventId == 1 {
		userLogin, userPass, email, flag := "", "", "", 0

		for _, element := range data["data"].([]interface{}) {
			paramId, err := strconv.Atoi(element.(map[string]interface{})["id"].(string))
			if err != nil {
				continue
			}

			value := element.(map[string]interface{})["value"].(string)

			if paramId == 1 {
				if utils.MatchRegexp("^[ \t\v\r\n\f]{0,}$", value) {
					utils.SendJSReply(map[string]interface{}{"result": "Заполните параметр 'Логин'."}, this.Response)

					return
				}
				userLogin = value
				flag += 1
				continue

			} else if paramId == 2 || paramId == 3 {
				if utils.MatchRegexp("^[ \t\v\r\n\f]{0,}$", value) {
					utils.SendJSReply(map[string]interface{}{"result": "Заполните параметр 'Пароль/Подтвердите пароль'."}, this.Response)

					return
				}
				userPass = value
				flag += 1
				continue

			} else if paramId == 4 {
				if utils.MatchRegexp("^[ \t\v\r\n\f]{0,}$", value) {
					utils.SendJSReply(map[string]interface{}{"result": "Заполните параметр 'Email'."}, this.Response)

					return
				}
				email = value
				flag += 1
				continue

			} else if flag > 3 {
				break
			}
		}

		result, regId = this.Register(userLogin, userPass, email, "user")
		if result != "ok" && regId == -1 {
			utils.SendJSReply(map[string]interface{}{"result": result}, this.Response)

			return
		}

		query := `SELECT users.id
            FROM users
            INNER JOIN faces ON faces.user_id = users.id
            INNER JOIN registrations ON registrations.face_id = faces.id
            WHERE registrations.id = $1;`
		userId := db.Query(query, []interface{}{regId})[0].(map[string]interface{})["id"].(int)

		err = this.InsertUserParams(userId, regId, data["data"].([]interface{}))
		if err != nil {
			db.QueryDeleteByIds("users", strconv.Itoa(userId))
			utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

			return
		}

	} else {
		utils.SendJSReply(map[string]interface{}{"result": "Unauthorized"}, this.Response)

		return
	}

	utils.SendJSReply(map[string]interface{}{"result": "ok"}, this.Response)
}
Exemple #17
0
func (this *BlankController) GetPersonBlankFromGroup() {
	userId, err := this.CheckSid()
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": "Unauthorized"}, this.Response)
		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	var personalForm bool
	switch request["personal"].(string) {
	case "true":
		personalForm = true
		break
	case "false":
		personalForm = false
		break
	default:
		panic("Invalid bool value")
	}

	faceId, err := strconv.Atoi(request["face_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	groupRegId, err := strconv.Atoi(request["group_reg_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	var regId int

	if faceId == -1 {
		if this.isAdmin() {
			db.QueryRow(db.QueryGetCaptFaceIdAndRegId, []interface{}{groupRegId}).Scan(&faceId, &regId)
		} else {
			if err := this.GetModel("faces").
				LoadWherePart(map[string]interface{}{"user_id": userId}).
				SelectRow([]string{"id"}).
				Scan(&faceId); err != nil {
				utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

				return
			}
			db.QueryRow(db.QueryGetCaptRegIdByGroupRegIdAndFaceId, []interface{}{groupRegId, faceId}).Scan(&regId)
		}
	} else {
		db.QueryRow(db.QueryGetRegIdByGroupRegIdAndFaceId, []interface{}{groupRegId, faceId}).Scan(&regId)
	}

	log.Println("faceId: ", faceId, ", groupRegId: ", groupRegId, ", regId: ", regId, ", formType: ", personalForm)

	blank := new(models.BlankManager).NewGroupBlank(personalForm)
	blank.SetGroupRegId(groupRegId).SetFaceId(faceId)

	utils.SendJSReply(
		map[string]interface{}{
			"result": "ok",
			"data":   blank.GetBlank(),
			"role":   this.isAdmin(),
			"regId":  regId},
		this.Response)
}
Exemple #18
0
func (this *UserController) ConfirmOrRejectPersonRequest() {
	if !sessions.CheckSession(this.Response, this.Request) {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)
		return
	}

	if !this.isAdmin() {
		http.Redirect(this.Response, this.Request, "/", http.StatusForbidden)
		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	eventId, err := strconv.Atoi(request["event_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	regId, err := strconv.Atoi(request["reg_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	query := `SELECT param_values.value, users.id as user_id
        FROM param_values
        INNER JOIN registrations ON registrations.id = param_values.reg_id
        INNER JOIN params ON params.id = param_values.param_id
        INNER JOIN events ON events.id = registrations.event_id
        INNER JOIN faces ON faces.id = registrations.face_id
        INNER JOIN users ON users.id = faces.user_id
        WHERE params.id in (4, 5, 6, 7) AND users.id in (
            SELECT users.id FROM registrations INNER JOIN events ON events.id = registrations.event_id
            INNER JOIN faces ON faces.id = registrations.face_id
            INNER JOIN users ON users.id = faces.user_id
            WHERE registrations.id = $1
        ) ORDER BY params.id;`

	data := db.Query(query, []interface{}{regId})

	if len(data) < 2 {
		utils.SendJSReply(
			map[string]interface{}{"result": "Нет регистрационных данных пользователя"},
			this.Response)
		return
	}

	email := data[0].(map[string]interface{})["value"].(string)

	to := data[1].(map[string]interface{})["value"].(string)
	to += " " + data[2].(map[string]interface{})["value"].(string)
	to += " " + data[3].(map[string]interface{})["value"].(string)

	event := db.Query(
		"SELECT name FROM events WHERE id=$1;",
		[]interface{}{eventId})[0].(map[string]interface{})["name"].(string)

	if request["confirm"].(bool) {
		if eventId == 1 {
			utils.SendJSReply(map[string]interface{}{"result": "Эту заявку нельзя подтвердить письмом"}, this.Response)
		} else {
			if mailer.SendEmailToConfirmRejectPersonRequest(to, email, event, true) {
				utils.SendJSReply(map[string]interface{}{"result": "Письмо с подтверждением заявки отправлено"}, this.Response)
			} else {
				utils.SendJSReply(map[string]interface{}{"result": "Ошибка. Письмо с подтверждением заявки не отправлено"}, this.Response)
			}
		}

	} else {
		if eventId == 1 {
			utils.SendJSReply(map[string]interface{}{"result": "Эту заявку нельзя отклонить письмом"}, this.Response)
		} else {
			query := `DELETE FROM param_values WHERE param_values.reg_id = $1;`
			db.Query(query, []interface{}{regId})

			query = `DELETE FROM registrations WHERE id = $1;`
			db.Query(query, []interface{}{regId})

			if mailer.SendEmailToConfirmRejectPersonRequest(to, email, event, false) {
				utils.SendJSReply(map[string]interface{}{"result": "Письмо с отклонением заявки отправлено"}, this.Response)
			} else {
				utils.SendJSReply(map[string]interface{}{"result": "Ошибка. Письмо с отклонением заявки не отправлено"}, this.Response)
			}
		}
	}
}
Exemple #19
0
func (this *GroupController) Register() {
	userId, err := this.CheckSid()
	if err != nil {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)
		return
	}

	request, err := utils.ParseJS(this.Request, this.Response)
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
	}

	groupId, err := strconv.Atoi(request["group_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
	}

	eventId, err := strconv.Atoi(request["event_id"].(string))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
	}

	var eventName string
	var teamEvent bool
	if err = this.GetModel("events").
		LoadWherePart(map[string]interface{}{"id": eventId}).
		SelectRow([]string{"name", "team"}).
		Scan(&eventName, &teamEvent); err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)
		return
	}

	faceId, groupName := 1, ""
	query := `SELECT groups.face_id, groups.name FROM groups
        INNER JOIN faces ON faces.id = groups.face_id
        INNER JOIN users ON users.id = faces.user_id
        WHERE users.id = $1 AND groups.id = $2;`
	err = db.QueryRow(query, []interface{}{userId, groupId}).Scan(&faceId, &groupName)

	if (err != nil || faceId == 1 || groupName == "") && !this.isAdmin() {
		utils.SendJSReply(map[string]interface{}{"result": "Вы не являетесь владельцем группы"}, this.Response)
		return
	}

	if db.IsExists("group_registrations", []string{"group_id", "event_id"}, []interface{}{groupId, eventId}) {
		utils.SendJSReply(map[string]interface{}{"result": "Группа уже зарегистрированна в этом мероприятии"}, this.Response)
		return
	}

	var groupregId int
	this.GetModel("group_registrations").
		LoadModelData(map[string]interface{}{"event_id": eventId, "group_id": groupId, "status": false}).
		QueryInsert("RETURNING id").
		Scan(&groupregId)

	query = `SELECT persons.status, persons.group_id, f.id as face_id,
        array_to_string(
        array(
            SELECT param_values.value
            FROM param_values
            INNER JOIN registrations ON registrations.id = param_values.reg_id
            INNER JOIN faces ON faces.id = registrations.face_id
            INNER JOIN events ON events.id = registrations.event_id
            INNER JOIN params ON params.id = param_values.param_id
            WHERE param_values.param_id IN (5, 6, 7) AND events.id = 1 AND faces.id = f.id ORDER BY param_values.param_id
        ), ' ') as name,

        (SELECT param_values.value
            FROM param_values
            INNER JOIN registrations ON registrations.id = param_values.reg_id
            INNER JOIN faces ON faces.id = registrations.face_id
            INNER JOIN events ON events.id = registrations.event_id
            INNER JOIN params ON params.id = param_values.param_id
            WHERE param_values.param_id = 4 AND events.id = 1 AND faces.id = f.id
        ) as email

        FROM persons
        INNER JOIN groups ON groups.id = persons.group_id
        INNER JOIN faces as f ON f.id = persons.face_id
        WHERE groups.id = $1;`
	data := db.Query(query, []interface{}{groupId})

	query = `SELECT params.id FROM events_forms
        INNER JOIN events ON events.id = events_forms.event_id
        INNER JOIN forms ON forms.id = events_forms.form_id
        INNER JOIN params ON forms.id = params.form_id
        WHERE events.id = $1 AND forms.personal = true ORDER BY forms.id;`
	params := db.Query(query, []interface{}{eventId})

	date := time.Now().Format("20060102T15:04:05Z00:00")

	for _, v := range data {
		status := v.(map[string]interface{})["status"].(bool)
		personFaceId := v.(map[string]interface{})["face_id"].(int)
		var personUserId int
		this.GetModel("faces").
			LoadWherePart(map[string]interface{}{"id": personFaceId}).
			SelectRow([]string{"user_id"}).
			Scan(&personUserId)

		if !status {
			continue
		}

		regId := this.regExists(personUserId, eventId)
		if regId == -1 {
			this.GetModel("registrations").
				LoadModelData(map[string]interface{}{
					"face_id":  personFaceId,
					"event_id": eventId,
					"status":   false}).
				QueryInsert("RETURNING id").
				Scan(&regId)

			for _, elem := range params {
				paramId := int(elem.(map[string]interface{})["id"].(int))
				this.GetModel("param_values").
					LoadModelData(map[string]interface{}{
						"param_id": paramId,
						"value":    " ",
						"date":     date,
						"user_id":  userId,
						"reg_id":   regId}).
					QueryInsert("").
					Scan()
			}
		}

		this.GetModel("regs_groupregs").
			LoadModelData(map[string]interface{}{"groupreg_id": groupregId, "reg_id": regId}).
			QueryInsert("").
			Scan()

		to := v.(map[string]interface{})["name"].(string)
		address := v.(map[string]interface{})["email"].(string)
		if !mailer.AttendAnEvent(to, address, eventName, groupName) {
			utils.SendJSReply(map[string]interface{}{"result": "Ошибка. Письмо с уведомлением не отправлено."}, this.Response)
		}
	}

	if teamEvent == true {
		query = `SELECT params.id FROM events_forms
            INNER JOIN events ON events.id = events_forms.event_id
            INNER JOIN forms ON forms.id = events_forms.form_id
            INNER JOIN params ON forms.id = params.form_id
            WHERE events.id = $1 AND forms.personal = false ORDER BY forms.id;`
		params := db.Query(query, []interface{}{eventId})

		var regId int
		this.GetModel("registrations").
			LoadModelData(map[string]interface{}{"face_id": faceId, "event_id": eventId, "status": false}).
			QueryInsert("RETURNING id").
			Scan(&regId)

		for _, elem := range params {
			this.GetModel("param_values").
				LoadModelData(map[string]interface{}{
					"param_id": int(elem.(map[string]interface{})["id"].(int)),
					"value":    " ",
					"date":     date,
					"user_id":  userId,
					"reg_id":   regId}).
				QueryInsert("").
				Scan()
		}

		this.GetModel("regs_groupregs").
			LoadModelData(map[string]interface{}{"groupreg_id": groupregId, "reg_id": regId}).
			QueryInsert("").
			Scan()
	}

	utils.SendJSReply(map[string]interface{}{"result": "ok"}, this.Response)
}