@ 2014-05-14T11:37:04Z <?php
namespace appointments\controllers;
class Settings extends AuthController
{
public function __construct($params = null)
{
parent::__construct($params);
}
public function index()
{
$biz = \appointments\models\CurrentUser::getInstance()->getBusiness();
// get locations
$locations = $biz->getLocations();
if (! empty($_POST) && $this->isValid()) {
$this->save($biz);
}
// get types
$builder = new \mimi\libs\WhereBuilder();
$builder->add('type', \appointments\models\BusinessTypes::TYPE_BUILTIN)
->add('deleted', \appointments\models\BusinessTypes::DELETED_NO)
->newGroup(\mimi\libs\WhereBuilder::OPERATOR_OR)
->add('added_by', $biz->getOwner()->getId())
->newGroup(\mimi\libs\WhereBuilder::OPERATOR_OR)
->add('id', $biz->type_id);
$types = \appointments\models\BusinessTypes::getAll($builder);
$timezones = new \appointments\models\Timezones();
$uniqueTz = $timezones->getUniques();
if (count($uniqueTz) > 0) {
$_timezones = [];
foreach ($uniqueTz as $offset => $hour) {
$_timezones[] = new \appointments\libs\SelectOption('UTC/GMT ' .$hour, $offset);
}
}
$currenciesModel = new \appointments\models\Currencies();
$currencies = $currenciesModel->getUnique();
\mimi\View::render('business', [
'item' => $biz,
'locations' => $locations,
'timezones' => $_timezones,
'businessTypes' => $types,
'currencies' => $currencies,
]);
}
public function bookings()
{
if (count($_POST) > 0) {
if (isset($_POST['required_fields']) && $this->isValidRequired($_POST['required_fields'])) {
$biz = \appointments\models\CurrentUser::getInstance()->getBusiness();
$settings = new \appointments\libs\Settings();
$bizSettings = new \appointments\models\collections\Settings(
$settings->setOwner($biz)->get()
);
$bizSettings->updateValueByName('required_fields', $_POST['required_fields']);
$bizSettings->save();
}
// additional fields
$this->updateFields($_POST['field']);
\mimi\Http::r('/settings/bookings');
}
// booking additional fields
$biz = \appointments\models\CurrentUser::getInstance()->getBusiness();
$additionalFields = $biz->getAdditionalFields();
\mimi\View::render('business/bookings', [
'additionalFields' => $additionalFields,
]);
}
protected function updateFields($data = array())
{
$ids = array();
if (! empty($data)) {
$ids = \mimi\libs\SimpleArray::extract($data, 'id', true);
}
$biz = \appointments\models\CurrentUser::getInstance()->getBusiness();
$additionalFields = $biz->getAdditionalFields(true);
if (count($ids) > 0) {
$additionalFields->removeMissing($ids);
foreach ($data as $id => $field) {
$item = $additionalFields->getById($id);
if ($item->isLoaded()) {
$item->label = $field['label'];
if ($field['required'] && $field['required'] == 1) {
$item->required = 1;
} else {
$item->required = 0;
}
$item->save();
}
}
} else {
$additionalFields->dropAll();
}
}
protected function isValidRequired($data)
{
$valid = true;
if (empty($data)) {
$valid = false;
\appointments\libs\SessionMessage::create('Please select one of the options for required fields');
}
if (true === $valid) {
$possibleOptions = ['pae', 'poe', 'p', 'e',];
if (! in_array($data, $possibleOptions)) {
$valid = false;
\appointments\libs\SessionMessage::create('Unknown required fields option');
}
}
return $valid;
}
public function hours()
{
$biz = \appointments\models\CurrentUser::getInstance()->getBusiness();
if (! empty($_POST)) {
// working hours
$workingHours = new \appointments\models\WorkingHours();
if ($workingHours->saveAll((int) $biz->id)) {
\mimi\View::addMessage(new \mimi\libs\Message(
'Business working hours saved'
), \mimi\View::MESSAGE_FLASH);
\mimi\Http::r('/settings/hours');
}
}
\mimi\View::render('business/hours', [
'item' => $biz,
]);
}
public function embedding()
{
$biz = \appointments\models\CurrentUser::getInstance()->getBusiness();
$settings['color'] = $biz->getSettingValue('button_settings_color');
$settings['text'] = $biz->getSettingValue('button_settings_text');
$settings['size'] = $biz->getSettingValue('button_settings_size');
$settings['width'] = $biz->getSettingValue('widget_settings_width');
$settings['height'] = $biz->getSettingValue('widget_settings_height');
\mimi\View::render('business/embedding', ['settings' => $settings,]);
}
/**
* @param \appointment\models\Business $biz
*/
protected function save(\appointments\models\Business $biz) {
// basic biz info
$biz->loadFormData();
$biz->owner_id = \appointments\models\CurrentUser::getInstance()->id;
$biz->slug = $this->getCurrentSlug($biz);
// check for user added type
if (isset($_POST['user_type']) && ! empty($_POST['user_type']) && $_POST['type_id'] == -1) {
$bizType = new \appointments\models\BusinessTypes();
$bizType->name = $_POST['user_type'];
$bizType->type = \appointments\models\BusinessTypes::TYPE_USERADDED;
$bizType->added_by = $biz->getOwner()->getId();
$bizType->save();
$biz->type_id = $bizType->getId();
}
$biz->save();
// update business locations
$this->updateLocations($biz);
// update user data
$this->updateUserData();
\appointments\libs\FlashMessage::create('Profile information saved');
\mimi\Http::r('/settings/');
}
/**
* Check if slug changed, if yes - return empty string
* (model's save() method will generate new)
*
* @param \appointments\models\Business $biz
* @return string
*/
protected function getCurrentSlug($biz) {
$__slug = $biz->generateSlug($_POST['name'], true);
if ($biz->getSlug() !== $__slug) {
return '';
}
return $biz->getSlug();
}
/**
* @return boolean
*/
private function updateUserData() {
$updated = false;
if ( ! empty($_POST['new_pass'])) {
$user = \appointments\models\CurrentUser::getInstance()->getUser();
$user->password = $_POST['new_pass'];
$user->save();
// update core user's password
$core = $user->getCoreUser();
$core->password = $user->password;
$core->save();
// switch back to main db
$user->setDbConnection(\appointments\libs\DbSwitcher::getConnection());
$updated = true;
}
return $updated;
}
/**
* @param \appointments\models\Business $biz
* @return boolean
*/
private function updateLogo(\appointments\models\Business $biz) {
$updated = false;
if (isset($_POST['uploadedFile']) && ! empty($_POST['uploadedFile'])) {
// store association
$businessFile = new \appointments\models\BusinessFiles();
$businessFile->getOneByFields([
'business_id' => \appointments\models\CurrentUser::getInstance()->getBusiness()->getId(),
]);
$businessFile->file_id = (int) $_POST['uploadedFile'];
if ( ! $businessFile->isLoaded()) {
$businessFile->business_id = $biz->getId();
}
$businessFile->save();
$updated = true;
}
return $updated;
}
/**
* @param \appointments\models\Business $biz
* @return boolean
*/
private function deleteLogo(\appointments\models\Business $biz) {
$removed = false;
if (isset($_POST['dropLogo']) && (int) $_POST['dropLogo'] === 1) {
$logo = $biz->getLogo();
if ($logo && $logo->isLoaded()) {
$logo->delete();
// drop relation
$businessFile = new \appointments\models\BusinessFiles();
$businessFile->getOneByFields([
'file_id' => $logo->getId(),
'business_id' => $biz->getId(),
]);
if ($businessFile->isLoaded()) {
$businessFile->delete();
$removed = true;
}
}
}
return $removed;
}
protected function updateLocations(\appointments\models\Business $business) {
$ids = [];
$data = $_POST['locations'];
if ( ! empty($data)) {
foreach ($data as $id => $_location) {
if (strstr($id, 'temp')) {
// if this is first address for this business, set it as main
$locationType = \appointments\models\BusinessLocations::TYPE_OTHER;
$businessLocations = new \appointments\models\BusinessLocations();
$locationsCount = $businessLocations->getByFieldsCount([
'business_id' => $business->getId(),
]);
if ( ! $locationsCount) {
$locationType = \appointments\models\BusinessLocations::TYPE_MAIN;
}
// new location
$location = new \appointments\models\Locations();
$location->name = $_location['name'];
$location->address = $_location['address'];
$location->city = $_location['city'];
$location->state = $_location['state'];
$location->postal_code = $_location['postal_code'];
$location->phone = $_location['phone'];
$location->save();
// if this is main location, update existing services with location id
if ($locationType === \appointments\models\BusinessLocations::TYPE_MAIN) {
$services = $business->getServices(true);
if ($services && count($services) > 0) {
/** @todo test if this step is required, technically service location is not required */
$services->updateLocationForAll($location->getId());
}
}
// add relation
$businessLocation = new \appointments\models\BusinessLocations();
$businessLocation->location_id = $location->getId();
$businessLocation->business_id = $business->getId();
$businessLocation->type = $locationType;
$businessLocation->save();
// save location id for later
$ids[] = $location->getId();
} else {
// check location with this id
$location = new \appointments\models\Locations();
$location->load($id);
if ($location->isLoaded() && $location->belongsTo($business)) {
$location->loadWith($_location);
$location->save();
$ids[] = $location->getId();
}
}
}
// check locations remover
if (isset($_POST['location-remover-data']) && ! empty($_POST['location-remover-data'])) {
$locationsRemover = json_decode($_POST['location-remover-data']);
if (count($locationsRemover->removed) > 0) {
$_clients = [];
// get client ids
foreach ($locationsRemover->removed as $locationId) {
$location = new \appointments\models\Locations();
$location->load($locationId);
if ($location->isLoaded()) {
$appointments = $location->getActiveAppointments();
if (false !== $appointments && count($appointments) > 0) {
// get unique client ids
foreach ($appointments as $appointment) {
$_clients[] = (int) $appointment->client_id;
// change status to cancelled
$appointment->cancel();
}
}
}
}
$this->sendAppointmentCancelledNotifications(
$locationsRemover, $_clients);
}
}
if (count($ids) > 0) {
// drop locations not in $ids array
$locations = $business->getLocations();
$this->dropLocations($locations, $ids);
}
}
}
/**
* @param array $locationsRemover
* @param array $_clients
* @return boolean
*/
protected function sendAppointmentCancelledNotifications($locationsRemover, $_clients = array()) {
$sent = false;
if (count($_clients) > 0 && $locationsRemover->type === 'send-delete') {
$clients = array_unique($_clients);
// notify clients
foreach ($clients as $clientId) {
$client = new \appointments\models\Clients();
$client->load($clientId);
if ($client->isLoaded()) {
// notify
$email = new \appointments\models\emails\AppointmentCancelled();
$email->setMessageBody($locationsRemover->message);
$email->send($client);
$sent = true;
}
}
}
return $sent;
}
protected function dropLocations($locations, $ids) {
$removed = false;
if ($locations->isLoaded()) {
foreach ($locations as $location) {
if ( ! in_array($location->getId(), $ids)) {
$location->delete();
$removed = true;
}
}
}
return $removed;
}
/**
* @return boolean
*/
protected function isValid() {
$valid = true;
if (\mimi\App::getInstance()->getVar(\appointments\libs\States::SITE_DISABLED)) {
$valid = false;
\appointments\libs\SessionMessage::create('This account has expired');
}
if (true === $valid) {
$email = filter_input(\INPUT_POST, 'email');
if ( ! empty($email) && ! \filter_var($email, \FILTER_VALIDATE_EMAIL)) {
$valid = false;
\mimi\View::addMessage(new \mimi\libs\Message(
"E-mail address is invalid", \mimi\libs\Message::TYPE_ERROR));
}
}
// validate password change (if there's any)
if (true === $valid) {
$current = filter_input(\INPUT_POST, 'current_pass');
$new = filter_input(\INPUT_POST, 'new_pass');
$repeated = filter_input(\INPUT_POST, 'repeated_pass');
if ( ! empty($current) && ! empty($new)) {
// ok, user is trying to change password; check current
$logged = \appointments\models\CurrentUser::getInstance()->getUser();
if ($logged->getPassword() !== $current) {
$valid = false;
\appointments\libs\SessionMessage::create('Current password is incorrect');
} else if ($new !== $repeated) {
$valid = false;
\appointments\libs\SessionMessage::create(
'New password must be identical with repeated password');
}
}
}
// validate e-mail - is it free or taken (in the system)
if (true === $valid) {
$email = filter_input(\INPUT_POST, 'email');
$loggedUser = \appointments\models\CurrentUser::getInstance()->getUser();
if ($loggedUser->getBusiness()->getEmail() !== $email) {
// email's changed, check if unique
$biz = new \appointments\models\Business();
$biz->getOneByFields(['email' => $email,]);
if ($biz->isLoaded()) {
// sorry, email taken
$valid = false;
\appointments\libs\SessionMessage::create(
'Sorry, but this e-mail address is already taken'
);
}
}
}
return $valid;
}
public function notifications()
{
$biz = \appointments\models\CurrentUser::getInstance()->getBusiness();
if (isset($_POST) && ! empty($_POST)) {
/** @todo settings object with default values for settings? */
if (isset($_POST['settings_list']) && ! empty($_POST['settings_list'])) {
$settingsList = explode(',', $_POST['settings_list']);
}
$biz->updateSettings(($_POST['settings']) ?: [], $settingsList);
}
\mimi\View::render('business/notifications', [
'item' => $biz,
]);
}
public function cancelSubscription()
{
$user = \appointments\models\CurrentUser::getInstance()->getUser();
$user->cancelSubscription();
\appointments\libs\FlashMessage::create('Subscription has been cancelled');
\mimi\Http::r('/settings');
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: Uncaught Error: Class "appointments\controllers\AuthController" not found in /in/CX22i:4
Stack trace:
#0 {main}
thrown in /in/CX22i on line 4
Process exited with code 255 . Output for 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 Fatal error: Uncaught Error: Class 'appointments\controllers\AuthController' not found in /in/CX22i:4
Stack trace:
#0 {main}
thrown in /in/CX22i on line 4
Process exited with code 255 . Output for 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 Fatal error: Class 'appointments\controllers\AuthController' not found in /in/CX22i on line 4
Process exited with code 255 . Output for 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.28 Fatal error: Class 'appointments\controllers\AuthController' not found in /in/CX22i on line 5
Process exited with code 255 . Output for 5.3.0 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/CX22i on line 34
Process exited with code 255 . preferences:dark mode live preview
251.6 ms | 401 KiB | 345 Q