brokers_cli.php 9.83 KB
<?php

if ( class_exists( 'WP_CLI' ) ) {
    class TZ_BROKER_COMMANDS {

        //collect rows of a csv file and store them in an associative array
        function sync_broker_teams_assignment() {

            $staff_members = [];
            $row = 0;
            if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . "staff_assignment.csv", "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    if($row == 0) {
                        ++$row;
                        continue;
                    }
                    $staff_members[] = ['full_name'=>trim($data[0]),'email'=>trim($data[1]),
                    'phone'=>trim($data[2]).' #'.trim($data[3]),'category'=>strtolower(trim($data[4])),'sub_category'=>strtolower(trim($data[5])),'brokerages'=>trim($data[6])];
                }
            }

            // if(count($staff_members) > 0) {

            //    $assignments = get_fields('options')['assignments'];

               //Gather all the field keys from the ACF system
               $field_keys = [];
               $field_obj = get_field_objects('options')['assignments'];
               foreach($field_obj['sub_fields'] as $fo) {
                $field_keys[] =  ['name'=>$fo['name'],'key'=>$fo['key']];
               }

               global $wpdb;
               $broker_posts = $wpdb->get_results('select post_author,ID from wp_posts where post_type = "broker"',OBJECT_K);

            //    $current_assignment_count = get_option('options_assignments');
                $iter_counter = 0;
               foreach($staff_members as $assign) {

                    foreach($field_keys as $fk) {

                        $option_name = 'options_assignments_'.$iter_counter."_".$fk['name'];
                        $option_value = $assign[$fk['name']];
                        if($option_value == 'all') {
                            $option_value = '';
                        }

                        $option_name_key = '_options_assignments_'.$iter_counter."_".$fk['name'];
                        $option_value_key = $fk['key'];

                        // if($fk['name'] == 'brokerages') {
                        //     var_dump($assign[$fk['name']]);
                        // }
                        
                        if($fk['name'] == 'brokerages' && !empty($option_value)) {
                            $option_value = explode(',',$option_value);

                            $new_option_values = [];

                            foreach($option_value as $ov) {
                                foreach($broker_posts as $key => $val ) {
                                    if($ov == $key) {
                                        $ov = $val->ID;
                                        $new_option_values[] = (int)$val->ID;
                                    }
                                }
                            }

                            var_dump($new_option_values);exit;

                            $option_value = serialize($new_option_values);
                        }

                        update_option($option_name, $option_value);
                        update_option($option_name_key, $option_value_key);
                    }
                    // var_dump( $assign);
                    ++$iter_counter;

               }

            // }

            // syncBrokerListToPosts();
               
        }

        function rebuild_brokers_assigment($args ) {

            // WP_CLI::log(wp_get_upload_dir()['basedir'] . "/" . $args[0]);

            $new_brokers = [];

            $row = 0;
            if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . $args[0], "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    if($row == 0) {
                        ++$row;
                        continue;
                    }
                    $broker_name = $data[0];
                    $broker_id = $data[2];
                    $new_brokers[] = ["broker_name"=>$broker_name,"broker_id"=>$broker_id];
                    ++$row;
                }
                fclose($handle);
            }

            $replacements = [];
            $brokers = getBrokerageList();

            foreach($new_brokers as $nb) {

                $broker_user = get_user_by('login',$nb['broker_name']);

                if($broker_user) {
                    
                    $old_broker_id = get_user_meta($broker_user->ID, 'broker_id', true);
                    $old_brokerage = get_user_meta($broker_user->ID, 'brokerage', true);

                    if(!empty($nb['broker_id']) && $old_broker_id != $nb['broker_id']) {

                        $new_brokerage = null;
                        foreach($brokers as $broker) {
                            if($broker['broker_id'] == $nb['broker_id']) {
                                $new_brokerage = $broker['brokerage'];
                            }
                        }

                        if(!empty($new_brokerage)) {
                            $replacements[] = ['user_id'=>$broker_user->ID,
                                'new_broker_id'=>$nb['broker_id'],'new_brokerage'=>$new_brokerage,
                                'old_broker_id'=>$old_broker_id,'old_brokerage'=>$old_brokerage];
                        }

                    }
                }

            }

            $old_assigns_brokers = get_option('old_broker_replacements');
            if(!$old_assigns_brokers) {
                update_option('old_broker_replacements', serialize($replacements));
            }

            foreach($replacements as $replacer) {

                update_user_meta($replacer['user_id'], 'broker_id', $replacer['new_broker_id']);
                update_user_meta($replacer['user_id'], 'brokerage', $replacer['new_brokerage']);

            }
            


        }

        function rebuild_broker_list_from_csv($args ) {

            // WP_CLI::log(wp_get_upload_dir()['basedir'] . "/" . $args[0]);

            $new_brokers = [];

            $row = 0;
            if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . $args[0], "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    if($row == 0) {
                        ++$row;
                        continue;
                    }
                    $new_id = $data[0];
                    $name = $data[1];
                    $new_brokers[] = ["broker_id"=>$new_id,"brokerage"=>$name];
                    // echo $name;
                    ++$row;
                }
                fclose($handle);
            }

            $old_brokers = getBrokerageList();

            $old_saved_brokers = get_option('old_broker_list');
            if(!$old_saved_brokers) {
                update_option('old_broker_list', serialize($old_brokers));
            }

            update_option('broker_list', serialize($new_brokers));


        }

        private function cleanTerm($str) {
            return trim($str);
        }

        private function cutOffDomain($str) {
            if(strpos($str, 'https://brokers.thecommonwell.ca/wp-content/uploads') !== false) {
                return str_replace( 'https://brokers.thecommonwell.ca/wp-content/uploads','',$str);
            } else {
                return $str;
            }
        }

        private static function setPostData($post_id, $categories, $doc_type) {

           wp_set_post_terms($post_id,$categories, 'media_category');
           wp_set_post_terms($post_id, $doc_type, 'document_type');
    
        }

        function import_media_files( $args ) {

            // WP_CLI::log(wp_get_upload_dir()['basedir'] . "/" . $args[0]);

            $new_media = [];

            $row = 0;
            if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . $args[0], "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    if($row == 0) {
                        ++$row;
                        continue;
                    }
                    $new_media[] = ['title'=>trim($data[0]),'file_name'=>trim($data[1]),'link'=>$this->cutOffDomain(trim($data[2])),
                        'type'=>trim($data[3]),'product_line'=>trim($data[4]),'keywords'=>trim($data[5])];
                    ++$row;
                }
                fclose($handle);
            }
            $document_types = [];
            foreach($new_media as $nm) {

                if(!file_exists(wp_get_upload_dir()['basedir'] . $nm['link'])) {
                    error_log($nm['link'] . " does not exist!");
                } else {

                    global $wpdb;

                    $post_id = $wpdb->get_results('select ID from wp_posts where guid like "%'.$nm['link'].'%"');
                  
                    if($post_id) {
                        $post_id = $post_id[0]->ID;
                        if(!$post_id) {
                            error_log($nm['link'] . " no attachment id!");
                        } else {
                            
                            $doc_type = $nm['type'];
                            $product_line = $nm['product_line'];

                            error_log($nm['link'] . " FOUND!" . $post_id);

                            $this->setPostData($post_id, $product_line, $doc_type);

                            // if(!isset($document_types[$nm['type']])) {
                            //     $document_types[$nm['type']] = 1;
                            // } else {
                            //     $document_types[$nm['type']]++;
                            // }
                            // $this->setPostData($post_id,);
                        }
                    } else {
                        error_log($nm['link'] . " no attachment id!");
                    }



                }

            }

        }

    }
    
    WP_CLI::add_command( 'tz_brokers', 'TZ_BROKER_COMMANDS' );
}