diff --git a/bootstrap.php b/bootstrap.php index ce879a5cab316a7cd6f39b994edef2d9546b6c70..b8d9052f2475e58192b9f0e301dbe446e9c52cd0 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -72,6 +72,8 @@ class Bootstrap { define('USER_EMAIL_TABLE',$prefix.'user_email'); define('USER_ACCOUNT_TABLE',$prefix.'user_account'); + define('ORGANIZATION_TABLE', $prefix.'organization'); + define('STAFF_TABLE',$prefix.'staff'); define('TEAM_TABLE',$prefix.'team'); define('TEAM_MEMBER_TABLE',$prefix.'team_member'); diff --git a/include/class.organization.php b/include/class.organization.php new file mode 100644 index 0000000000000000000000000000000000000000..ab70f8fb3ec0fc81e621fedf3c3bd828b06fcad8 --- /dev/null +++ b/include/class.organization.php @@ -0,0 +1,116 @@ +<?php +/********************************************************************* + class.organization.php + + Peter Rotich <peter@osticket.com> + Jared Hancock <jared@osticket.com> + Copyright (c) 2014 osTicket + http://www.osticket.com + + Released under the GNU General Public License WITHOUT ANY WARRANTY. + See LICENSE.TXT for details. + + vim: expandtab sw=4 ts=4 sts=4: +**********************************************************************/ +require_once(INCLUDE_DIR . 'class.orm.php'); + +class OrganizationModel extends VerySimpleModel { + static $meta = array( + 'table' => ORGANIZATION_TABLE, + 'pk' => array('id'), + 'joins' => array( + 'users' => array( + 'reverse' => 'UserAccountModel.org', + ), + ) + ); + + var $users; + + static function objects() { + $qs = parent::objects(); + + return $qs; + } + + function getId() { + return $this->id; + } +} + +class Organization extends OrganizationModel { + + function __construct($ht) { + parent::__construct($ht); + } + + //XXX: Shouldn't getName use magic get method to figure this out? + function getName() { + return $this->name; + } + + function getUpdateDate() { + return $this->updated; + } + + function getCreateDate() { + return $this->created; + } + + function to_json() { + + $info = array( + 'id' => $this->getId(), + 'name' => (string) $this->getName() + ); + + return JsonDataEncoder::encode($info); + } + + + function __toString() { + return (string) $this->getName(); + } + + function delete() { + return parent::delete(); + } + + static function fromVars($vars) { + + if (!($org = Organization::lookup(array('name' => $vars['name'])))) { + $org = Organization::create(array( + 'name' => $vars['name'], + 'created' => new SqlFunction('NOW'), + 'updated' => new SqlFunction('NOW'), + )); + $org->save(true); + } + + return $org; + } + + static function fromForm($form) { + + if(!$form) return null; + + //Validate the form + $valid = true; + if (!$form->isValid()) + $valid = false; + + //Make sure the email is not in-use + if (($field=$form->getField('name')) + && $field->getClean() + && Organization::lookup(array('name' => $field->getClean()))) { + $field->addError('Organization with the same name already exists'); + $valid = false; + } + + return $valid ? self::fromVars($form->getClean()) : null; + } + +} +//Organization::_inspect(); + +?>