You are here

function ueditor_entity_update in UEditor - 百度编辑器 7.2

Implementation of hook_entity_update().

File

./ueditor.module, line 659
Integration ueditor for wysiwyg.

Code

function ueditor_entity_update($entity, $type) {
  if ($type != 'file') {
    $info = entity_get_info($type);
    list($id) = entity_extract_ids($type, $entity);
    if (!empty($id)) {
      $editor_profiles = _ueditor_get_wysiwyg();
      $fields = _ueditor_get_editor_fields($entity, $type, $editor_profiles);
      if ($fields) {

        //add support taxonomy term description field.
        if ($type == 'taxonomy_term' && isset($entity->description)) {
          $fields['description'][]['value'] = $entity->description;
        }
        foreach ($fields as $field_key => $field) {
          if (!empty($field[0]['value'])) {
            preg_match_all('/(.*?(src|href)=["|\'])(.*?)["|\']/ms', $field[0]['value'], $matchs[$field_key]);
          }
          else {
            $matchs[$field_key][3] = array();
          }
        }
        if (isset($entity->original)) {
          $old_fields = _ueditor_get_editor_fields($entity->original, $type, $editor_profiles);

          //add support taxonomy term description field.
          if ($type == 'taxonomy_term' && isset($entity->original->description)) {
            $old_fields['description'][]['value'] = $entity->original->description;
          }
          if ($old_fields) {
            foreach ($old_fields as $old_key => $old_field) {
              if (!empty($old_field[0]['value'])) {
                preg_match_all('/(.*?(src|href)=["|\'])(.*?)["|\']/ms', $old_field[0]['value'], $old_matchs[$old_key]);
              }
              else {
                $old_matchs[$old_key][3] = array();
              }
            }
          }
          if (!empty($matchs) && !empty($old_matchs)) {
            $diff_new_images = array();
            $diff_old_images = array();
            foreach ($matchs as $k => $match) {
              $intersect = array_intersect_assoc($matchs[$k][3], $old_matchs[$k][3]);
              foreach ($intersect as $i => $item) {
                unset($matchs[$k][3][$i]);
                unset($old_matchs[$k][3][$i]);
              }
              $diff_new_images[$k] = $matchs[$k][3];
              $diff_old_images[$k] = $old_matchs[$k][3];
            }
          }
          if (count(array_filter($diff_new_images))) {
            ueditor_file_register(array_filter($diff_new_images), $type, '', $id);
          }
          if (count(array_filter($diff_old_images))) {
            ueditor_delete_file(array_filter($diff_old_images), $type, '', $id);
          }
        }
      }
    }
  }
}