예제 #1
1
function createEmailFromTemplate($templateName, $user, $data = null)
{
    global $APP_DIR;
    $email_config = getConfig("_email");
    if (!$email_config || !$templateName || !$user) {
        return null;
    }
    $templateDir = $email_config["templates"];
    $template = readTextFile("{$APP_DIR}/{$templateDir}/{$templateName}.html");
    if (!$template) {
        return null;
    }
    $name = is_string($user) ? substringBefore($user, "@") : $user["first_name"] . " " . $user["last_name"];
    $to_email = is_string($user) ? $user : $user["email"];
    if (isset($email_config["to"])) {
        $to_email = $email_config["to"];
    }
    $logo = combine($email_config["baseUrl"], getConfig("app.logo"));
    $cfg = array("site" => getConfig("defaultTitle"), "baseUrl" => $email_config["baseUrl"], "logo" => $logo, "name" => $name, "to" => $to_email);
    $template = replaceVariables($template, $cfg);
    $template = replaceVariables($template, $user);
    $template = replaceVariables($template, $data);
    $subject = substringBefore($template, "\n");
    $body = substringAfter($template, "\n");
    //replace classes with inlineStyles
    $styles = readConfigFile("{$APP_DIR}/{$templateDir}/inline.css");
    if ($styles) {
        $body = "<div class=\"fp-email\">{$body}</div>";
        $body = inlineStyles($body, $styles);
    }
    debug("createEmail subject", $subject);
    debug("createEmail body", $body);
    return createEmail($to_email, $subject, $body, true);
}
예제 #2
0
 /**
  * Evaluates the operands for this tree using the operator and the unary
  * operator.
  * 
  * @param wrapStringFunctionResults
  *            Indicates if the results from functions that return strings
  *            should be wrapped in quotes. The quote character used will be
  *            whatever is the current quote character for this object.
  * 
  * @exception EvaluateException
  *                Thrown is an error is encountered while processing the
  *                expression.
  *
  * @return String
  */
 public function evaluate($wrapStringFunctionResults)
 {
     $rtnResult = null;
     // Get the left operand.
     $leftResultString = null;
     $leftResultDouble = null;
     if ($this->leftOperand instanceof ExpressionTree) {
         $leftResultString = $this->leftOperand->evaluate($wrapStringFunctionResults);
         try {
             $leftResultDouble = new Double($leftResultString);
             $leftResultString = null;
         } catch (NumberFormatException $exception) {
             $leftResultDouble = null;
         }
     } else {
         if ($this->leftOperand instanceof ExpressionOperand) {
             $leftExpressionOperand = $this->leftOperand;
             $leftResultString = $leftExpressionOperand->getValue();
             $leftResultString = $this->evaluator->replaceVariables($leftResultString);
             // Check if the operand is a string or not. If it is not a string,
             // then it must be a number.
             if (!$this->evaluator->isExpressionString($leftResultString)) {
                 try {
                     $leftResultDouble = new Double($leftResultString);
                     $leftResultString = null;
                 } catch (NumberFormatException $nfe) {
                     throw new EvaluationException("Expression is invalid.", $nfe);
                 }
                 if ($leftExpressionOperand->getUnaryOperator() != null) {
                     $leftResultDouble = new Double($leftExpressionOperand->getUnaryOperator()->evaluate($leftResultDouble));
                 }
             } else {
                 if ($leftExpressionOperand->getUnaryOperator() != null) {
                     throw new EvaluationException("Invalid operand for unary operator.");
                 }
             }
         } else {
             if ($this->leftOperand instanceof ParsedFunction) {
                 $parsedFunction = $this->leftOperand;
                 $function = $parsedFunction->getFunction();
                 $arguments = $parsedFunction->getArguments();
                 $arguments = $this->evaluator . replaceVariables($arguments);
                 if ($this->evaluator->getProcessNestedFunctions()) {
                     $arguments = $this->evaluator->processNestedFunctions($arguments);
                 }
                 try {
                     $functionResult = $function->execute($this->evaluator, $arguments);
                     $leftResultString = $functionResult->getResult();
                     if ($functionResult->getType() == FunctionConstants::FUNCTION_RESULT_TYPE_NUMERIC) {
                         $resultDouble = new Double($leftResultString);
                         // Process a unary operator if one exists.
                         if ($parsedFunction->getUnaryOperator() != null) {
                             $resultDouble = new Double($parsedFunction->getUnaryOperator()->evaluate($resultDouble . getValue()));
                         }
                         // Get the final result.
                         $leftResultString = $resultDouble . toString();
                     } else {
                         if ($functionResult->getType() == FunctionConstants::FUNCTION_RESULT_TYPE_STRING) {
                             // The result must be a string result.
                             if ($wrapStringFunctionResults) {
                                 $leftResultString = $this->evaluator->getQuoteCharacter() . $leftResultString . $this->evaluator->getQuoteCharacter();
                             }
                             if ($parsedFunction->getUnaryOperator() != null) {
                                 throw new EvaluationException("Invalid operand for unary operator.");
                             }
                         }
                     }
                 } catch (FunctionException $fe) {
                     throw new EvaluationException($fe->getMessage(), $fe);
                 }
                 if (!$this->evaluator->isExpressionString($leftResultString)) {
                     try {
                         $leftResultDouble = new Double($leftResultString);
                         $leftResultString = null;
                     } catch (NumberFormatException $nfe) {
                         throw new EvaluationException("Expression is invalid.", $nfe);
                     }
                 }
             } else {
                 if ($this->leftOperand != null) {
                     throw new EvaluationException("Expression is invalid.");
                 }
             }
         }
     }
     // Get the right operand.
     $rightResultString = null;
     $rightResultDouble = null;
     if ($this->rightOperand instanceof ExpressionTree) {
         $rightResultString = $this->rightOperand->evaluate($wrapStringFunctionResults);
         try {
             $rightResultDouble = new Double($rightResultString);
             $rightResultString = null;
         } catch (NumberFormatException $exception) {
             $rightResultDouble = null;
         }
     } else {
         if ($this->rightOperand instanceof ExpressionOperand) {
             $rightExpressionOperand = $this->rightOperand;
             $rightResultString = $this->rightOperand->getValue();
             $rightResultString = $this->evaluator->replaceVariables($rightResultString);
             // Check if the operand is a string or not. If it not a string,
             // then it must be a number.
             if (!$this->evaluator->isExpressionString($rightResultString)) {
                 try {
                     $rightResultDouble = new Double($rightResultString);
                     $rightResultString = null;
                 } catch (NumberFormatException $nfe) {
                     throw new EvaluationException("Expression is invalid.", $nfe);
                 }
                 if ($rightExpressionOperand->getUnaryOperator() != null) {
                     $rightResultDouble = new Double($rightExpressionOperand->getUnaryOperator()->evaluate($rightResultDouble));
                 }
             } else {
                 if ($rightExpressionOperand->getUnaryOperator() != null) {
                     throw new EvaluationException("Invalid operand for unary operator.");
                 }
             }
         } else {
             if ($this->rightOperand instanceof ParsedFunction) {
                 $parsedFunction = $this->rightOperand;
                 $function = $parsedFunction->getFunction();
                 $arguments = $parsedFunction->getArguments();
                 $arguments = $this->evaluator->replaceVariables($arguments);
                 if ($this->evaluator->getProcessNestedFunctions()) {
                     $arguments = $this->evaluator->processNestedFunctions($arguments);
                 }
                 try {
                     $functionResult = $function->execute($this->evaluator, $arguments);
                     $rightResultString = $functionResult->getResult();
                     if ($functionResult->getType() == FunctionConstants::FUNCTION_RESULT_TYPE_NUMERIC) {
                         $resultDouble = new Double($rightResultString);
                         // Process a unary operator if one exists.
                         if ($parsedFunction->getUnaryOperator() != null) {
                             $resultDouble = new Double($parsedFunction->getUnaryOperator()->evaluate($resultDouble->getValue()));
                         }
                         // Get the final result.
                         $rightResultString = $resultDouble->toString();
                     } else {
                         if ($functionResult->getType() == FunctionConstants::FUNCTION_RESULT_TYPE_STRING) {
                             // The result must be a string result.
                             if ($wrapStringFunctionResults) {
                                 $rightResultString = $this->evaluator->getQuoteCharacter() . $rightResultString . $this->evaluator->getQuoteCharacter();
                             }
                             if ($parsedFunction->getUnaryOperator() != null) {
                                 throw new EvaluationException("Invalid operand for unary operator.");
                             }
                         }
                     }
                 } catch (FunctionException $fe) {
                     throw new EvaluationException($fe . getMessage(), $fe);
                 }
                 if (!$this->evaluator->isExpressionString($rightResultString)) {
                     try {
                         $rightResultDouble = new Double($rightResultString);
                         $rightResultString = null;
                     } catch (NumberFormatException $nfe) {
                         throw new EvaluationException("Expression is invalid.", $nfe);
                     }
                 }
             } else {
                 if ($this->rightOperand == null) {
                     // Do nothing.
                 } else {
                     throw new EvaluationException("Expression is invalid.");
                 }
             }
         }
     }
     // Evaluate the the expression.
     if ($leftResultDouble != null && $rightResultDouble != null) {
         $doubleResult = $this->operator->evaluate($leftResultDouble, $rightResultDouble);
         if ($this->getUnaryOperator() != null) {
             $doubleResult = $this->getUnaryOperator()->evaluate($doubleResult);
         }
         $rtnResult = new Double($doubleResult);
         $rtnResult = $rtnResult->toString();
     } else {
         if ($leftResultString != null && $rightResultString != null) {
             $rtnResult = $this->operator->evaluate($leftResultString, $rightResultString);
         } else {
             if ($leftResultDouble != null && $rightResultDouble == null) {
                 $doubleResult = new Double(-1);
                 if ($this->unaryOperator != null) {
                     $doubleResult = $this->unaryOperator->evaluate($leftResultDouble);
                 } else {
                     // Do not allow numeric (left) and
                     // string (right) to be evaluated together.
                     throw new EvaluationException("Expression is invalid.");
                 }
                 $rtnResult = new Double($doubleResult);
                 $rtnResult = $rtnResult->toString();
             } else {
                 throw new EvaluationException("Expression is invalid.");
             }
         }
     }
     return $rtnResult;
 }