protected function renderAvailableAttributesContent()
 {
     $modelClassName = $this->model->getModelClassName();
     $model = new $modelClassName(false);
     $adapter = new ModelNumberOrCurrencyAttributesAdapter($model);
     $attributeData = $adapter->getAttributes();
     $title = Zurmo::t('DesignerModule', 'Create a math formula that is calculated from other fields.' . ' Use the Formula Name from the Available Fields grid below to create your formula.' . ' Example formula (field1 * field2) / field3');
     $spanContent = '<span id="formula-tooltip" class="tooltip" title="' . $title . '">?</span>';
     if (count($attributeData) > 0) {
         $content = '<strong>' . Zurmo::t('DesignerModule', 'Available Fields:') . '</strong> ' . $spanContent;
         $content .= '<table id="available-fields">';
         $content .= '<tr><th>' . Zurmo::t('DesignerModule', 'Field Name') . '</th>';
         $content .= '<th>' . Zurmo::t('DesignerModule', 'Formula Name') . '</th></tr>';
         foreach ($attributeData as $attributeName => $data) {
             $content .= '<tr><td>' . $data['attributeLabel'] . '</td><td>' . $attributeName . '</td></tr>';
         }
         $content .= '</table>';
     } else {
         $content = '<span class="error">' . Zurmo::t('DesignerModule', 'There are no fields in this module to be used in a formula.');
         $content .= '</span>';
     }
     $qtip = new ZurmoTip();
     $qtip->addQTip("#formula-tooltip");
     return $content;
 }
 public function testGetAttributes()
 {
     $adapter = new ModelNumberOrCurrencyAttributesAdapter(new TestOperatorTypeModel());
     $attributes = $adapter->getAttributes();
     $this->assertEquals(2, count($attributes));
     $this->assertTrue(isset($attributes['integerStandard']));
     $this->assertTrue(isset($attributes['floatStandard']));
     $adapter = new ModelNumberOrCurrencyAttributesAdapter(new CurrencyValueTestItem());
     $attributes = $adapter->getAttributes();
     $compareData = array();
     $this->assertEquals(1, count($attributes));
     $this->assertTrue(isset($attributes['amount']));
 }
 protected function renderAvailableAttributesContent()
 {
     $modelClassName = $this->model->getModelClassName();
     $model = new $modelClassName(false);
     $adapter = new ModelNumberOrCurrencyAttributesAdapter($model);
     $attributeDataNumerOrCurrency = $adapter->getAttributes();
     $title = Zurmo::t('DesignerModule', 'Create a formula that is evaluated based on other fields. ' . 'The formula can be a math expression calculated from number ' . 'fields, for example, you can use an expression like ' . '(field1 * field2) / field3. The formula can also include an if ' . 'statement, use the IF(condition;trueValue;falseValue) syntax. ' . 'Within the condition and values you can use strings, string fields, ' . 'number fields or math expressions. Strings should be surrounded by ' . '\'. In the condition you can ' . 'use the operators <, >, ==, !=, <= and >=. An example of an if ' . 'statement is IF(field1 == field4;field2/365;0)');
     // Not Coding Standard
     $spanContent = '<span id="formula-tooltip" class="tooltip" title="' . $title . '">?</span>';
     $content = null;
     $adapter = new ModelAttributesAdapter($model);
     $attributeData = $adapter->getAttributes();
     if (count($attributeData) > 0) {
         $content .= '<strong>' . Zurmo::t('DesignerModule', 'Available Fields For Formula:') . '</strong> ';
         $content .= $spanContent;
         $content .= '<table id="available-fields">';
         $content .= '<tr><th>' . Zurmo::t('DesignerModule', 'Field Name') . '</th>';
         $content .= '<th>' . Zurmo::t('DesignerModule', 'Formula Name') . '</th>';
         $content .= '<th>' . Zurmo::t('DesignerModule', 'Can be used in math expression') . '</th></tr>';
         foreach ($attributeData as $attributeName => $data) {
             $content .= '<tr><td>' . $data['attributeLabel'] . '</td>';
             $content .= '<td>' . $attributeName . '</td>';
             $canBeUsedInMathExpression = Zurmo::t('DesignerModule', 'No');
             if (in_array($attributeName, array_keys($attributeDataNumerOrCurrency))) {
                 $canBeUsedInMathExpression = Zurmo::t('DesignerModule', 'Yes');
             }
             $content .= '<td>' . $canBeUsedInMathExpression . '</td></tr>';
         }
         $content .= '</table>';
     } else {
         $content .= '<span class="error">' . Zurmo::t('DesignerModule', 'There are no fields in this module to be used in an expression.');
         $content .= '</span>';
     }
     $qtip = new ZurmoTip();
     $qtip->addQTip("#formula-tooltip");
     return ZurmoHtml::tag('div', array('class' => 'field-instructions'), $content);
 }
 protected static function isExpressionValid($expression, $modelClassName)
 {
     assert('is_string($expression)');
     if (strpos($expression, '"') !== false) {
         return false;
     }
     $expression = trim($expression);
     if (static::isString($expression)) {
         return true;
     }
     if (static::isAttribute($expression, $modelClassName)) {
         return true;
     }
     $model = new $modelClassName(false);
     $adapter = new ModelNumberOrCurrencyAttributesAdapter($model);
     $patterns = array();
     foreach ($adapter->getAttributes() as $attribute => $data) {
         $patterns[] = '/\\b' . $attribute . '\\b/';
     }
     $expression = preg_replace($patterns, '1', $expression);
     if ($expression != strtoupper($expression) || $expression != strtolower($expression)) {
         return false;
     }
     if (static::mathEval($expression) === false) {
         return false;
     }
     return true;
 }
예제 #5
0
 /**
  * Given a formula string and a model, determine if the formula is correctly formed and is using valid
  * attributes from the given model.
  * @param $formula
  * @param ModelNumberOrCurrencyAttributesAdapter $adapter
  * @return bool
  */
 public static function isFormulaValid($formula, ModelNumberOrCurrencyAttributesAdapter $adapter)
 {
     assert('is_string($formula)');
     foreach ($adapter->getAttributes() as $attribute => $data) {
         $formula = str_replace($attribute, 1, $formula);
     }
     if ($formula != strtoupper($formula) || $formula != strtolower($formula)) {
         return false;
     }
     if (static::mathEval($formula) === false) {
         return false;
     }
     return true;
 }