예제 #1
0
 /**
  * Retrieves updates on parent and order to apply to WP entities to synchronize WP tree with our tree
  * on edit actions (inserts/delete).
  */
 public function get_synchronisation_data_on_action($action, $node_choice, $index_node, $new_node = 0)
 {
     $synchronisation_data = array('all_apm_ids' => array(), 'actions' => array());
     switch ($action) {
         case 'insert_before':
             $index_node_order = $this->apm_tree->get_node_order($index_node);
             if ($node_choice !== false && $this->apm_tree->are_siblings($node_choice, $index_node)) {
                 if ($this->apm_tree->compare_siblings_order($node_choice, $index_node) > 0) {
                     $synchronisation_data['actions'][$node_choice] = array('order' => $index_node_order - 1);
                     $synchronisation_data['all_apm_ids'][] = $node_choice;
                     $siblings_to_update = $this->apm_tree->get_siblings_interval($node_choice, $index_node, false, false);
                     foreach ($siblings_to_update as $apm_id) {
                         $synchronisation_data['actions'][$apm_id] = array('order' => '--');
                         $synchronisation_data['all_apm_ids'][] = $apm_id;
                     }
                 } else {
                     $synchronisation_data['actions'][$node_choice] = array('order' => $index_node_order);
                     $synchronisation_data['all_apm_ids'][] = $node_choice;
                     $siblings_to_update = $this->apm_tree->get_siblings_interval($index_node, $node_choice, true, false);
                     foreach ($siblings_to_update as $apm_id) {
                         $synchronisation_data['actions'][$apm_id] = array('order' => '++');
                         $synchronisation_data['all_apm_ids'][] = $apm_id;
                     }
                 }
             } else {
                 $index_node_father = $this->apm_tree->get_node_father($index_node);
                 if ($node_choice !== false) {
                     $siblings_to_update = $this->apm_tree->get_siblings_after($node_choice, false);
                     foreach ($siblings_to_update as $apm_id) {
                         $synchronisation_data['actions'][$apm_id] = array('order' => '--');
                         $synchronisation_data['all_apm_ids'][] = $apm_id;
                     }
                     $synchronisation_data['actions'][$node_choice] = array('order' => $index_node_order, 'parent' => $index_node_father);
                     $synchronisation_data['all_apm_ids'][] = $node_choice;
                 } elseif (!empty($new_node)) {
                     $synchronisation_data['actions'][$new_node] = array('order' => $index_node_order, 'parent' => $index_node_father);
                     $synchronisation_data['all_apm_ids'][] = $new_node;
                 }
                 $synchronisation_data['all_apm_ids'][] = $index_node_father;
                 $siblings_to_update = $this->apm_tree->get_siblings_after($index_node, true);
                 foreach ($siblings_to_update as $apm_id) {
                     $synchronisation_data['actions'][$apm_id] = array('order' => '++');
                     $synchronisation_data['all_apm_ids'][] = $apm_id;
                 }
             }
             break;
         case 'insert_after':
             $index_node_order = $this->apm_tree->get_node_order($index_node);
             if ($node_choice !== false && $this->apm_tree->are_siblings($node_choice, $index_node)) {
                 if ($this->apm_tree->compare_siblings_order($node_choice, $index_node) > 0) {
                     $synchronisation_data['actions'][$node_choice] = array('order' => $index_node_order);
                     $synchronisation_data['all_apm_ids'][] = $node_choice;
                     $siblings_to_update = $this->apm_tree->get_siblings_interval($node_choice, $index_node, false, true);
                     foreach ($siblings_to_update as $apm_id) {
                         $synchronisation_data['actions'][$apm_id] = array('order' => '--');
                         $synchronisation_data['all_apm_ids'][] = $apm_id;
                     }
                 } else {
                     $synchronisation_data['actions'][$node_choice] = array('order' => $index_node_order + 1);
                     $synchronisation_data['all_apm_ids'][] = $node_choice;
                     $siblings_to_update = $this->apm_tree->get_siblings_interval($index_node, $node_choice, false, false);
                     foreach ($siblings_to_update as $apm_id) {
                         $synchronisation_data['actions'][$apm_id] = array('order' => '++');
                         $synchronisation_data['all_apm_ids'][] = $apm_id;
                     }
                 }
             } else {
                 $index_node_father = $this->apm_tree->get_node_father($index_node);
                 if ($node_choice !== false) {
                     $siblings_to_update = $this->apm_tree->get_siblings_after($node_choice, false);
                     foreach ($siblings_to_update as $apm_id) {
                         $synchronisation_data['actions'][$apm_id] = array('order' => '--');
                         $synchronisation_data['all_apm_ids'][] = $apm_id;
                     }
                     $synchronisation_data['actions'][$node_choice] = array('order' => $index_node_order + 1, 'parent' => $index_node_father);
                     $synchronisation_data['all_apm_ids'][] = $node_choice;
                 } elseif (!empty($new_node)) {
                     $synchronisation_data['actions'][$new_node] = array('order' => $index_node_order + 1, 'parent' => $index_node_father);
                     $synchronisation_data['all_apm_ids'][] = $new_node;
                 }
                 $synchronisation_data['all_apm_ids'][] = $index_node_father;
                 $siblings_to_update = $this->apm_tree->get_siblings_after($index_node, false);
                 foreach ($siblings_to_update as $apm_id) {
                     $synchronisation_data['actions'][$apm_id] = array('order' => '++');
                     $synchronisation_data['all_apm_ids'][] = $apm_id;
                 }
                 $synchronisation_data['all_apm_ids'][] = $index_node;
             }
             break;
         case 'insert_child':
             $new_order = $this->apm_tree->get_nb_children($index_node);
             if ($node_choice !== false) {
                 if ($this->apm_tree->get_node_father($node_choice) == $index_node) {
                     $new_order--;
                 }
                 $siblings_to_update = $this->apm_tree->get_siblings_after($node_choice, false);
                 foreach ($siblings_to_update as $apm_id) {
                     $synchronisation_data['actions'][$apm_id] = array('order' => '--');
                     $synchronisation_data['all_apm_ids'][] = $apm_id;
                 }
                 $synchronisation_data['actions'][$node_choice] = array('order' => $new_order, 'parent' => $index_node);
                 $synchronisation_data['all_apm_ids'][] = $node_choice;
             } elseif (!empty($new_node)) {
                 $synchronisation_data['actions'][$new_node] = array('order' => $new_order, 'parent' => $index_node);
                 $synchronisation_data['all_apm_ids'][] = $new_node;
             }
             $synchronisation_data['all_apm_ids'][] = $index_node;
             break;
         case 'delete':
             $siblings_to_update = $this->apm_tree->get_siblings_after($node_choice, false);
             foreach ($siblings_to_update as $apm_id) {
                 $synchronisation_data['actions'][$apm_id] = array('order' => '--');
                 $synchronisation_data['all_apm_ids'][] = $apm_id;
             }
             break;
     }
     $synchronisation_data['all_apm_ids'] = array_unique($synchronisation_data['all_apm_ids']);
     return $synchronisation_data;
 }