예제 #1
0
 public function add_child($father_node, ApmTree $to_add, $as_first_child = false)
 {
     if ($father_node === false || $father_node === '' || $father_node === null) {
         //$father_node can be 0!
         $father_node = $this->get_root();
     }
     if (array_key_exists($father_node, $this->tree)) {
         if ($as_first_child) {
             array_unshift($this->tree[$father_node], $to_add->get_root());
         } else {
             $this->tree[$father_node][] = $to_add->get_root();
         }
     } else {
         $this->tree = $this->tree + array($father_node => array($to_add->get_root()));
     }
     if (!$to_add->is_simple_node()) {
         $this->tree = $this->tree + $to_add->get_tree();
     }
 }
예제 #2
0
 /**
  * Synchronizes our separate arbo with WP "real" pages
  * !!Careful!! $this->nodes_data must be loaded before calling this!
  */
 public function synchronize_tree_with_wp_entities($synchronisation_data = array(), $synchronize_only_my_children = array())
 {
     global $wpdb;
     if (empty($synchronisation_data) && empty($synchronize_only_my_children)) {
         //Synchronize all tree!
         $tree = $this->apm_tree->get_tree();
         //To avoid calling $this->nodes_data->get([node])->wp_id each time:
         $wp_ids = $this->nodes_data->get_wp_ids_from_apm_ids($this->apm_tree->get_nodes_flat(self::root_id));
         foreach ($tree as $parent => $children) {
             $parent_wp_id = $wp_ids[$parent];
             $children_wp_ids = array();
             foreach ($children as $child) {
                 $children_wp_ids[] = $wp_ids[$child];
             }
             if (!empty($children_wp_ids)) {
                 //We want to set order too... so we have to make one request per child!!
                 //$sql = "UPDATE ". $wpdb->posts ." SET post_parent='$parent_wp_id' WHERE ID IN ('". implode("','",$children_wp_ids) ."')";
                 //$results = $wpdb->query($sql);
                 $cpt = 0;
                 foreach ($children_wp_ids as $child_wp_id) {
                     $sql = "UPDATE " . $wpdb->posts . " SET post_parent='{$parent_wp_id}', menu_order='{$cpt}' WHERE ID='{$child_wp_id}'";
                     $results = $wpdb->query($sql);
                     $cpt++;
                 }
             }
         }
     } else {
         if (!empty($synchronisation_data)) {
             //Optimized synchronisation :
             $wp_ids = $this->nodes_data->get_wp_ids_from_apm_ids($synchronisation_data['all_apm_ids']);
             foreach ($synchronisation_data['actions'] as $apm_id => $actions) {
                 $set = array();
                 $ok_to_update = true;
                 foreach ($actions as $action => $value) {
                     switch ($action) {
                         case 'order':
                             if ($value === '--') {
                                 $set[] = "menu_order=menu_order-1";
                             } elseif ($value === '++') {
                                 $set[] = "menu_order=menu_order+1";
                             } else {
                                 $set[] = "menu_order='{$value}'";
                             }
                             break;
                         case 'parent':
                             if (array_key_exists($value, $wp_ids)) {
                                 $set[] = "post_parent='" . $wp_ids[$value] . "'";
                             } else {
                                 $ok_to_update = false;
                             }
                             break;
                     }
                 }
                 $sql_set = implode(", ", $set);
                 if ($ok_to_update && array_key_exists($apm_id, $wp_ids)) {
                     $sql = "UPDATE " . $wpdb->posts . " SET {$sql_set} WHERE ID='" . $wp_ids[$apm_id] . "'";
                     $results = $wpdb->query($sql);
                 }
             }
         } else {
             //Synchronise only direct children of the fathers nodes passed in $synchronize_only_my_children:
             //When comming from an editing action, $synchronize_only_my_children contains 1 or 2 items.
             foreach ($synchronize_only_my_children as $father) {
                 $children = $this->apm_tree->get_children($father);
                 //To avoid calling $this->nodes_data->get([node])->wp_id each time:
                 $wp_ids = $this->nodes_data->get_wp_ids_from_apm_ids(array_merge($children, array($father)));
                 $cpt = 0;
                 foreach ($children as $child) {
                     $sql = "UPDATE " . $wpdb->posts . " SET post_parent='" . $wp_ids[$father] . "', menu_order='{$cpt}' WHERE ID='" . $wp_ids[$child] . "'";
                     $results = $wpdb->query($sql);
                     $cpt++;
                 }
             }
         }
     }
 }