You are here

function easychart_update_7200 in Easychart 7.3

Same name and namespace in other branches
  1. 7.2 easychart.install \easychart_update_7200()

Transpose all existing data.

File

./easychart.install, line 141
Easychart install file.

Code

function easychart_update_7200() {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'easychart')
    ->fieldCondition('easychart', 'csv', '', '!=');
  $result = $query
    ->execute();
  if (isset($result['node'])) {
    $easycharts = $result['node'];
    foreach ($easycharts as $easychart) {
      $node = node_load($easychart->nid);
      $data = field_get_items('node', $node, 'easychart');

      // Get separator.
      $separator = ',';
      $seps = array(
        ';',
        ',',
        '|',
        "\t",
      );
      $max = 0;
      foreach ($seps as $sep) {
        $count = substr_count($data[0]['csv'], $sep);
        if ($count > $max) {
          $separator = $sep;
          $max = $count;
        }
      }
      $new_data = array();
      $rows = str_getcsv($data[0]['csv'], "\n");

      //parse the rows
      foreach ($rows as &$row) {
        $new_data[] = str_getcsv($row, $separator);
      }
      $new_data = _transpose($new_data);

      // Clear the first cell if there are categories
      if (!is_numeric($new_data[1][0])) {
        $new_data[0][0] = '';
      }
      $node->easychart['und'][0]['csv'] = json_encode($new_data);
      field_attach_update('node', $node);
    }
  }
}