PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/helpers/model/ajax.php

https://github.com/josesanch/wimpy
PHP | 267 lines | 195 code | 58 blank | 14 comment | 29 complexity | 53a694d85df690a50265b7d2ce6571ec MD5 | raw file
  1. <?php
  2. class helpers_model_ajax {
  3. protected $model;
  4. public function __construct($model) {
  5. $this->model = $model;
  6. }
  7. public function listItems()
  8. {
  9. $limit = $where = $order = "";
  10. if(isset($_REQUEST["limit"]))
  11. $limit = "limit: ".$_REQUEST["start"].", ".$_REQUEST["limit"];
  12. if(isset($_REQUEST["query"]))
  13. $where = "nombre like '%".$_REQUEST["query"]."%'";
  14. if(isset($_REQUEST["sort"]))
  15. $order = "order: ".$_REQUEST["sort"]." ".$_REQUEST["dir"];
  16. $count = $this->model->count($where);
  17. if ($_REQUEST["fields"])
  18. $columns = "columns: ".$_REQUEST["fields"];
  19. $data = $this->model->select($where, $limit, $columns, $order);
  20. $items = array();
  21. foreach($data as $item) $items[] = $item->getRowData();
  22. echo json_encode(array("items" => $items, "count" => $count));
  23. }
  24. public function files($action, $field, $id)
  25. {
  26. $cond = "";
  27. switch ($action) {
  28. case 'read':
  29. $this->model->select($id);
  30. $images = new helpers_images();
  31. $module = web::request("tmp_upload") ? web::request("tmp_upload") : $this->model->image_label;
  32. if(!web::request("tmp_upload")) $cond = " and iditem='$id' ";
  33. $data = $images->select("module='$module' $cond and field='$field' ", "order:orden", "columns: id, nombre, extension, tipo");
  34. if (web::auth()->hasPermission($this->model, auth::DELETE) || web::auth()->hasPermission($this->model, auth::MODIFY))
  35. $sortable= " sortable";
  36. $str = "
  37. <h5 class='images'>".count($data)." archivos</h5>
  38. <ul id='files_$field' class='images-dataview clearfix$sortable'>";
  39. $filters = new html_template_filters();
  40. foreach($data as $item) {
  41. $str .="
  42. <li id='images-$item->id'>
  43. <div>";
  44. if ($item->isImage()) {
  45. $str .= "<a href='".$item->url()."' class='dataview-image' rel='images-{$module}'><img src='".$item->src("100x80", thumb::CROP)."' title='$item->nombre ".$filters->bytes($item->size())."'/></a>";
  46. } else {
  47. $str .= "<img src='".$item->src("100x80", thumb::CROP)."' title='$item->nombre ".$filters->bytes($item->size())."'/>";
  48. }
  49. $editable = "";
  50. if (web::auth()->hasPermission($this->model, auth::DELETE) || web::auth()->hasPermission($this->model, auth::MODIFY)) {
  51. $str .=" <a href='#' class='images-delete' title='Eliminar archivo' id='$id-".get_class($this->model)."-$field-$item->id-".web::request("tmp_upload")."'><img src='/resources/icons/cross.gif' border='0'/></a>";
  52. $editable = "class='editable'";
  53. }
  54. $str .= "
  55. <a href='".$item->url()."' class='images-download' id='$id-".get_class($this->model)."-$field-$item->id-".web::request("tmp_upload")."' target='_blank' title='Descargar documento'><img src='/resources/admin/images/document-save.png' border='0'/></a>
  56. </div>
  57. <input type='text' $editable id='file-$item->id' value='$item->nombre'/>
  58. </li>";
  59. }
  60. $str .= "</ul>";
  61. echo $str;
  62. exit;
  63. break;
  64. case 'save':
  65. web::log("$action, $id, $field");
  66. web::log(var_export($_FILES, true));
  67. if (isset($id)) {
  68. $this->model->select($id);
  69. }
  70. echo ($this->model->uploadImage($field)) ? "1" : "0";
  71. exit;
  72. case 'destroy':
  73. $image = new helpers_images();
  74. $image->delete($id);
  75. exit;
  76. case 'update':
  77. list($t, $id) = explode('-', $_REQUEST['id']);
  78. web::database()->exec("UPDATE images SET nombre='".$_REQUEST["value"]."' where id=".$id);
  79. echo $_REQUEST['value'];
  80. exit;
  81. }
  82. }
  83. public function load($id = null)
  84. {
  85. $item = $this->model->select($id);
  86. $arr = array();
  87. foreach($this->model->getFields() as $field => $attr) {
  88. $arr[$field] = $this->model->$field;
  89. }
  90. # var_dump($arr);
  91. // echo '{ data: '.json_encode($arr).', success: true, "recordCount" : 1}';
  92. // echo json_encode(array('success' => 'true',"data" => $arr));
  93. header('Content-type: text/xml;');
  94. echo '<?xml version="1.0" encoding="UTF-8"?><response success="true"><data>';
  95. foreach($this->model->getFields() as $field => $attr) {
  96. if($attr["type"] == 'text')
  97. echo "<$field><![CDATA[".$item->$field."]]></$field>";
  98. }
  99. echo '</data></response>';
  100. exit;
  101. }
  102. public function autocomplete($valor)
  103. {
  104. //if (web::request("term")) $valor = web::request("term");
  105. //if (!$valor) exit;
  106. $limit = web::request("limit");
  107. if (!$limit) $limit = 25;
  108. if ($limit) $limit = "limit: $limit";
  109. if(web::request("field")) {
  110. $attrs = $this->model->getFields(web::request("field"));
  111. if($attrs["show"]) $name = $attrs["show"];
  112. }
  113. if(!$name) $name = $this->model->getTitleField();
  114. if (web::request("term"))
  115. $q = strtolower(web::request("term"));
  116. else
  117. $q = strtolower($_GET["q"]);
  118. $primaryKey = array_shift($this->model->getPrimaryKeys());
  119. $results = $this->model->select(
  120. "columns: $primaryKey as id, $name as text",
  121. "where: $name like '%$q%'", "order: $name", $limit
  122. );
  123. if (web::request("q")) {
  124. foreach($results as $row) {
  125. echo $row->text."|".$row->id."\n";
  126. }
  127. } else {
  128. $json = array();
  129. foreach($results as $row) {
  130. $json[]= array("id" => $row->id, "label" => $row->text, "value" => $row->text);
  131. }
  132. echo json_encode($json);
  133. }
  134. exit;
  135. }
  136. public function reorderImages()
  137. {
  138. $orden = web::request("orden");
  139. $images = explode(",", $orden);
  140. $ids = array();
  141. $count = 0;
  142. foreach ($images as $img) {
  143. $id = array_pop(explode("-", $img));
  144. web::database()->query("UPDATE images SET orden='".($count++)."' WHERE id='$id'");
  145. }
  146. }
  147. /*
  148. * Reordenar las listas de administración.
  149. */
  150. public function reorderList()
  151. {
  152. $orden = web::request("orden");
  153. $rows = explode(",", $orden);
  154. $ids = array();
  155. // Obtenemos el mínimo
  156. foreach ($rows as $row) {
  157. $explode = explode('-', $row);
  158. $ids[]= array_pop($explode);
  159. }
  160. $result = web::database()->query("SELECT min(orden) as minimo FROM ".$this->model->getDatabaseTable()." where id in (".implode(",", $ids).")")->fetch();
  161. $count = $result["minimo"];
  162. // Ordenamos lo demás
  163. foreach ($rows as $row) {
  164. $explode = explode('-', $row);
  165. $id = array_pop($explode);
  166. web::database()->query("UPDATE ".$this->model->getDatabaseTable()." SET orden='".($count++)."' WHERE id='$id'");
  167. }
  168. exit;
  169. }
  170. public function delete() {
  171. $this->model->delete($_REQUEST["id"]);
  172. }
  173. private function parse($request) {
  174. return (isset($_REQUEST[$request])) ? json_decode(stripslashes($_REQUEST[$request]), true) : null;
  175. }
  176. public function getValue($id)
  177. {
  178. $attrs = $this->model->getFields(web::request("field"));
  179. if($attrs["show"]) $name = $attrs["show"];
  180. if(!$name) $name = $this->model->getTitleField();
  181. $primaryKey = array_shift($this->model->getPrimaryKeys());
  182. $data = $this->model->selectFirst(
  183. "columns: $name as text",
  184. "where: $primaryKey='$id'"
  185. );
  186. /*
  187. var_dump("columns: $name as text",
  188. "where: $primaryKey='$id'");
  189. */
  190. echo $data->text;
  191. exit;
  192. }
  193. public function getValueDialog($id)
  194. {
  195. $attrs = $this->model->getFields(web::request("field"));
  196. $relatedModelName = $attrs["belongs_to"] ? $attrs["belongs_to"] : $attrs["belongsTo"];
  197. $relatedModel = new $relatedModelName;
  198. if($attrs["show"]) $name = $attrs["show"];
  199. if(!$name) $name = $relatedModel->getTitleField();
  200. $fieldName = $field = web::request("field");
  201. if (substr($fieldName, -3) == "_id") $fieldName = substr($fieldName, 0, -3);
  202. $primaryKey = array_shift($relatedModel->getPrimaryKeys());
  203. $sql = "select $name as text from $relatedModelName as $fieldName
  204. where $fieldName.$primaryKey='$id'";
  205. $data = web::database()->query($sql)->fetch();
  206. echo $data["text"];
  207. exit;
  208. }
  209. }