Example #1
0
 /**
  * Поменять позиции местами
  *
  * @param int $parentId
  * @param int $nodeId1
  * @param int $nodeId2
  * @throws DatabaseException
  * @throws GraphException
  */
 public function swapPositions($parentId, $nodeId1, $nodeId2)
 {
     $directChilds = $this->edgesGateway->findDirectChildren($parentId);
     if (empty($directChilds)) {
         throw new GraphException('Child nodes not found.');
     }
     $childNodeIds = ArrayHelper::getColumn($directChilds, 'end');
     if (!in_array($nodeId1, $childNodeIds) || !in_array($nodeId2, $childNodeIds)) {
         throw new GraphException('Specified node is not a child.');
     }
     $sourcePosition = ArrayHelper::first(ArrayHelper::find($directChilds, ['end' => $nodeId1]));
     $targetPosition = ArrayHelper::first(ArrayHelper::find($directChilds, ['end' => $nodeId2]));
     $tmpPos = $sourcePosition['pos'];
     $sourcePosition['pos'] = $targetPosition['pos'];
     $targetPosition['pos'] = $tmpPos;
     $this->edgesGateway->updatePositions([$sourcePosition, $targetPosition]);
 }
Example #2
0
 /**
  * Return Fields in rule variables format.
  * Required for rule builder
  *
  * Note: Radios are grouped by its name and buttons are excluded
  *
  * @return array
  */
 public function getRuleVariables()
 {
     $allFields = Json::decode($this->fields, true);
     // Exclude buttons.
     $fields = ArrayHelper::exclude($allFields, ['submit', 'reset', 'image'], 'type');
     // Radios
     $radioFields = ArrayHelper::filter($fields, 'radio', 'type');
     // Group Radios by name
     $radioGroups = ArrayHelper::group($radioFields, 'name');
     $variables = array();
     foreach ($fields as $field) {
         // For Radio Buttons
         // We take the name and label of the group
         // and options item save the value of each radio
         if (isset($field['type']) && $field['type'] === "radio") {
             // Check if the radio group was saved in variables before
             if (isset($field['name']) && isset($variables[$field['name']])) {
                 continue;
             }
             // Get all radios with the same name
             $radios = isset($radioGroups[$field['name']]) ? $radioGroups[$field['name']] : null;
             // Get first radio
             $firstRadio = ArrayHelper::first($radios);
             // Set variable attributes
             $variable = ['name' => $firstRadio['name'], 'label' => $this->getFieldLabel($firstRadio, true), 'fieldType' => 'radio'];
             // Get each radio value, and add to options
             $options = [];
             foreach ($radios as $radio) {
                 $option = ["value" => $radio['value'], "label" => $this->getFieldLabel($radio)];
                 array_push($options, $option);
             }
             if (count($options) > 0) {
                 $variable['options'] = $options;
             }
             // Save the variable, by the name of the radio group
             $variables[$variable['name']] = $variable;
             continue;
             // Skip the rest of the current loop iteration
         }
         $variable = ['name' => $field['id'], 'label' => $this->getFieldLabel($field), 'fieldType' => isset($field['type']) ? $field['type'] : $field['tagName']];
         $options = [];
         if (isset($field['options'])) {
             // Select List has options
             foreach ($field['options'] as $option) {
                 $option = ["value" => $option['value'], "label" => $option['label']];
                 array_push($options, $option);
             }
         }
         if (count($options) > 0) {
             $variable['options'] = $options;
         }
         $variables[$variable['name']] = $variable;
     }
     // Add Form to variables
     $form = ['name' => "form", 'label' => Yii::t('app', "This form"), 'fieldType' => "form"];
     $variables[$form['name']] = $form;
     return array_values($variables);
     // Remove keys
 }