Indefero

Indefero Git Source Tree

Root/src/IDF/Form/Admin/ProjectDelete.php

1<?php
2/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4# ***** BEGIN LICENSE BLOCK *****
5# This file is part of InDefero, an open source project management application.
6# Copyright (C) 2008-2011 Céondo Ltd and contributors.
7#
8# InDefero is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# InDefero is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21#
22# ***** END LICENSE BLOCK ***** */
23
24
25/**
26 * Delete a project.
27 *
28 * It is also removing the SCM files, so handle with care.
29 *
30 */
31class IDF_Form_Admin_ProjectDelete extends Pluf_Form
32{
33 public $project = null;
34
35 public function initFields($extra=array())
36 {
37 $this->project = $extra['project'];
38 $this->user = $extra['user'];
39 $this->fields['code'] = new Pluf_Form_Field_Varchar(
40 array('required' => true,
41 'label' => __('Confirmation code'),
42 'initial' => '',
43 ));
44 $this->fields['agree'] = new Pluf_Form_Field_Boolean(
45 array('required' => true,
46 'label' => __('I have made a backup of all the important data of this project.'),
47 'initial' => '',
48 ));
49 }
50
51 public function clean_code()
52 {
53 $code = $this->cleaned_data['code'];
54 if ($code != $this->getCode()) {
55 throw new Pluf_Form_Invalid(__('The confirmation code does not match. Please provide a valid confirmation code to delete the project.'));
56 }
57 return $code;
58 }
59
60 public function clean_agree()
61 {
62 if (!$this->cleaned_data['agree']) {
63 throw new Pluf_Form_Invalid(__('Sorry, you really need to backup your data before deletion.'));
64 }
65 return $this->cleaned_data['agree'];
66 }
67
68 public function getCode()
69 {
70 return substr(md5(Pluf::f('secret_key').$this->user->id.'.'.$this->project->id),
71 0, 8);
72 }
73
74
75 public function save($commit=true)
76 {
77 if (!$this->isValid()) {
78 throw new Exception(__('Cannot save the model from an invalid form.'));
79 }
80 // So, we drop the project, it will cascade and delete all the
81 // elements of the project. For large projects, this may use
82 // quite some memory.
83 $this->project->delete();
84 return true;
85 }
86}
87
88
89

Archive Download this file