public function executeCreate(sfWebRequest $request)
 {
     global $CFG;
     $CFG->current_app->requireMahara();
     $current_user = $CFG->current_app->getCurrentUser();
     if (!$current_user->getRoleManager()->HasPrivilege('GCUser')) {
         $CFG->current_app->gcError("Unauthorized attempt to access eschool/create.", 'gcpageaccessdenied');
     }
     $this->formErrors = array();
     $this->eschoolForm = new GcrEschoolForm();
     $form = $request->getPostParameters();
     // validate form values
     $this->validateShortName($form['short_name']);
     if (!GcrEschoolTypeTable::validateEschoolType($form['eschool_type'])) {
         $this->formErrors['eschool_type'] = 'eSchool type is invalid.';
     }
     // make an address object to insert in to the address table for the address submitted
     $addressObject = new GcrAddress();
     $addressObject->setStreet1($form['street1']);
     $addressObject->setStreet2($form['street2']);
     $addressObject->setCity($form['city']);
     $addressObject->setState($form['state']);
     $addressObject->setCountry($form['country']);
     $addressObject->setZipcode($form['zipcode']);
     // Try to add the eschool's address to the address table
     $addressObject->save();
     // make a person object to insert in to the person table for contact 1
     $personObject = new GcrPerson();
     $personObject->setFirstName($form['first_name']);
     $personObject->setLastName($form['last_name']);
     $personObject->setAddress($addressObject->getId());
     $personObject->setPhone1($form['phone1']);
     $personObject->setPhone2($form['phone2']);
     $personObject->setEmail($form['email']);
     // Try to add the contact 1 to the person table
     $personObject->save();
     // make a person object to insert in to the person table for contact 2
     $person2Object = new GcrPerson();
     $person2Object->setFirstName($form['first_name_2']);
     $person2Object->setLastName($form['last_name_2']);
     $person2Object->setAddress($addressObject->getId());
     $person2Object->setPhone1($form['phone1_2']);
     $person2Object->setPhone2($form['phone2_2']);
     $person2Object->setEmail($form['email_2']);
     // try to add the contact 2 to the person table
     $person2Object->save();
     // make an array of values to validate as the eschool form
     $eschoolFields = array('id' => '', 'full_name' => $form['full_name'], 'short_name' => strtolower($form['short_name']), 'external_url' => $form['external_url'], 'logo' => $CFG->current_app->getLogo(), 'suspended' => '', 'can_sell' => '', 'contact1' => $personObject->getId(), 'contact2' => $person2Object->getId(), 'address' => $addressObject->getId(), 'eschool_type' => $form['eschool_type'], 'eschool_creator' => $CFG->current_app->getId(), 'admin_password' => GcrEschoolTable::generateAdminPassword(), 'password_salt' => GcrEschoolTable::generateRandomString(), 'creation_date' => time(), 'organization_id' => $CFG->current_app->getId(), 'visible' => '1', 'street1' => $form['street1'], 'street2' => $form['street2'], 'city' => $form['city'], 'state' => $form['state'], 'country' => $form['country'], 'zipcode' => $form['zipcode'], 'first_name' => $form['first_name'], 'last_name' => $form['last_name'], 'phone1' => $form['phone1'], 'phone2' => $form['phone2'], 'email' => $form['email'], 'first_name_2' => $form['first_name_2'], 'last_name_2' => $form['last_name_2'], 'phone1_2' => $form['phone1_2'], 'phone2_2' => $form['phone2_2'], 'email_2' => $form['email_2'], '_csrf_token' => $form['_csrf_token']);
     // try to add the eschool data to the eschool table
     if (!($eschoolRecord = $this->processForm($eschoolFields, $this->eschoolForm, $request->getFiles()))) {
         $this->formErrors['eschoolRecord'] = 'Some Field(s) Have Missing or Incorrect Data';
     }
     // If the eschool record was saved, we need to also add a trial record for it
     if (count($this->formErrors) == 0) {
         // create the eschool if everything is valid
         $eschoolRecord->create();
         $this->emailNewEschoolGC($eschoolRecord, $personObject, $person2Object);
         // send user to the newly created eschool
         $this->redirect($eschoolRecord->getUrl());
     } else {
         $addressObject->delete();
         $personObject->delete();
         $person2Object->delete();
         $this->getResponse()->setTitle('Create a Trial eSchool');
         $this->setTemplate('new');
     }
 }
 public function generateNewMhrInstitution($params = array())
 {
     // For some reason, mhr_institution requires an all lowercase alphabetic "name" column
     // fortunately, this column is only used as a primary key, so we can make it anything.
     // Why didn't they just use a normal auto inc integer?
     if (!isset($params['name'])) {
         do {
             $params['name'] = GcrEschoolTable::generateAdminPassword(12, 'abcdfghjkmnpqrstvwxyz', false);
         } while ($this->selectFromMhrTable('institution', 'name', $params['name'], true));
     }
     return $this->insertIntoMhrTable('institution', $params);
 }