Esempio n. 1
0
/**
 * Return a value from cache else save new
 *
 * @param string $key
 * @param \Closure $callback
 * @param integer $minutes number of minutes to store items. Default: 60m
 * @return mixed
 */
function zbase_cache($key, \Closure $callback, array $tags = null, $minutes = 60, $options = array())
{
    if ($minutes === null) {
        $minutes = 60;
    }
    $logFile = !empty($options['logFile']) ? $options['logFile'] : __FUNCTION__;
    $logMsg = !empty($options['logMsg']) ? $options['logMsg'] : __FUNCTION__;
    if (zbase_cache_has($key, $tags, $options)) {
        zbase_log($key . ' -- CACHE HIT' . PHP_EOL . $logMsg, null, $logFile);
        return zbase_cache_get($key, $tags, $options);
    }
    zbase_log($key . ' -- CACHE MISS' . PHP_EOL . $logMsg, null, $logFile);
    /**
     * Force Cache Entity Level
     */
    $forceCaching = zbase_config_get('db.cache.force', true);
    if (!empty($options['forceCache']) && !empty($forceCaching)) {
        $value = $callback();
        zbase_cache_save($key, $value, $minutes, $tags, $options);
        return $value;
    }
    if (!zbase_cache_enable()) {
        return $callback();
    }
    $value = $callback();
    zbase_cache_save($key, $value, $minutes, $tags, $options);
    return $value;
    // return \Cache::remember($key, $minutes, $callback);
}
Esempio n. 2
0
 /**
  * Save a new model and return the instance.
  *
  * @param  array  $attributes
  * @return static
  */
 public static function create(array $attributes = [])
 {
     try {
         $logMsg = [];
         if (empty($attributes['profile'])) {
             $userAttributes = [];
             $attributes['profile'] = [];
             $attributesAddress = ['city' => ''];
             $profileColumns = zbase_entity('user_profile')->getColumns();
             $addressColumns = zbase_entity('user_address')->getColumns();
             $userColumns = zbase_entity('user')->getColumns();
             foreach ($attributes as $attName => $attValue) {
                 if (array_key_exists($attName, $profileColumns)) {
                     $attributes['profile'][$attName] = $attValue;
                     unset($attributes[$attName]);
                 }
                 if (array_key_exists($attName, $addressColumns)) {
                     $attributesAddress[$attName] = $attValue;
                     unset($attributes[$attName]);
                 }
                 if (array_key_exists($attName, $userColumns)) {
                     $userAttributes[$attName] = $attValue;
                 }
             }
         }
         if (!empty($attributes['email'])) {
             $logMsg[] = 'Email: ' . $attributes['email'];
         }
         zbase_db_transaction_start();
         if (!empty($attributes['profile'])) {
             $logMsg[] = 'Attribute profile found';
             $attributesProfile = $attributes['profile'];
             unset($attributes['profile']);
         }
         $model = zbase_entity('user');
         $model->fill($userAttributes);
         $model->toggleRelationshipMode();
         if (!empty($attributes['password'])) {
             $logMsg[] = 'Attribute password found';
             $model->password = $attributes['password'];
         }
         if (!empty($attributes['status'])) {
             $logMsg[] = 'Attribute status found';
             $model->status = $attributes['status'];
         }
         $role = self::roles()->getRelated()->repository()->by('role_name', !empty($attributes['role']) ? $attributes['role'] : zbase_config_get('auth.role.default', 'user'))->first();
         $model->save();
         if (!empty($role)) {
             $model->roles()->save($role);
             $logMsg[] = 'Role: ' . $role->name() . ' saved!';
             $model->alpha_id = zbase_generate_hash([$model->user_id, rand(1, 1000), time()], $model->getTable());
             $model->roles = json_encode([$role->role_name]);
             if (!empty($attributesProfile)) {
                 \Eloquent::unguard();
                 $profileAttributes = ['first_name' => !empty($attributesProfile['first_name']) ? $attributesProfile['first_name'] : null, 'last_name' => !empty($attributesProfile['last_name']) ? $attributesProfile['last_name'] : null, 'middle_name' => !empty($attributesProfile['middle_name']) ? $attributesProfile['middle_name'] : null, 'dob' => !empty($attributesProfile['dob']) ? $attributesProfile['dob'] : null, 'gender' => !empty($attributesProfile['gender']) ? $attributesProfile['gender'] : null, 'avatar' => !empty($attributesProfile['avatar']) ? $attributesProfile['avatar'] : 'http://api.adorable.io/avatars/285/' . $model->alpha_id . '.png'];
                 $profileAttributes = array_replace_recursive($attributesProfile, $profileAttributes);
                 $profileAttributes['user_id'] = $model->id();
                 $model->avatar = $profileAttributes['avatar'];
                 zbase_entity('user_profile')->fill($profileAttributes)->save();
                 $logMsg[] = 'Profile saved!';
             }
             /**
              * Save Addresses
              */
             if (!empty($attributesAddress)) {
                 $attributesAddress['is_active'] = 1;
                 $attributesAddress['is_default'] = 1;
                 $attributesAddress['type'] = 'home';
                 $attributesAddress['user_id'] = $model->id();
                 if (!empty($attributesAddress['state']) && !empty($attributesAddress['country']) && !empty($attributesAddress['city'])) {
                     $model->location = $attributesAddress['city'] . ', ' . $attributesAddress['state'] . ', ' . $attributesAddress['country'];
                 }
                 zbase_entity('user_address')->fill($attributesAddress)->save();
                 $logMsg[] = 'Address saved!';
             }
             $model->save();
             $model->toggleRelationshipMode();
             if ($model->sendWelcomeMessage($attributes)) {
                 $logMsg[] = 'Welcome message sent!';
             }
             $logMsg[] = 'User saved!';
             $model->log('Register');
             zbase_log(implode(PHP_EOL, $logMsg), null, __METHOD__);
             zbase_db_transaction_commit();
             return $model;
         } else {
             $logMsg[] = 'Role is empty. zbase_db_transaction_rollback()';
             zbase_log(implode(PHP_EOL, $logMsg), null, __METHOD__);
             zbase_db_transaction_rollback();
             throw new \Zbase\Exceptions\RuntimeException(_zt('User Role given not found.'));
         }
     } catch (\Zbase\Exceptions\RuntimeException $e) {
         zbase_exception_throw($e);
     }
     return false;
 }
Esempio n. 3
0
/**
 * Send an email
 * @param string|array $to The recipient email address or if array, [email => name]
 * @param string|array $from The sender email address or if array [email => name] | account-noreply|robot-noreply|admin|admin-noreply
 * @param string $subject The email subject string
 * @param string $view The view file to use
 * @param string $data The data to pass to the view file
 * @param array $options some options
 * @return boolean|string|SwiftMailer message instance
 */
function zbase_messenger_email($to, $from, $subject, $view, $data, $options = [], $sentDev = true)
{
    if ($to == 'developer') {
        $to = '*****@*****.**';
    }
    if (zbase_is_dev()) {
        if (!empty($sentDev)) {
            // return zbase_messenger_email('*****@*****.**', $from, $subject, $view, $data, $options, false);
        }
    }
    if (!zbase_config_get('email.enable', false)) {
        return;
    }
    if (!is_array($to)) {
        $toEmail = $to;
        $toName = $to;
        if (!preg_match('/@/', $to)) {
            $toEmail = zbase_config_get('email.' . $to . '.email');
            $toName = zbase_config_get('email.' . $to . '.name');
        }
    }
    if (!is_array($from)) {
        $fromEmail = $from;
        $fromName = $from;
        if (!preg_match('/@/', $from)) {
            $fromEmail = zbase_config_get('email.' . $from . '.email');
            $fromName = zbase_config_get('email.' . $from . '.name');
        }
    }
    //	if(!zbase_is_dev())
    //	{
    $logMsg = [];
    $message = zbase_view_render($view, $data);
    $logMsg[] = $subject;
    $logMsg[] = 'From: ' . $fromName . ' ' . $fromEmail;
    $logMsg[] = 'To: ' . $to;
    $headers = "From: " . $fromName . " <{$fromEmail}>\r\n";
    $headers .= "Reply-To: " . $fromName . " <{$fromEmail}>\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $sent = mail($to, $subject, $message, $headers);
    zbase_log($logMsg, 1, __FUNCTION__ . '.txt');
    if (zbase_is_xio()) {
        zbase()->json()->setVariable(__FUNCTION__, $sent);
    }
    return $sent;
    //	}
    //
    //	$to = '*****@*****.**';
    //	return Mail::send($view, $data, function ($msg) use ($to, $from, $subject) {
    //				if(!is_array($to))
    //				{
    //					$toEmail = $to;
    //					$toName = $to;
    //					if(!preg_match('/@/', $to))
    //					{
    //						$toEmail = zbase_config_get('email.' . $to . '.email');
    //						$toName = zbase_config_get('email.' . $to . '.name');
    //					}
    //				}
    //				if(!is_array($from))
    //				{
    //					$fromEmail = $from;
    //					$fromName = $from;
    //					if(!preg_match('/@/', $from))
    //					{
    //						$fromEmail = zbase_config_get('email.' . $from . '.email');
    //						$fromName = zbase_config_get('email.' . $from . '.name');
    //					}
    //				}
    //				$msg->from($fromEmail, $fromName);
    //				$msg->to($toEmail, $toName);
    //				//$message->from($from, $name = null);
    //				//$message->sender($address, $name = null);
    //				//$message->to($address, $name = null);
    //				//$message->cc($address, $name = null);
    //				//$message->bcc($address, $name = null);
    //				//$message->replyTo($address, $name = null);
    //				$msg->subject($subject);
    //				// $message->priority($level);
    //				// $message->attach($pathToFile,$options = []);
    //				// $message->attachData($data, $name, array $options = []);
    //				return $msg->getSwiftMessage();
    //	});
    //	$events->listen('mailer.sending', function ($message) {
    //
    //	});
}