migrations/Version20180906151556.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. use Parsedown;
  7. /**
  8.  * Auto-generated Migration: Please modify to your needs!
  9.  */
  10. final class Version20180906151556 extends AbstractMigration
  11. {
  12.     public function up(Schema $schema): void
  13.     {
  14.         // this up() migration is auto-generated, please modify it to your needs
  15.         $this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
  16.         $stmt $this->connection->prepare('SELECT * FROM config_pdf_field');
  17.         $stmt->execute();
  18.         while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  19.             if (empty($row->mapping)) {
  20.                 continue;
  21.             }
  22.             $mapping unserialize($row->mapping);
  23.             foreach ($mapping as $key => $value) {
  24.                 if (\in_array($value$this->getMergeFields())) {
  25.                     $mapping[$key] = array_search($value$this->getMergeFields());
  26.                 }
  27.             }
  28.             $this->addSql('UPDATE config_pdf_field SET mapping = :mapping WHERE id = :id', [
  29.                 'id' => $row->id,
  30.                 'mapping' => serialize($mapping),
  31.             ]);
  32.         }
  33.         $this->parseString('config_document', ['body''header''footer'], 'toHtml');
  34.         $this->parseString('config_stage', ['message_body'], 'toHtml');
  35.         $this->parseString('config_stage_item', ['message_body'], 'toHtml');
  36.         $this->parseString('customer_letter', ['body'], 'toHtml');
  37.         $this->parseString('customer_message', ['body'], 'toHtml');
  38.         $this->parseString('feed_comment', ['body'], 'toHtml');
  39.         $this->parseString('issue_comment', ['body'], 'toHtml');
  40.         $this->parseString('user', ['signature'], 'toHtml');
  41.     }
  42.     public function down(Schema $schema): void
  43.     {
  44.         // this down() migration is auto-generated, please modify it to your needs
  45.         $this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
  46.         $stmt $this->connection->prepare('SELECT * FROM config_pdf_field');
  47.         $stmt->execute();
  48.         while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  49.             if (empty($row->mapping)) {
  50.                 continue;
  51.             }
  52.             $mapping unserialize($row->mapping);
  53.             foreach ($mapping as $key => $value) {
  54.                 if (\array_key_exists($value$this->getMergeFields())) {
  55.                     $mapping[$key] = $this->getMergeFields()[$value];
  56.                 }
  57.             }
  58.             $this->addSql('UPDATE config_pdf_field SET mapping = :mapping WHERE id = :id', [
  59.                 'id' => $row->id,
  60.                 'mapping' => serialize($mapping),
  61.             ]);
  62.         }
  63.         $this->parseString('config_document', ['body''header''footer'], 'toMarkdown');
  64.         $this->parseString('config_stage', ['message_body'], 'toMarkdown');
  65.         $this->parseString('config_stage_item', ['message_body'], 'toMarkdown');
  66.         $this->parseString('customer_letter', ['body'], 'toMarkdown');
  67.         $this->parseString('customer_message', ['body'], 'toMarkdown');
  68.         $this->parseString('feed_comment', ['body'], 'toMarkdown');
  69.         $this->parseString('issue_comment', ['body'], 'toMarkdown');
  70.         $this->parseString('user', ['signature'], 'toMarkdown');
  71.     }
  72.     /**
  73.      * @param string $table
  74.      * @param string $fields
  75.      * @param array  $method
  76.      */
  77.     private function parseString(string $table, array $fieldsstring $method)
  78.     {
  79.         $stmt $this->connection->prepare(sprintf('SELECT count(1) FROM %s'$table));
  80.         $stmt->execute();
  81.         // get total entries
  82.         $total $stmt->fetchColumn();
  83.         // insert using pagination
  84.         for ($i 0$i ceil($total 100); ++$i) {
  85.             $stmt $this->connection->prepare(sprintf('SELECT * FROM %s ORDER BY id ASC LIMIT %d, 100'$table100 $i));
  86.             $stmt->execute();
  87.             while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  88.                 $sets = [];
  89.                 $params = [];
  90.                 foreach ($fields as $field) {
  91.                     if (!empty($row->$field)) {
  92.                         $sets[] = sprintf('%s = :%s'$field$field);
  93.                         $params[$field] = $this->$method($row->$field);
  94.                     }
  95.                 }
  96.                 if (\count($params)) {
  97.                     $this->addSql(sprintf('UPDATE %s SET %s WHERE id = :id'$tableimplode(', '$sets)), array_merge([
  98.                         'id' => $row->id,
  99.                     ], $params));
  100.                 }
  101.             }
  102.         }
  103.     }
  104.     /**
  105.      * @param string $markdown
  106.      *
  107.      * @return string
  108.      */
  109.     private function toHtml(string $markdown): string
  110.     {
  111.         $converter = new Parsedown();
  112.         $html $converter->text($markdown);
  113.         $html preg_replace(
  114.             '/\[\[embed_field key=("|&quot;)(\w+)("|&quot;) \/(\])?(\])?/isU',
  115.             '<span class="text-editor-merge-field-container"><span class="text-editor-merge-field is-recipient" data-name="$2" contenteditable="false"># $2</span></span>',
  116.             $html
  117.         );
  118.         $html preg_replace(
  119.             '/\[\[signature_field id=("|&quot;)(\w+)("|&quot;) placeholder=("|&quot;)([\w\s]+)("|&quot;) \/(\])?(\])?/isU',
  120.             '<div class="signature-pad-link" data-id="$2" data-placeholder="$5" contenteditable="false">$5</div>',
  121.             $html
  122.         );
  123.         $html preg_replace(
  124.             '/\[\[signature_field id=("|&quot;)(\w+)("|&quot;) placeholder=("|&quot;)([\w\s]+)("|&quot;) img=("|&quot;)([\w_\:\.\-\=\/]+)("|&quot;) \/(\])?(\])?/isU',
  125.             '<div class="signature-pad-link" data-id="$2" data-placeholder="$5" data-image="$8"><img src="$8" /></div>',
  126.             $html
  127.         );
  128.         foreach ($this->getMergeFields() as $to => $from) {
  129.             $html str_replace(sprintf('data-name="%s"'$from), sprintf('data-name="%s"'$to), $html);
  130.             $html str_replace(sprintf('contenteditable="false"># %s</span>'$from), sprintf('contenteditable="false"># %s</span>'$to), $html);
  131.             $html str_replace(sprintf('{{%s}}'$from), sprintf('<span class="text-editor-merge-field-container"><span class="text-editor-merge-field is-recipient" data-name="%s" contenteditable="false"># %s</span></span>'$to$to), $html);
  132.         }
  133.         return $html;
  134.     }
  135.     /**
  136.      * @param string $html
  137.      *
  138.      * @return string
  139.      */
  140.     private function toMarkdown(string $html): string
  141.     {
  142.         $markdown preg_replace(
  143.             '/<span class="text-editor-merge-field-container"><span class="text-editor-merge-field is-recipient"(.*)data-name="(\w+)"(.*)># (\w+)<\/span><\/span>/isU',
  144.             '[[embed_field key="$1" /]]',
  145.             $html
  146.         );
  147.         $markdown preg_replace(
  148.             '/<div class="signature-pad-link"(.*)data-id="(\w+)" data-placeholder="([\w\s]+)"(.*)>([\w\s]+)<\/div>/isU',
  149.             '[[signature_field id="$2" placeholder="$3" /]]',
  150.             $markdown
  151.         );
  152.         $markdown preg_replace(
  153.             '/<div class="signature-pad-link"(.*)data-id="(\w+)" data-placeholder="([\w\s]+)" data-image="([\w_\:\.\-\=\/]+)"(.*)>(.+)<\/div>/isU',
  154.             '[[signature_field id="$2" placeholder="$3" img="$4" /]]',
  155.             $markdown
  156.         );
  157.         foreach ($this->getMergeFields() as $to => $from) {
  158.             $markdown str_replace(sprintf('[[embed_field key="%s"'$to), sprintf('[[embed_field key="%s"'$from), $markdown);
  159.             $markdown str_replace(sprintf('[[signature_field id="%s"'$to), sprintf('[[signature_field id="%s"'$from), $markdown);
  160.         }
  161.         // get rid of attributes!
  162.         $elements = ['p''span''ul''ol''li''strong''h1''h2''h3''h4''h5''h6'];
  163.         foreach ($elements as $element) {
  164.             $markdown preg_replace("/<$element.*?>/""<$element>"$markdown);
  165.         }
  166.         $regexMap = [
  167.             'p' => '',
  168.             '\/p' => "\n",
  169.             'span' => '',
  170.             'span.*?' => '',
  171.             '\/span' => '',
  172.             'h1' => '#',
  173.             'h2' => '##',
  174.             'h3' => '###',
  175.             'h4' => '####',
  176.             'h5' => '#####',
  177.             'h6' => '######',
  178.             '\/h\d' => "\n",
  179.             'br' => "\n",
  180.             'br\s\/' => "\n",
  181.             'strong' => '__',
  182.             '\/strong' => '__',
  183.             'em' => '_',
  184.             '\/em' => '_',
  185.         ];
  186.         // replace simple tags w/ markdown equivalent
  187.         foreach ($regexMap as $element => $replacement) {
  188.             $markdown preg_replace("/<$element>/"$replacement$markdown);
  189.         }
  190.         // images: Capture <img> tags, isolate src
  191.         if (preg_match_all('/<img.*src="([^\s"]*?)".*>?/'$markdown$matches)) {
  192.             // swap markdown replacement for <img> markup for each match
  193.             foreach ($matches[0] as $i => $img_markup) {
  194.                 $url $matches[1][$i];
  195.                 // alt tags
  196.                 $alt null;
  197.                 if (preg_match('/alt="([^\s"]*?)"/'$img_markup$alt_match)) {
  198.                     $alt $alt[1];
  199.                 }
  200.                 // build markdown and swap
  201.                 $img_markdown "![$alt]($url)";
  202.                 $markdown str_replace($img_markup$img_markdown$markdown);
  203.             }
  204.         }
  205.         // links: Capture <a> tags, isolate href
  206.         if (preg_match_all('/<a.*?href="([^\s"]*?)".*?>(.*?)?<\/a>/'$markdown$matches)) {
  207.             // Swap markdown replacement for <a> markup for each match
  208.             foreach ($matches[0] as $i => $a_markup) {
  209.                 $href $matches[1][$i];
  210.                 $text $matches[2][$i];
  211.                 // Build markdown and swap
  212.                 $a_markdown "[$text]($href)";
  213.                 $markdown str_replace($a_markup$a_markdown$markdown);
  214.             }
  215.         }
  216.         // unordered lists
  217.         if (preg_match_all('/<ul>(.*?)<\/ul>/s'$markdown$matches)) {
  218.             $markdown preg_replace('/<ul>|<\/ul>/'''$markdown);
  219.             foreach ($matches[0] as $ul) {
  220.                 if (preg_match_all('/<li>(.*?)<\/li>/s'$ul$list_items)) {
  221.                     foreach ($list_items[0] as $i => $li_markup) {
  222.                         $li_inner $list_items[1][$i];
  223.                         $li_markdown '- '.$li_inner;
  224.                         $markdown str_replace($li_markup$li_markdown$markdown);
  225.                     }
  226.                 }
  227.             }
  228.         }
  229.         // ordered lists
  230.         if (preg_match_all('/<ol>(.*?)<\/ol>/s'$markdown$matches)) {
  231.             $markdown preg_replace('/<ol>|<\/ol>/'''$markdown);
  232.             foreach ($matches[0] as $ol) {
  233.                 if (preg_match_all('/<li>(.*?)<\/li>/s'$ol$list_items)) {
  234.                     foreach ($list_items[0] as $i => $li_markup) {
  235.                         $li_inner $list_items[1][$i];
  236.                         $li_markdown $i .'. '.$li_inner;
  237.                         $markdown str_replace($li_markup$li_markdown$markdown);
  238.                     }
  239.                 }
  240.             }
  241.         }
  242.         // blockquotes
  243.         if (preg_match_all('/<blockquote>(.*?)<\/blockquote>/s'$markdown$matches)) {
  244.             foreach ($matches[1] as $i => $inner_html) {
  245.                 $blockquote_markup $matches[$i];
  246.                 $blockquote_markdown '';
  247.                 $lines explode("\n"$inner_html);
  248.                 foreach ($lines as $line) {
  249.                     $blockquote_markdown .= '> '.$line."\n";
  250.                 }
  251.                 $markdown str_replace($blockquote_markup$blockquote_markdown$markdown);
  252.             }
  253.         }
  254.         return $markdown;
  255.     }
  256.     /**
  257.      * @return array
  258.      */
  259.     private function getMergeFields(): array
  260.     {
  261.         static $fields = [];
  262.         if (\count($fields)) {
  263.             return $fields;
  264.         }
  265.         $stmt $this->connection->prepare('SELECT * FROM config_address');
  266.         $stmt->execute();
  267.         while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  268.             $fields array_merge($fields, [
  269.                 sprintf('address_%d_phone_number'$row->id) => sprintf('address_%d_telephone'$row->id),
  270.                 sprintf('address_%d_fax_number'$row->id) => sprintf('address_%d_fax'$row->id),
  271.                 sprintf('address_%d_use_address'$row->id) => sprintf('address_%d_flag_address'$row->id),
  272.                 sprintf('address_%d_use_phone_number'$row->id) => sprintf('address_%d_flag_telephone'$row->id),
  273.                 sprintf('address_%d_use_fax_number'$row->id) => sprintf('address_%d_flag_fax'$row->id),
  274.                 sprintf('address_%d_street'$row->id) => sprintf('address_%d_street_address'$row->id),
  275.                 sprintf('address_%d_zip'$row->id) => sprintf('address_%d_postcode'$row->id),
  276.             ]);
  277.         }
  278.         $fields array_merge($fields, [
  279.             'customer_id' => 'id',
  280.             'customer_first_name' => 'customer_firstname',
  281.             'customer_middle_name' => 'customer_middle_initial',
  282.             'customer_last_name' => 'customer_lastname',
  283.             'customer_birthday' => 'customer_birthday',
  284.             'customer_marital_status' => 'customer_marital_status',
  285.             'customer_spouse_first_name' => 'customer_spouses_firstname',
  286.             'customer_spouse_middle_name' => 'customer_spouses_middle_initial',
  287.             'customer_spouse_last_name' => 'customer_spouses_lastname',
  288.             'customer_spouse_birthday' => 'customer_spouses_birthday',
  289.             'customer_company_name' => 'customer_company',
  290.             'customer_company_street' => 'company_street_address',
  291.             'customer_company_city' => 'company_city',
  292.             'customer_company_state' => 'company_state',
  293.             'customer_company_zip' => 'company_postcode',
  294.             'customer_billing_company_name' => 'billing_company',
  295.             'customer_billing_first_name' => 'billing_firstname',
  296.             'customer_billing_last_name' => 'billing_lastname',
  297.             'customer_billing_street' => 'billing_street_address',
  298.             'customer_billing_city' => 'billing_city',
  299.             'customer_billing_state' => 'billing_state',
  300.             'customer_billing_zip' => 'billing_postcode',
  301.             'customer_billing_as_address' => 'same_as_address',
  302.             'customer_phone_number' => 'customer_telephone',
  303.             'customer_spouse_phone_number' => 'customer_spouses_telephone',
  304.             'customer_fax_number' => 'customer_fax',
  305.             'customer_mobile_number' => 'customer_mobile',
  306.             'customer_work_number' => 'customer_work_number',
  307.             'customer_ssn' => 'customer_ssn',
  308.             'customer_spouse_ssn' => 'customer_spouses_ssn',
  309.             'customer_ein' => 'customer_ein',
  310.             'customer_email' => 'customer_email_address',
  311.             'customer_spouse_email' => 'customer_spouses_email_address',
  312.             'customer_password' => 'customer_password',
  313.             'customer_plain_password' => 'customer_raw_password',
  314.             'customer_irs_debt' => 'irs_debt',
  315.             'customer_state_debt' => 'state_debt',
  316.             'customer_checking_saving' => 'checking_saving',
  317.             'customer_available_credit' => 'available_credit',
  318.             'customer_credit_owed' => 'credit_owed',
  319.             'customer_fee' => 'fee',
  320.             'customer_years_filed' => 'years',
  321.             'customer_biz_years_filed' => 'biz_years',
  322.             'customer_pqa_received_at' => 'pqa_received_date',
  323.             'customer_bqa_received_at' => 'bqa_received_date',
  324.             'customer_rating' => 'rating',
  325.             'customer_interview_time' => 'interview_time',
  326.             'customer_stage_started_at' => 'stage_started',
  327.             'customer_stage_percent_done' => 'done_percent',
  328.             'customer_converted_at' => 'date_converted',
  329.             'customer_last_login' => 'last_login',
  330.             'customer_created_at' => 'date_added',
  331.             'customer_modified_at' => 'last_modified',
  332.             'customer_street' => 'customer_street_address',
  333.             'customer_city' => 'customer_city',
  334.             'customer_state' => 'customer_state',
  335.             'customer_zip' => 'customer_postcode',
  336.             'customer_user' => 'administrators_id',
  337.             'customer_company' => 'customers_companies_id',
  338.             'customer_source' => 'source_id',
  339.             'customer_level' => 'level',
  340.             'customer_case_manager_user' => 'cm_user',
  341.             'customer_lead_agent_user' => 'la_user',
  342.             'customer_financial_user' => 'financial_user',
  343.             'customer_service_user' => 'service_sales_user',
  344.             'customer_processor_user' => 'processor_user',
  345.             'customer_resolution_specialist_user' => 'resolution_specialist_user',
  346.             'customer_stage' => 'stage_id',
  347.             'customer_status' => 'status_id',
  348.         ]);
  349.         $fields array_merge($fields, [
  350.             'interview_filed_last3_years' => 'interview_filed_last_3_years',
  351.             'interview_home_value80' => 'interview_home_value_80',
  352.             'interview_car_payment1' => 'interview_car_payment_1',
  353.             'interview_car_payment2' => 'interview_car_payment_2',
  354.             'interview_taxes1530' => 'interview_taxes_15_30',
  355.             'interview_total_equity2' => 'interview_total_equity_2',
  356.         ]);
  357.         $fields array_merge($fields, [
  358.             'card_brand' => 'cc_type',
  359.             'card_owner' => 'cc_owner',
  360.             'card_number' => 'cc_number',
  361.             'card_exp_month' => 'cc_expires_month',
  362.             'card_exp_year' => 'cc_expires_year',
  363.             'card_expires' => 'cc_expires',
  364.             'card_cvc' => 'cc_cvc',
  365.             'card_scheduling' => 'cc_scheduling',
  366.             'e_check_owner' => 'echk_owner',
  367.             'e_check_bank_name' => 'echk_bank',
  368.             'e_check_account_number' => 'echk_account',
  369.             'e_check_routing' => 'echk_routing',
  370.             'e_check_number' => 'echk_number_check',
  371.             'e_check_phone_number' => 'echk_telephone',
  372.             'e_check_scheduling' => 'echk_scheduling',
  373.         ]);
  374.         return $fields;
  375.     }
  376. }