1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
  class ClientForms extends Page { 
    static $db = array(
      'AccessMyAccountTitle' => 'Varchar(255)',
      'AccessMyAccountText' => 'HTMLText',
      'MyAccountTitle' => 'Varchar(255)',
      'MyAccountText' => 'HTMLText',
      "AccountThanksTitle" => "Varchar",
      "AccountThanksContent" => "HTMLText",
      "ErrorTitle" => "Varchar",
      "ErrorContent" => "HTMLText",
    );

    function getCMSFields() {
         $fields = parent::getCMSFields();  
      $fields->addFieldToTab("Root.Content.Main", new TextField("AccessMyAccountTitle", "Title for not logged Users"));
      $fields->addFieldToTab("Root.Content.Main", new HTMLEditorField("AccessMyAccountText", "Content for not Logged Users"));
      $fields->addFieldToTab("Root.Content.Main", new TextField("MyAccountTitle", "'My account' Title"));
      $fields->addFieldToTab("Root.Content.Main", new HTMLEditorField("MyAccountText", "'My account' text"));        

      $fields->addFieldsToTab("Root.Content.Thanks", array(
        new TextField("ThanksTitle", "Title"),
        new HTMLEditorField("ThanksContent", "Content")
      ));

      $fields->addFieldsToTab("Root.Content.Error", array(
        new TextField("ErrorTitle", "Title"),
        new HTMLEditorField("ErrorContent", "Content")    
      ));


      $fields->removeFieldFromTab("Root.Content.Main","Content");
      $fields->removeFieldFromTab("Root.Content.Main","Title");
      $fields->removeFieldFromTab("Root.Content.Main","MenuTitle");      
      return $fields;
    }



    //remember to change the default group ID


    function SignupForm(){
      return new Form($this, "Form", new FieldSet(
        // List your fields here
        new TextField("FirstName", "First name"),
        new TextField("Surname"),
        new EmailField("Email", "Email address")
        ), new FieldSet(
          // List the action buttons here
          new FormAction("SignupAction", "Sign up")
          ), new RequiredFields(
                    "Email", "FirstName"     
          )
        );
    }

    function SignupAction($data, $form) {

      if($member = DataObject::get_one("Member", "`Email` = '". Convert::raw2sql($data['Email']) . "'")) {
          if($member) {
            $form->addErrorMessage("Blurb",
            _t('ForumMemberProfile.EMAILEXISTS','Sorry, that email address already exists. Please choose another.'),
            "bad");

            // Load errors into session and post back
          Session::set("FormInfo.Form_RegistrationForm.data", $data);
            Director::redirectBack();
            return;
          }
      }

      // Create a new Member object and load the form data into it
      $member = new Member();
      $form->saveInto($member);
      // Write it to the database.  This needs to happen before we add it to a group
      $member->write(); 
      $member->login();
      // Add the member to group. (Check if it exists first)

      if($group = DataObject::get_one('Group', "Code = 'clients'")) {
        $member->Groups()->add($group);
        // Redirect to a page thanking people for registering
        Director::redirect($this->Link() . 'thanks');        
      }else{
        // Redirect to a failure page
        Director::redirect($this->Link() . 'error');
      }
    }

    function thanks() {
      return array(
        'Title' => $this->ThanksTitle,
        'Content' => $this->ThanksContent,
        'Form' => ' ',
      );
    }
    function error() {
      return array(
        'Title' => $this->ErrorTitle,
        'Content' => $this->ErrorContent,
        'Form' => ' ',
      );
    }

  }


  class ClientForms_Controller extends Page_Controller{      
    function Form(){
      if($url = Director::urlparam('Form')){
        Session::set('formToLoad', $url); 
      }else{
        Session::clear("formToLoad");                    
      } 
      $var = Session::get('formToLoad');
      if (isset($var)) {
        $fields = singleton('Member')->$var();
        $actions = new FieldSet(new FormAction("SaveFields", "Update my information"));

        $form = new Form($this, "Form", $fields, $actions);
        $form->loadDataFrom(Member::currentUser());        
        return array('Form' => $form);
      }

    }

    function SaveFields($data, $form){ 
      $member = Member::currentUser();
      $form->saveInto($member);
      $member->write();
      Director::redirect('my-account-home/');
      Session::clear("formToLoad");            
    }  


  }      

?>