extra_field.lib.php 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  4. /**
  5. * Class ExtraField
  6. */
  7. class ExtraField extends Model
  8. {
  9. public $columns = array(
  10. 'id',
  11. 'field_type',
  12. 'variable',
  13. 'display_text',
  14. 'default_value',
  15. 'field_order',
  16. 'visible',
  17. 'changeable',
  18. 'filter',
  19. 'extra_field_type',
  20. /* Enable this when field_loggeable is introduced as a table field (2.0)
  21. 'field_loggeable',
  22. */
  23. 'created_at'
  24. );
  25. public $ops = array(
  26. 'eq' => '=', //equal
  27. 'ne' => '<>', //not equal
  28. 'lt' => '<', //less than
  29. 'le' => '<=', //less than or equal
  30. 'gt' => '>', //greater than
  31. 'ge' => '>=', //greater than or equal
  32. 'bw' => 'LIKE', //begins with
  33. 'bn' => 'NOT LIKE', //doesn't begin with
  34. 'in' => 'LIKE', //is in
  35. 'ni' => 'NOT LIKE', //is not in
  36. 'ew' => 'LIKE', //ends with
  37. 'en' => 'NOT LIKE', //doesn't end with
  38. 'cn' => 'LIKE', //contains
  39. 'nc' => 'NOT LIKE' //doesn't contain
  40. );
  41. const FIELD_TYPE_TEXT = 1;
  42. const FIELD_TYPE_TEXTAREA = 2;
  43. const FIELD_TYPE_RADIO = 3;
  44. const FIELD_TYPE_SELECT = 4;
  45. const FIELD_TYPE_SELECT_MULTIPLE = 5;
  46. const FIELD_TYPE_DATE = 6;
  47. const FIELD_TYPE_DATETIME = 7;
  48. const FIELD_TYPE_DOUBLE_SELECT = 8;
  49. const FIELD_TYPE_DIVIDER = 9;
  50. const FIELD_TYPE_TAG = 10;
  51. const FIELD_TYPE_TIMEZONE = 11;
  52. const FIELD_TYPE_SOCIAL_PROFILE = 12;
  53. const FIELD_TYPE_CHECKBOX = 13;
  54. const FIELD_TYPE_MOBILE_PHONE_NUMBER = 14;
  55. const FIELD_TYPE_INTEGER = 15;
  56. const FIELD_TYPE_FILE_IMAGE = 16;
  57. const FIELD_TYPE_FLOAT = 17;
  58. const FIELD_TYPE_FILE = 18;
  59. const FIELD_TYPE_VIDEO_URL = 19;
  60. const FIELD_TYPE_LETTERS_ONLY = 20;
  61. const FIELD_TYPE_ALPHANUMERIC = 21;
  62. const FIELD_TYPE_LETTERS_SPACE = 22;
  63. const FIELD_TYPE_ALPHANUMERIC_SPACE = 23;
  64. public $type = 'user';
  65. public $pageName;
  66. public $pageUrl;
  67. public $extraFieldType = 0;
  68. /**
  69. * @param string $type
  70. */
  71. public function __construct($type)
  72. {
  73. $this->type = $type;
  74. $this->table = Database::get_main_table(TABLE_EXTRA_FIELD);
  75. $this->table_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  76. $this->table_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  77. switch ($this->type) {
  78. case 'calendar_event':
  79. $this->extraFieldType = EntityExtraField::CALENDAR_FIELD_TYPE;
  80. break;
  81. case 'course':
  82. $this->extraFieldType = EntityExtraField::COURSE_FIELD_TYPE;
  83. break;
  84. case 'user':
  85. $this->extraFieldType = EntityExtraField::USER_FIELD_TYPE;
  86. break;
  87. case 'session':
  88. $this->extraFieldType = EntityExtraField::SESSION_FIELD_TYPE;
  89. break;
  90. case 'question':
  91. $this->extraFieldType = EntityExtraField::QUESTION_FIELD_TYPE;
  92. break;
  93. case 'lp':
  94. $this->extraFieldType = EntityExtraField::LP_FIELD_TYPE;
  95. break;
  96. case 'lp_item':
  97. $this->extraFieldType = EntityExtraField::LP_ITEM_FIELD_TYPE;
  98. break;
  99. }
  100. $this->pageUrl = 'extra_fields.php?type='.$this->type;
  101. // Example QuestionFields
  102. $this->pageName = get_lang(ucwords($this->type).'Fields');
  103. }
  104. /**
  105. * @return int
  106. */
  107. public function getExtraFieldType()
  108. {
  109. return $this->extraFieldType;
  110. }
  111. /**
  112. * @return array
  113. */
  114. public static function getValidExtraFieldTypes()
  115. {
  116. return array(
  117. 'user',
  118. 'course',
  119. 'session',
  120. 'question',
  121. 'lp',
  122. 'calendar_event',
  123. 'lp_item'
  124. );
  125. }
  126. /**
  127. * @return int
  128. */
  129. public function get_count()
  130. {
  131. $em = Database::getManager();
  132. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  133. $query->select('count(e.id)');
  134. $query->where('e.extraFieldType = :type');
  135. $query->setParameter('type', $this->getExtraFieldType());
  136. return $query->getQuery()->getScalarResult();
  137. }
  138. /**
  139. * @param string $sidx
  140. * @param string $sord
  141. * @param int $start
  142. * @param int $limit
  143. *
  144. * @return array
  145. */
  146. public function getAllGrid($sidx, $sord, $start, $limit)
  147. {
  148. switch ($sidx) {
  149. case 'field_order':
  150. $sidx = 'e.fieldOrder';
  151. break;
  152. case 'variable':
  153. $sidx = 'e.variable';
  154. break;
  155. case 'display_text':
  156. $sidx = 'e.displayText';
  157. break;
  158. case 'changeable':
  159. $sidx = 'e.changeable';
  160. break;
  161. case 'visible':
  162. $sidx = 'e.visible';
  163. break;
  164. case 'filter':
  165. $sidx = 'e.filter';
  166. break;
  167. case 'display_text':
  168. $sidx = 'e.fieldType';
  169. break;
  170. }
  171. $em = Database::getManager();
  172. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  173. $query->select('e')
  174. ->where('e.extraFieldType = :type')
  175. ->setParameter('type', $this->getExtraFieldType())
  176. ->orderBy($sidx, $sord)
  177. ->setFirstResult($start)
  178. ->setMaxResults($limit);
  179. //echo $query->getQuery()->getSQL();
  180. return $query->getQuery()->getArrayResult();
  181. }
  182. /**
  183. * @param array $conditions
  184. * @param null $order_field_options_by
  185. *
  186. * @return array
  187. */
  188. public function get_all($conditions = array(), $order_field_options_by = null)
  189. {
  190. $conditions = Database::parse_conditions(array('where' => $conditions));
  191. if (empty($conditions)) {
  192. $conditions .= " WHERE extra_field_type = ".$this->extraFieldType;
  193. } else {
  194. $conditions .= " AND extra_field_type = ".$this->extraFieldType;
  195. }
  196. $sql = "SELECT * FROM $this->table
  197. $conditions
  198. ORDER BY field_order ASC
  199. ";
  200. $result = Database::query($sql);
  201. $extraFields = Database::store_result($result, 'ASSOC');
  202. $option = new ExtraFieldOption($this->type);
  203. if (!empty($extraFields)) {
  204. foreach ($extraFields as &$extraField) {
  205. $extraField['options'] = $option->get_field_options_by_field(
  206. $extraField['id'],
  207. false,
  208. $order_field_options_by
  209. );
  210. }
  211. }
  212. return $extraFields;
  213. }
  214. /**
  215. * @param string $variable
  216. *
  217. * @return array|bool
  218. */
  219. public function get_handler_field_info_by_field_variable($variable)
  220. {
  221. $variable = Database::escape_string($variable);
  222. $sql = "SELECT * FROM {$this->table}
  223. WHERE
  224. variable = '$variable' AND
  225. extra_field_type = $this->extraFieldType";
  226. $result = Database::query($sql);
  227. if (Database::num_rows($result)) {
  228. $row = Database::fetch_array($result, 'ASSOC');
  229. // All the options of the field
  230. $sql = "SELECT * FROM $this->table_field_options
  231. WHERE field_id='".intval($row['id'])."'
  232. ORDER BY option_order ASC";
  233. $result = Database::query($sql);
  234. while ($option = Database::fetch_array($result)) {
  235. $row['options'][$option['id']] = $option;
  236. }
  237. return $row;
  238. } else {
  239. return false;
  240. }
  241. }
  242. /**
  243. * @param int $fieldId
  244. *
  245. * @return array|bool
  246. */
  247. public function getFieldInfoByFieldId($fieldId)
  248. {
  249. $fieldId = intval($fieldId);
  250. $sql = "SELECT * FROM {$this->table}
  251. WHERE
  252. field_id = '$fieldId' AND
  253. extra_field_type = $this->extraFieldType";
  254. $result = Database::query($sql);
  255. if (Database::num_rows($result)) {
  256. $row = Database::fetch_array($result, 'ASSOC');
  257. // All the options of the field
  258. $sql = "SELECT * FROM $this->table_field_options
  259. WHERE field_id='".$fieldId."'
  260. ORDER BY option_order ASC";
  261. $result = Database::query($sql);
  262. while ($option = Database::fetch_array($result)) {
  263. $row['options'][$option['id']] = $option;
  264. }
  265. return $row;
  266. } else {
  267. return false;
  268. }
  269. }
  270. /**
  271. * @return int
  272. */
  273. public function get_max_field_order()
  274. {
  275. $sql = "SELECT MAX(field_order)
  276. FROM {$this->table}
  277. WHERE
  278. extra_field_type = '.$this->extraFieldType.'";
  279. $res = Database::query($sql);
  280. $order = 0;
  281. if (Database::num_rows($res) > 0) {
  282. $row = Database::fetch_row($res);
  283. $order = $row[0] + 1;
  284. }
  285. return $order;
  286. }
  287. /**
  288. * @param string $handler
  289. *
  290. * @return array
  291. */
  292. public static function get_extra_fields_by_handler($handler)
  293. {
  294. $types= array();
  295. $types[self::FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
  296. $types[self::FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
  297. $types[self::FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
  298. $types[self::FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect');
  299. $types[self::FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple');
  300. $types[self::FIELD_TYPE_DATE] = get_lang('FieldTypeDate');
  301. $types[self::FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime');
  302. $types[self::FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect');
  303. $types[self::FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider');
  304. $types[self::FIELD_TYPE_TAG] = get_lang('FieldTypeTag');
  305. $types[self::FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone');
  306. $types[self::FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile');
  307. $types[self::FIELD_TYPE_MOBILE_PHONE_NUMBER] = get_lang('FieldTypeMobilePhoneNumber');
  308. $types[self::FIELD_TYPE_CHECKBOX] = get_lang('FieldTypeCheckbox');
  309. $types[self::FIELD_TYPE_INTEGER] = get_lang('FieldTypeInteger');
  310. $types[self::FIELD_TYPE_FILE_IMAGE] = get_lang('FieldTypeFileImage');
  311. $types[self::FIELD_TYPE_FLOAT] = get_lang('FieldTypeFloat');
  312. $types[self::FIELD_TYPE_FILE] = get_lang('FieldTypeFile');
  313. $types[self::FIELD_TYPE_VIDEO_URL] = get_lang('FieldTypeVideoUrl');
  314. $types[self::FIELD_TYPE_LETTERS_ONLY] = get_lang('FieldTypeOnlyLetters');
  315. $types[self::FIELD_TYPE_ALPHANUMERIC] = get_lang('FieldTypeAlphanumeric');
  316. $types[self::FIELD_TYPE_LETTERS_SPACE] = get_lang(
  317. 'FieldTypeLettersSpaces'
  318. );
  319. $types[self::FIELD_TYPE_ALPHANUMERIC_SPACE] = get_lang(
  320. 'FieldTypeAlphanumericSpaces'
  321. );
  322. switch ($handler) {
  323. case 'course':
  324. case 'session':
  325. case 'user':
  326. break;
  327. }
  328. return $types;
  329. }
  330. /**
  331. * Add elements to a form
  332. *
  333. * @param FormValidator $form
  334. * @param int $itemId
  335. * @param array $exclude variables of extra field to exclude
  336. * @return array|bool
  337. */
  338. public function addElements($form, $itemId = 0, $exclude = [])
  339. {
  340. if (empty($form)) {
  341. return false;
  342. }
  343. $itemId = intval($itemId);
  344. $form->addHidden('item_id', $itemId);
  345. $extraData = false;
  346. if (!empty($itemId)) {
  347. $extraData = self::get_handler_extra_data($itemId);
  348. if ($form) {
  349. $form->setDefaults($extraData);
  350. }
  351. }
  352. $extraFields = $this->get_all(null, 'option_order');
  353. $extra = $this->set_extra_fields_in_form(
  354. $form,
  355. $extraData,
  356. false,
  357. $extraFields,
  358. $itemId,
  359. $exclude
  360. );
  361. return $extra;
  362. }
  363. /**
  364. * @param int $itemId (session_id, question_id, course id)
  365. *
  366. * @return array
  367. */
  368. public function get_handler_extra_data($itemId)
  369. {
  370. if (empty($itemId)) {
  371. return array();
  372. }
  373. $extra_data = array();
  374. $fields = self::get_all();
  375. $field_values = new ExtraFieldValue($this->type);
  376. if (!empty($fields) > 0) {
  377. foreach ($fields as $field) {
  378. $field_value = $field_values->get_values_by_handler_and_field_id(
  379. $itemId, $field['id']
  380. );
  381. if ($field['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  382. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  383. $extra_data['extra_'.$field['variable']] = $tags;
  384. continue;
  385. }
  386. if ($field_value) {
  387. $field_value = $field_value['value'];
  388. switch ($field['field_type']) {
  389. case ExtraField::FIELD_TYPE_TAG:
  390. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  391. $extra_data['extra_'.$field['variable']] = $tags;
  392. break;
  393. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  394. $selected_options = explode(
  395. '::',
  396. $field_value
  397. );
  398. $firstOption = isset($selected_options[0]) ? $selected_options[0] : '';
  399. $secondOption = isset($selected_options[1]) ? $selected_options[1] : '';
  400. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $firstOption;
  401. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable'].'_second'] = $secondOption;
  402. break;
  403. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  404. $field_value = explode(';', $field_value);
  405. $extra_data['extra_'.$field['variable']] = $field_value;
  406. break;
  407. case ExtraField::FIELD_TYPE_RADIO:
  408. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $field_value;
  409. break;
  410. default:
  411. $extra_data['extra_'.$field['variable']] = $field_value;
  412. break;
  413. }
  414. } else {
  415. // Set default values
  416. if (isset($field['field_default_value']) && !empty($field['field_default_value'])) {
  417. $extra_data['extra_'.$field['variable']] = $field['field_default_value'];
  418. }
  419. }
  420. }
  421. }
  422. return $extra_data;
  423. }
  424. /**
  425. * @param string $field_type
  426. *
  427. * @return array
  428. */
  429. public function get_all_extra_field_by_type($field_type)
  430. {
  431. // all the information of the field
  432. $sql = "SELECT * FROM {$this->table}
  433. WHERE
  434. field_type = '".Database::escape_string($field_type)."' AND
  435. extra_field_type = $this->extraFieldType
  436. ";
  437. $result = Database::query($sql);
  438. $return = array();
  439. while ($row = Database::fetch_array($result)) {
  440. $return[] = $row['id'];
  441. }
  442. return $return;
  443. }
  444. /**
  445. * @return array
  446. */
  447. public function get_field_types()
  448. {
  449. return self::get_extra_fields_by_handler($this->type);
  450. }
  451. /**
  452. * @param int $id
  453. *
  454. * @return null
  455. */
  456. public function get_field_type_by_id($id)
  457. {
  458. $types = self::get_field_types();
  459. if (isset($types[$id])) {
  460. return $types[$id];
  461. }
  462. return null;
  463. }
  464. /**
  465. * Converts a string like this:
  466. * France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
  467. * into
  468. * array(
  469. * 'France' =>
  470. * array('Paris', 'Bregtane', 'Marseilles'),
  471. * 'Belgique' =>
  472. * array('Namur', 'Liège')
  473. * ), etc
  474. * @param string $string
  475. *
  476. * @return array
  477. */
  478. public static function extra_field_double_select_convert_string_to_array($string)
  479. {
  480. $options = explode('|', $string);
  481. $options_parsed = array();
  482. $id = 0;
  483. if (!empty($options)) {
  484. foreach ($options as $sub_options) {
  485. $options = explode(':', $sub_options);
  486. $sub_sub_options = explode(';', $options[1]);
  487. $options_parsed[$id] = array(
  488. 'label' => $options[0],
  489. 'options' => $sub_sub_options,
  490. );
  491. $id++;
  492. }
  493. }
  494. return $options_parsed;
  495. }
  496. /**
  497. * @param array $options
  498. *
  499. * @return array
  500. */
  501. public static function extra_field_double_select_convert_array_to_ordered_array($options)
  502. {
  503. $options_parsed = array();
  504. if (!empty($options)) {
  505. foreach ($options as $option) {
  506. if ($option['option_value'] == 0) {
  507. $options_parsed[$option['id']][] = $option;
  508. } else {
  509. $options_parsed[$option['option_value']][] = $option;
  510. }
  511. }
  512. }
  513. return $options_parsed;
  514. }
  515. /**
  516. * @param array $options the result of the get_field_options_by_field() array
  517. *
  518. * @return string
  519. */
  520. public static function extra_field_double_select_convert_array_to_string($options)
  521. {
  522. $string = null;
  523. $options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options);
  524. if (!empty($options_parsed)) {
  525. foreach ($options_parsed as $option) {
  526. foreach ($option as $key => $item) {
  527. $string .= $item['display_text'];
  528. if ($key == 0) {
  529. $string .= ':';
  530. } else {
  531. if (isset($option[$key + 1])) {
  532. $string .= ';';
  533. }
  534. }
  535. }
  536. $string .= '|';
  537. }
  538. }
  539. if (!empty($string)) {
  540. $string = substr($string, 0, strlen($string) - 1);
  541. }
  542. return $string;
  543. }
  544. /**
  545. * @param array $params
  546. *
  547. * @return array
  548. */
  549. public function clean_parameters($params)
  550. {
  551. if (!isset($params['variable']) || empty($params['variable'])) {
  552. $params['variable'] = $params['display_text'];
  553. }
  554. $params['variable'] = trim(strtolower(str_replace(" ", "_", $params['variable'])));
  555. if (!isset($params['field_order'])) {
  556. $max_order = self::get_max_field_order();
  557. $params['field_order'] = $max_order;
  558. }
  559. return $params;
  560. }
  561. /**
  562. * @param array $params
  563. * @param bool $show_query
  564. *
  565. * @return bool
  566. */
  567. public function save($params, $show_query = false)
  568. {
  569. $fieldInfo = self::get_handler_field_info_by_field_variable($params['variable']);
  570. $params = self::clean_parameters($params);
  571. $params['extra_field_type'] = $this->extraFieldType;
  572. if ($fieldInfo) {
  573. return $fieldInfo['id'];
  574. } else {
  575. $id = parent::save($params, $show_query);
  576. if ($id) {
  577. $session_field_option = new ExtraFieldOption($this->type);
  578. $params['field_id'] = $id;
  579. $session_field_option->save($params);
  580. }
  581. return $id;
  582. }
  583. }
  584. /**
  585. * @param array $params
  586. *
  587. * @return bool|void
  588. */
  589. public function update($params)
  590. {
  591. $params = self::clean_parameters($params);
  592. if (isset($params['id'])) {
  593. $field_option = new ExtraFieldOption($this->type);
  594. $params['field_id'] = $params['id'];
  595. $field_option->save($params);
  596. }
  597. parent::update($params);
  598. }
  599. /**
  600. * @param $id
  601. *
  602. * @return bool|void
  603. */
  604. public function delete($id)
  605. {
  606. $field_option = new ExtraFieldOption($this->type);
  607. $field_option->delete_all_options_by_field_id($id);
  608. $session_field_values = new ExtraFieldValue($this->type);
  609. $session_field_values->delete_all_values_by_field_id($id);
  610. parent::delete($id);
  611. }
  612. /**
  613. * @param FormValidator $form
  614. * @param array $extraData
  615. * @param bool $admin_permissions
  616. * @param int $user_id
  617. * @param array $extra
  618. * @param int $itemId
  619. * @param array $exclude variables of extra field to exclude
  620. * @return array
  621. */
  622. public function set_extra_fields_in_form(
  623. $form,
  624. $extraData,
  625. $admin_permissions = false,
  626. $extra = array(),
  627. $itemId = null,
  628. $exclude = []
  629. ) {
  630. $type = $this->type;
  631. $jquery_ready_content = null;
  632. if (!empty($extra)) {
  633. foreach ($extra as $field_details) {
  634. // Getting default value id if is set
  635. $defaultValueId = null;
  636. if (isset($field_details['options']) && !empty($field_details['options'])) {
  637. $valueToFind = null;
  638. if (isset($field_details['field_default_value'])) {
  639. $valueToFind = $field_details['field_default_value'];
  640. }
  641. // If a value is found we override the default value
  642. if (isset($extraData['extra_'.$field_details['variable']])) {
  643. $valueToFind = $extraData['extra_'.$field_details['variable']];
  644. }
  645. foreach ($field_details['options'] as $option) {
  646. if ($option['option_value'] == $valueToFind) {
  647. $defaultValueId = $option['id'];
  648. }
  649. }
  650. }
  651. if (!$admin_permissions) {
  652. if ($field_details['visible'] == 0) {
  653. continue;
  654. }
  655. if (in_array($field_details['variable'], $exclude)) {
  656. continue;
  657. }
  658. }
  659. switch ($field_details['field_type']) {
  660. case ExtraField::FIELD_TYPE_TEXT:
  661. $form->addElement(
  662. 'text',
  663. 'extra_'.$field_details['variable'],
  664. $field_details['display_text'],
  665. array()
  666. );
  667. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  668. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  669. if (!$admin_permissions) {
  670. if ($field_details['visible'] == 0) {
  671. $form->freeze(
  672. 'extra_'.$field_details['variable']
  673. );
  674. }
  675. }
  676. break;
  677. case ExtraField::FIELD_TYPE_TEXTAREA:
  678. $form->addHtmlEditor(
  679. 'extra_'.$field_details['variable'],
  680. $field_details['display_text'],
  681. false,
  682. false,
  683. array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130')
  684. );
  685. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  686. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  687. if (!$admin_permissions) {
  688. if ($field_details['visible'] == 0) {
  689. $form->freeze(
  690. 'extra_'.$field_details['variable']
  691. );
  692. }
  693. }
  694. break;
  695. case ExtraField::FIELD_TYPE_RADIO:
  696. $group = array();
  697. if (isset($field_details['options']) && !empty($field_details['options'])) {
  698. foreach ($field_details['options'] as $option_details) {
  699. $options[$option_details['option_value']] = $option_details['display_text'];
  700. $group[] = $form->createElement(
  701. 'radio',
  702. 'extra_'.$field_details['variable'],
  703. $option_details['option_value'],
  704. $option_details['display_text'].'<br />',
  705. $option_details['option_value']
  706. );
  707. }
  708. }
  709. $form->addGroup(
  710. $group,
  711. 'extra_'.$field_details['variable'],
  712. $field_details['display_text'],
  713. ''
  714. );
  715. if (!$admin_permissions) {
  716. if ($field_details['visible'] == 0) {
  717. $form->freeze(
  718. 'extra_'.$field_details['variable']
  719. );
  720. }
  721. }
  722. break;
  723. case ExtraField::FIELD_TYPE_CHECKBOX:
  724. $group = array();
  725. if (isset($field_details['options']) && !empty($field_details['options'])) {
  726. foreach ($field_details['options'] as $option_details) {
  727. $options[$option_details['option_value']] = $option_details['display_text'];
  728. $group[] = $form->createElement(
  729. 'checkbox',
  730. 'extra_'.$field_details['variable'],
  731. $option_details['option_value'],
  732. $option_details['display_text'].'<br />',
  733. $option_details['option_value']
  734. );
  735. }
  736. } else {
  737. $fieldVariable = "extra_{$field_details['variable']}";
  738. $checkboxAttributes = array();
  739. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  740. $checkboxAttributes['checked'] = true;
  741. }
  742. // We assume that is a switch on/off with 1 and 0 as values
  743. $group[] = $form->createElement(
  744. 'checkbox',
  745. 'extra_'.$field_details['variable'],
  746. null,
  747. //$field_details['display_text'].'<br />',
  748. get_lang('Yes'),
  749. $checkboxAttributes
  750. );
  751. }
  752. $form->addGroup(
  753. $group,
  754. 'extra_'.$field_details['variable'],
  755. $field_details['display_text'],
  756. ''
  757. );
  758. if (!$admin_permissions) {
  759. if ($field_details['visible'] == 0) {
  760. $form->freeze(
  761. 'extra_'.$field_details['variable']
  762. );
  763. }
  764. }
  765. break;
  766. case ExtraField::FIELD_TYPE_SELECT:
  767. $get_lang_variables = false;
  768. if (in_array(
  769. $field_details['variable'],
  770. array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message')
  771. )
  772. ) {
  773. $get_lang_variables = true;
  774. }
  775. // Get extra field workflow
  776. $userInfo = api_get_user_info();
  777. $addOptions = array();
  778. $optionsExists = false;
  779. global $app;
  780. // Check if exist $app['orm.em'] object
  781. if (isset($app['orm.em']) && is_object($app['orm.em'])) {
  782. $optionsExists = $app['orm.em']
  783. ->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  784. ->findOneBy(array('fieldId' => $field_details['id']));
  785. }
  786. if ($optionsExists) {
  787. if (isset($userInfo['status']) && !empty($userInfo['status'])) {
  788. $fieldWorkFlow = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  789. ->findBy(
  790. array(
  791. 'fieldId' => $field_details['id'],
  792. 'relatedFieldOptionId' => $defaultValueId,
  793. 'roleId' => $userInfo['status']
  794. )
  795. );
  796. foreach ($fieldWorkFlow as $item) {
  797. $addOptions[] = $item->getFieldOptionId();
  798. }
  799. }
  800. }
  801. $options = array();
  802. if (empty($defaultValueId)) {
  803. $options[''] = get_lang('SelectAnOption');
  804. }
  805. $optionList = array();
  806. if (!empty($field_details['options'])) {
  807. foreach ($field_details['options'] as $option_details) {
  808. $optionList[$option_details['id']] = $option_details;
  809. if ($get_lang_variables) {
  810. $options[$option_details['option_value']] = get_lang($option_details['display_text']);
  811. } else {
  812. if ($optionsExists) {
  813. // Adding always the default value
  814. if ($option_details['id'] == $defaultValueId) {
  815. $options[$option_details['option_value']] = $option_details['display_text'];
  816. } else {
  817. if (isset($addOptions) && !empty($addOptions)) {
  818. // Parsing filters
  819. if (in_array($option_details['id'], $addOptions)) {
  820. $options[$option_details['option_value']] = $option_details['display_text'];
  821. }
  822. }
  823. }
  824. } else {
  825. // Normal behaviour
  826. $options[$option_details['option_value']] = $option_details['display_text'];
  827. }
  828. }
  829. }
  830. if (isset($optionList[$defaultValueId])) {
  831. if (isset($optionList[$defaultValueId]['option_value']) &&
  832. $optionList[$defaultValueId]['option_value'] == 'aprobada'
  833. ) {
  834. // @todo function don't exists api_is_question_manager
  835. /*if (api_is_question_manager() == false) {
  836. $form->freeze();
  837. }*/
  838. }
  839. }
  840. // Setting priority message
  841. if (isset($optionList[$defaultValueId]) &&
  842. isset($optionList[$defaultValueId]['priority'])
  843. ) {
  844. if (!empty($optionList[$defaultValueId]['priority'])) {
  845. $priorityId = $optionList[$defaultValueId]['priority'];
  846. $option = new ExtraFieldOption($this->type);
  847. $messageType = $option->getPriorityMessageType($priorityId);
  848. $form->addElement(
  849. 'label',
  850. null,
  851. Display::return_message(
  852. $optionList[$defaultValueId]['priority_message'],
  853. $messageType
  854. )
  855. );
  856. }
  857. }
  858. }
  859. if ($get_lang_variables) {
  860. $field_details['display_text'] = get_lang($field_details['display_text']);
  861. }
  862. // chzn-select doesn't work for sessions??
  863. $form->addElement(
  864. 'select',
  865. 'extra_' . $field_details['variable'],
  866. $field_details['display_text'],
  867. $options,
  868. array('id' => 'extra_'.$field_details['variable'])
  869. );
  870. /* Enable this when field_loggeable is introduced as a table field (2.0)
  871. if ($optionsExists && $field_details['field_loggeable'] && !empty($defaultValueId)) {
  872. $form->addElement(
  873. 'textarea',
  874. 'extra_' . $field_details['variable'] . '_comment',
  875. $field_details['display_text'] . ' ' . get_lang('Comment')
  876. );
  877. $extraFieldValue = new ExtraFieldValue($this->type);
  878. $repo = $app['orm.em']->getRepository($extraFieldValue->entityName);
  879. $repoLog = $app['orm.em']->getRepository('Gedmo\Loggable\Entity\LogEntry');
  880. $newEntity = $repo->findOneBy(
  881. array(
  882. $this->handlerEntityId => $itemId,
  883. 'fieldId' => $field_details['id']
  884. )
  885. );
  886. // @todo move this in a function inside the class
  887. if ($newEntity) {
  888. $logs = $repoLog->getLogEntries($newEntity);
  889. if (!empty($logs)) {
  890. $html = '<b>' . get_lang('LatestChanges') . '</b><br /><br />';
  891. $table = new HTML_Table(array('class' => 'data_table'));
  892. $table->setHeaderContents(0, 0, get_lang('Value'));
  893. $table->setHeaderContents(0, 1, get_lang('Comment'));
  894. $table->setHeaderContents(0, 2, get_lang('ModifyDate'));
  895. $table->setHeaderContents(0, 3, get_lang('Username'));
  896. $row = 1;
  897. foreach ($logs as $log) {
  898. $column = 0;
  899. $data = $log->getData();
  900. $fieldValue = isset($data['fieldValue']) ? $data['fieldValue'] : null;
  901. $comment = isset($data['comment']) ? $data['comment'] : null;
  902. $table->setCellContents($row, $column, $fieldValue);
  903. $column++;
  904. $table->setCellContents($row, $column, $comment);
  905. $column++;
  906. $table->setCellContents($row, $column, api_get_local_time($log->getLoggedAt()->format('Y-m-d H:i:s')));
  907. $column++;
  908. $table->setCellContents($row, $column, $log->getUsername());
  909. $row++;
  910. }
  911. $form->addElement('label', null, $html.$table->toHtml());
  912. }
  913. }
  914. }
  915. */
  916. if (!$admin_permissions) {
  917. if ($field_details['visible'] == 0) {
  918. $form->freeze('extra_' . $field_details['variable']);
  919. }
  920. }
  921. break;
  922. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  923. $options = array();
  924. foreach ($field_details['options'] as $option_id => $option_details) {
  925. $options[$option_details['option_value']] = $option_details['display_text'];
  926. }
  927. $form->addElement(
  928. 'select',
  929. 'extra_'.$field_details['variable'],
  930. $field_details['display_text'],
  931. $options,
  932. array('multiple' => 'multiple')
  933. );
  934. if (!$admin_permissions) {
  935. if ($field_details['visible'] == 0) {
  936. $form->freeze('extra_'.$field_details['variable']);
  937. }
  938. }
  939. break;
  940. case ExtraField::FIELD_TYPE_DATE:
  941. $form->addDatePicker('extra_'.$field_details['variable'], $field_details['display_text']);
  942. if (!$admin_permissions) {
  943. if ($field_details['visible'] == 0) {
  944. $form->freeze('extra_'.$field_details['variable']);
  945. }
  946. }
  947. $form->applyFilter('theme', 'trim');
  948. break;
  949. case ExtraField::FIELD_TYPE_DATETIME:
  950. $form->addDateTimePicker(
  951. 'extra_'.$field_details['variable'],
  952. $field_details['display_text']
  953. );
  954. $defaults['extra_'.$field_details['variable']] = api_get_local_time();
  955. if (!isset($form->_defaultValues['extra_'.$field_details['variable']])) {
  956. $form->setDefaults($defaults);
  957. }
  958. if (!$admin_permissions) {
  959. if ($field_details['visible'] == 0) {
  960. $form->freeze('extra_'.$field_details['variable']);
  961. }
  962. }
  963. $form->applyFilter('theme', 'trim');
  964. break;
  965. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  966. $first_select_id = 'first_extra_'.$field_details['variable'];
  967. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
  968. $jquery_ready_content .= '
  969. $("#'.$first_select_id.'").on("change", function() {
  970. var id = $(this).val();
  971. if (id) {
  972. $.ajax({
  973. url: "'.$url.'&a=get_second_select_options",
  974. dataType: "json",
  975. data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id,
  976. success: function(data) {
  977. $("#second_extra_'.$field_details['variable'].'").empty();
  978. $.each(data, function(index, value) {
  979. $("#second_extra_'.$field_details['variable'].'").append($("<option/>", {
  980. value: index,
  981. text: value
  982. }));
  983. });
  984. $("#second_extra_'.$field_details['variable'].'").selectpicker("refresh");
  985. },
  986. });
  987. } else {
  988. $("#second_extra_'.$field_details['variable'].'").empty();
  989. }
  990. });';
  991. $first_id = null;
  992. $second_id = null;
  993. if (!empty($extraData)) {
  994. $first_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable']];
  995. $second_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable'].'_second'];
  996. }
  997. $options = ExtraField::extra_field_double_select_convert_array_to_ordered_array(
  998. $field_details['options']
  999. );
  1000. $values = array('' => get_lang('Select'));
  1001. $second_values = array();
  1002. if (!empty($options)) {
  1003. foreach ($options as $option) {
  1004. foreach ($option as $sub_option) {
  1005. if ($sub_option['option_value'] == '0') {
  1006. $values[$sub_option['id']] = $sub_option['display_text'];
  1007. } else {
  1008. if ($first_id === $sub_option['option_value']) {
  1009. $second_values[$sub_option['id']] = $sub_option['display_text'];
  1010. }
  1011. }
  1012. }
  1013. }
  1014. }
  1015. $group = array();
  1016. $group[] = $form->createElement(
  1017. 'select',
  1018. 'extra_'.$field_details['variable'],
  1019. null,
  1020. $values,
  1021. array('id' => $first_select_id)
  1022. );
  1023. $group[] = $form->createElement(
  1024. 'select',
  1025. 'extra_'.$field_details['variable'].'_second',
  1026. null,
  1027. $second_values,
  1028. array('id' => 'second_extra_'.$field_details['variable'])
  1029. );
  1030. $form->addGroup(
  1031. $group,
  1032. 'extra_'.$field_details['variable'],
  1033. $field_details['display_text'],
  1034. '&nbsp;'
  1035. );
  1036. if (!$admin_permissions) {
  1037. if ($field_details['visible'] == 0) {
  1038. $form->freeze('extra_'.$field_details['variable']);
  1039. }
  1040. }
  1041. break;
  1042. case ExtraField::FIELD_TYPE_DIVIDER:
  1043. $form->addElement(
  1044. 'static',
  1045. $field_details['variable'],
  1046. '<br /><strong>'.$field_details['display_text'].'</strong>'
  1047. );
  1048. break;
  1049. case ExtraField::FIELD_TYPE_TAG:
  1050. $variable = $field_details['variable'];
  1051. $field_id = $field_details['id'];
  1052. //Added for correctly translate the extra_field
  1053. $get_lang_variables = false;
  1054. if (in_array($variable, ['tags'])) {
  1055. $get_lang_variables = true;
  1056. }
  1057. if ($get_lang_variables) {
  1058. $field_details['display_text'] = get_lang($field_details['display_text']);
  1059. }
  1060. $tagsSelect = $form->addSelect(
  1061. "extra_{$field_details['variable']}",
  1062. $field_details['display_text']
  1063. );
  1064. $tagsSelect->setAttribute('class', null);
  1065. $tagsSelect->setAttribute('id', "extra_{$field_details['variable']}");
  1066. $tagsSelect->setMultiple(true);
  1067. if ($this->type == 'user') {
  1068. /* //the magic should be here
  1069. $user_tags = UserManager::get_user_tags($user_id, $field_details[0]);
  1070. $tag_list = '';
  1071. if (is_array($user_tags) && count($user_tags) > 0) {
  1072. foreach ($user_tags as $tag) {
  1073. $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
  1074. }
  1075. }
  1076. $multi_select = '<select id="extra_'.$field_details[1].'" name="extra_'.$field_details[1].'">
  1077. '.$tag_list.'
  1078. </select>';
  1079. $form->addElement('label', $field_details[3], $multi_select);
  1080. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  1081. $complete_text = get_lang('StartToType');
  1082. //if cache is set to true the jquery will be called 1 time
  1083. $jquery_ready_content = <<<EOF
  1084. $("#extra_$field_details[1]").fcbkcomplete({
  1085. json_url: "$url?a=search_tags&field_id=$field_details[0]",
  1086. cache: false,
  1087. filter_case: true,
  1088. filter_hide: true,
  1089. complete_text:"$complete_text",
  1090. firstselected: true,
  1091. //onremove: "testme",
  1092. //onselect: "testme",
  1093. filter_selected: true,
  1094. newel: true
  1095. });
  1096. EOF;
  1097. break;*/
  1098. // The magic should be here
  1099. $user_tags = UserManager::get_user_tags($itemId, $field_details['id']);
  1100. if (is_array($user_tags) && count($user_tags) > 0) {
  1101. foreach ($user_tags as $tag) {
  1102. $tagsSelect->addOption(
  1103. $tag['tag'],
  1104. $tag['tag'],
  1105. ['selected' => 'selected', 'class' => 'selected']
  1106. );
  1107. }
  1108. }
  1109. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  1110. } else {
  1111. $em = Database::getManager();
  1112. $fieldTags = $em
  1113. ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  1114. ->findBy([
  1115. 'fieldId' => $field_id,
  1116. 'itemId' => $itemId
  1117. ]);
  1118. foreach ($fieldTags as $fieldTag) {
  1119. $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId());
  1120. if (empty($tag)) {
  1121. continue;
  1122. }
  1123. $tagsSelect->addOption(
  1124. $tag->getTag(),
  1125. $tag->getTag(),
  1126. ['selected' => 'selected', 'class' => 'selected']
  1127. );
  1128. }
  1129. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php';
  1130. }
  1131. $complete_text = get_lang('StartToType');
  1132. //if cache is set to true the jquery will be called 1 time
  1133. $jquery_ready_content .= <<<EOF
  1134. $("#extra_$variable").fcbkcomplete({
  1135. json_url: "$url?a=search_tags&field_id=$field_id&type={$this->type}",
  1136. cache: false,
  1137. filter_case: true,
  1138. filter_hide: true,
  1139. complete_text:"$complete_text",
  1140. firstselected: false,
  1141. filter_selected: true,
  1142. newel: true
  1143. });
  1144. EOF;
  1145. break;
  1146. case ExtraField::FIELD_TYPE_TIMEZONE:
  1147. $form->addElement(
  1148. 'select',
  1149. 'extra_'.$field_details['variable'],
  1150. $field_details['display_text'],
  1151. api_get_timezones(),
  1152. ''
  1153. );
  1154. if ($field_details['visible'] == 0) {
  1155. $form->freeze(
  1156. 'extra_'.$field_details['variable']
  1157. );
  1158. }
  1159. break;
  1160. case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
  1161. // get the social network's favicon
  1162. $extra_data_variable = isset($extraData['extra_'.$field_details['variable']]) ? $extraData['extra_'.$field_details['variable']] : null;
  1163. $field_default_value = isset($field_details['field_default_value']) ? $field_details['field_default_value'] : null;
  1164. $icon_path = UserManager::get_favicon_from_url(
  1165. $extra_data_variable,
  1166. $field_default_value
  1167. );
  1168. // special hack for hi5
  1169. $leftpad = '1.7';
  1170. $top = '0.4';
  1171. $domain = parse_url($icon_path, PHP_URL_HOST);
  1172. if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
  1173. $leftpad = '3';
  1174. $top = '0';
  1175. }
  1176. // print the input field
  1177. $form->addElement(
  1178. 'text',
  1179. 'extra_'.$field_details['variable'],
  1180. $field_details['display_text'],
  1181. array(
  1182. 'size' => 60,
  1183. 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; '
  1184. )
  1185. );
  1186. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1187. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1188. if ($field_details['visible'] == 0) {
  1189. $form->freeze('extra_'.$field_details['variable']);
  1190. }
  1191. break;
  1192. case ExtraField::FIELD_TYPE_MOBILE_PHONE_NUMBER:
  1193. $form->addElement(
  1194. 'text',
  1195. 'extra_'.$field_details[1],
  1196. $field_details[3]." (".get_lang('CountryDialCode').")",
  1197. array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx')
  1198. );
  1199. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  1200. $form->applyFilter('extra_'.$field_details[1], 'trim');
  1201. $form->applyFilter('extra_'.$field_details[1], 'mobile_phone_number_filter');
  1202. $form->addRule(
  1203. 'extra_'.$field_details[1],
  1204. get_lang('MobilePhoneNumberWrong'),
  1205. 'mobile_phone_number'
  1206. );
  1207. if ($field_details['visible'] == 0) {
  1208. $form->freeze('extra_'.$field_details['variable']);
  1209. }
  1210. break;
  1211. case ExtraField::FIELD_TYPE_INTEGER:
  1212. $form->addElement(
  1213. 'number',
  1214. 'extra_'.$field_details['variable'],
  1215. $field_details['display_text'],
  1216. array('class' => 'span1', 'step' => 1)
  1217. );
  1218. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1219. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1220. $form->applyFilter('extra_'.$field_details['variable'], 'intval');
  1221. if (!$admin_permissions) {
  1222. if ($field_details['visible'] == 0) {
  1223. $form->freeze(
  1224. 'extra_'.$field_details['variable']
  1225. );
  1226. }
  1227. }
  1228. break;
  1229. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  1230. $fieldVariable = "extra_{$field_details['variable']}";
  1231. $fieldTexts = array(
  1232. $field_details['display_text']
  1233. );
  1234. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  1235. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1236. $fieldTexts[] = Display::img(
  1237. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1238. $field_details['display_text'],
  1239. array('width' => '300')
  1240. );
  1241. }
  1242. }
  1243. $form->addElement(
  1244. 'file',
  1245. $fieldVariable,
  1246. $fieldTexts,
  1247. array('accept' => 'image/*')
  1248. );
  1249. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1250. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1251. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  1252. $form->addRule(
  1253. 'extra_'.$field_details['variable'],
  1254. get_lang('OnlyImagesAllowed') . ' ('.implode(',', $allowed_picture_types).')',
  1255. 'filetype',
  1256. $allowed_picture_types
  1257. );
  1258. if (!$admin_permissions) {
  1259. if ($field_details['visible'] == 0) {
  1260. $form->freeze(
  1261. 'extra_'.$field_details['variable']
  1262. );
  1263. }
  1264. }
  1265. break;
  1266. case ExtraField::FIELD_TYPE_FLOAT:
  1267. $form->addElement(
  1268. 'number',
  1269. 'extra_'.$field_details['variable'],
  1270. $field_details['display_text'],
  1271. array('class' => 'span1', 'step' => '0.01')
  1272. );
  1273. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1274. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1275. $form->applyFilter('extra_'.$field_details['variable'], 'floatval');
  1276. if (!$admin_permissions) {
  1277. if ($field_details['visible'] == 0) {
  1278. $form->freeze(
  1279. 'extra_'.$field_details['variable']
  1280. );
  1281. }
  1282. }
  1283. break;
  1284. case ExtraField::FIELD_TYPE_FILE:
  1285. $fieldVariable = "extra_{$field_details['variable']}";
  1286. $fieldTexts = array(
  1287. $field_details['display_text']
  1288. );
  1289. if (is_array($extraData) &&
  1290. array_key_exists($fieldVariable, $extraData)
  1291. ) {
  1292. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1293. $fieldTexts[] = Display::url(
  1294. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1295. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1296. array(
  1297. 'title' => $field_details['display_text'],
  1298. 'target' => '_blank'
  1299. )
  1300. );
  1301. }
  1302. }
  1303. $form->addElement(
  1304. 'file',
  1305. $fieldVariable,
  1306. $fieldTexts,
  1307. array()
  1308. );
  1309. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1310. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1311. if (!$admin_permissions) {
  1312. if ($field_details['visible'] == 0) {
  1313. $form->freeze(
  1314. 'extra_'.$field_details['variable']
  1315. );
  1316. }
  1317. }
  1318. break;
  1319. case ExtraField::FIELD_TYPE_VIDEO_URL:
  1320. //Added for correctly translate the extra_field
  1321. $get_lang_variables = false;
  1322. if (in_array($field_details['variable'], ['video_url'])) {
  1323. $get_lang_variables = true;
  1324. }
  1325. if ($get_lang_variables) {
  1326. $field_details['display_text'] = get_lang($field_details['display_text']);
  1327. }
  1328. $form->addUrl(
  1329. "extra_{$field_details['variable']}",
  1330. $field_details['display_text'],
  1331. false,
  1332. ['placeholder' => 'https://']
  1333. );
  1334. break;
  1335. case ExtraField::FIELD_TYPE_LETTERS_ONLY:
  1336. $form->addTextLettersOnly(
  1337. "extra_{$field_details['variable']}",
  1338. $field_details['display_text']
  1339. );
  1340. $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
  1341. if (!$admin_permissions) {
  1342. if ($field_details['visible'] == 0) {
  1343. $form->freeze(
  1344. 'extra_' . $field_details['variable']
  1345. );
  1346. }
  1347. }
  1348. break;
  1349. case ExtraField::FIELD_TYPE_ALPHANUMERIC:
  1350. $form->addTextAlphanumeric(
  1351. "extra_{$field_details['variable']}",
  1352. $field_details['display_text']
  1353. );
  1354. $form->applyFilter(
  1355. 'extra_' . $field_details['variable'],
  1356. 'stripslashes'
  1357. );
  1358. if (!$admin_permissions) {
  1359. if ($field_details['visible'] == 0) {
  1360. $form->freeze(
  1361. 'extra_' . $field_details['variable']
  1362. );
  1363. }
  1364. }
  1365. break;
  1366. case ExtraField::FIELD_TYPE_LETTERS_SPACE:
  1367. $form->addTextLettersAndSpaces(
  1368. "extra_{$field_details['variable']}",
  1369. $field_details['display_text']
  1370. );
  1371. $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
  1372. if (!$admin_permissions) {
  1373. if ($field_details['visible'] == 0) {
  1374. $form->freeze(
  1375. 'extra_' . $field_details['variable']
  1376. );
  1377. }
  1378. }
  1379. break;
  1380. case ExtraField::FIELD_TYPE_ALPHANUMERIC_SPACE:
  1381. $form->addTextAlphanumericAndSpaces(
  1382. "extra_{$field_details['variable']}",
  1383. $field_details['display_text']
  1384. );
  1385. $form->applyFilter(
  1386. 'extra_' . $field_details['variable'],
  1387. 'stripslashes'
  1388. );
  1389. if (!$admin_permissions) {
  1390. if ($field_details['visible'] == 0) {
  1391. $form->freeze(
  1392. 'extra_' . $field_details['variable']
  1393. );
  1394. }
  1395. }
  1396. break;
  1397. }
  1398. }
  1399. }
  1400. $return = array();
  1401. $return['jquery_ready_content'] = $jquery_ready_content;
  1402. return $return;
  1403. }
  1404. function setupBreadcrumb(&$breadcrumb, $action)
  1405. {
  1406. if ($action == 'add') {
  1407. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  1408. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Add'));
  1409. } elseif ($action == 'edit') {
  1410. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  1411. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Edit'));
  1412. } else {
  1413. $breadcrumb[] = array('url' => '#', 'name' => $this->pageName);
  1414. }
  1415. }
  1416. /**
  1417. * Displays the title + grid
  1418. */
  1419. public function display()
  1420. {
  1421. // action links
  1422. echo '<div class="actions">';
  1423. echo '<a href="../admin/index.php">'.Display::return_icon(
  1424. 'back.png',
  1425. get_lang('BackTo').' '.get_lang('PlatformAdmin'),
  1426. '',
  1427. ICON_SIZE_MEDIUM
  1428. ).'</a>';
  1429. echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'">'.Display::return_icon(
  1430. 'add_user_fields.png',
  1431. get_lang('Add'),
  1432. '',
  1433. ICON_SIZE_MEDIUM
  1434. ).'</a>';
  1435. echo '</div>';
  1436. echo Display::grid_html($this->type.'_fields');
  1437. }
  1438. /**
  1439. * @return array
  1440. */
  1441. public function getJqgridColumnNames()
  1442. {
  1443. return array(
  1444. get_lang('Name'),
  1445. get_lang('FieldLabel'),
  1446. get_lang('Type'),
  1447. get_lang('FieldChangeability'),
  1448. get_lang('Visibility'),
  1449. get_lang('Filter'),
  1450. get_lang('FieldOrder'),
  1451. get_lang('Actions')
  1452. );
  1453. }
  1454. /**
  1455. * @return array
  1456. */
  1457. public function getJqgridColumnModel()
  1458. {
  1459. return array(
  1460. array(
  1461. 'name' => 'display_text',
  1462. 'index' => 'display_text',
  1463. 'width' => '180',
  1464. 'align' => 'left',
  1465. ),
  1466. array(
  1467. 'name' => 'variable',
  1468. 'index' => 'variable',
  1469. 'width' => '',
  1470. 'align' => 'left',
  1471. 'sortable' => 'true'
  1472. ),
  1473. array(
  1474. 'name' => 'field_type',
  1475. 'index' => 'field_type',
  1476. 'width' => '',
  1477. 'align' => 'left',
  1478. 'sortable' => 'true'
  1479. ),
  1480. array(
  1481. 'name' => 'changeable',
  1482. 'index' => 'changeable',
  1483. 'width' => '50',
  1484. 'align' => 'left',
  1485. 'sortable' => 'true'
  1486. ),
  1487. array(
  1488. 'name' => 'visible',
  1489. 'index' => 'visible',
  1490. 'width' => '40',
  1491. 'align' => 'left',
  1492. 'sortable' => 'true'
  1493. ),
  1494. array(
  1495. 'name' => 'filter',
  1496. 'index' => 'filter',
  1497. 'width' => '30',
  1498. 'align' => 'left',
  1499. 'sortable' => 'true'
  1500. ),
  1501. array(
  1502. 'name' => 'field_order',
  1503. 'index' => 'field_order',
  1504. 'width' => '40',
  1505. 'align' => 'left',
  1506. 'sortable' => 'true'
  1507. ),
  1508. array(
  1509. 'name' => 'actions',
  1510. 'index' => 'actions',
  1511. 'width' => '100',
  1512. 'align' => 'left',
  1513. 'formatter' => 'action_formatter',
  1514. 'sortable' => 'false'
  1515. )
  1516. );
  1517. }
  1518. /**
  1519. * @param string $url
  1520. * @param string $action
  1521. * @return FormValidator
  1522. */
  1523. public function return_form($url, $action)
  1524. {
  1525. $form = new FormValidator($this->type.'_field', 'post', $url);
  1526. $form->addElement('hidden', 'type', $this->type);
  1527. $id = isset($_GET['id']) ? intval($_GET['id']) : null;
  1528. $form->addElement('hidden', 'id', $id);
  1529. // Setting the form elements
  1530. $header = get_lang('Add');
  1531. $defaults = array();
  1532. if ($action == 'edit') {
  1533. $header = get_lang('Modify');
  1534. // Setting the defaults
  1535. $defaults = $this->get($id);
  1536. }
  1537. $form->addElement('header', $header);
  1538. $form->addElement('text', 'display_text', get_lang('Name'), array('class' => 'span5'));
  1539. // Field type
  1540. $types = self::get_field_types();
  1541. $form->addElement(
  1542. 'select',
  1543. 'field_type',
  1544. get_lang('FieldType'),
  1545. $types,
  1546. array('id' => 'field_type')
  1547. );
  1548. $form->addElement('label', get_lang('Example'), '<div id="example">-</div>');
  1549. $form->addElement('text', 'variable', get_lang('FieldLabel'), array('class' => 'span5'));
  1550. $form->addElement(
  1551. 'text',
  1552. 'field_options',
  1553. get_lang('FieldPossibleValues'),
  1554. array('id' => 'field_options', 'class' => 'span6')
  1555. );
  1556. $fieldWithOptions = array(
  1557. ExtraField::FIELD_TYPE_RADIO,
  1558. ExtraField::FIELD_TYPE_SELECT_MULTIPLE,
  1559. ExtraField::FIELD_TYPE_SELECT,
  1560. ExtraField::FIELD_TYPE_TAG,
  1561. ExtraField::FIELD_TYPE_DOUBLE_SELECT,
  1562. );
  1563. if ($action == 'edit') {
  1564. if (in_array($defaults['field_type'], $fieldWithOptions)) {
  1565. $url = Display::url(
  1566. get_lang('EditExtraFieldOptions'),
  1567. 'extra_field_options.php?type='.$this->type.'&field_id='.$id
  1568. );
  1569. $form->addElement('label', null, $url);
  1570. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_SELECT) {
  1571. $urlWorkFlow = Display::url(
  1572. get_lang('EditExtraFieldWorkFlow'),
  1573. 'extra_field_workflow.php?type='.$this->type.'&field_id='.$id
  1574. );
  1575. $form->addElement('label', null, $urlWorkFlow);
  1576. }
  1577. $form->freeze('field_options');
  1578. }
  1579. }
  1580. $form->addElement(
  1581. 'text',
  1582. 'default_value',
  1583. get_lang('FieldDefaultValue'),
  1584. array('id' => 'default_value')
  1585. );
  1586. $group = array();
  1587. $group[] = $form->createElement('radio', 'visible', null, get_lang('Yes'), 1);
  1588. $group[] = $form->createElement('radio', 'visible', null, get_lang('No'), 0);
  1589. $form->addGroup($group, '', get_lang('Visible'), '', false);
  1590. $group = array();
  1591. $group[] = $form->createElement('radio', 'changeable', null, get_lang('Yes'), 1);
  1592. $group[] = $form->createElement('radio', 'changeable', null, get_lang('No'), 0);
  1593. $form->addGroup($group, '', get_lang('FieldChangeability'), '', false);
  1594. $group = array();
  1595. $group[] = $form->createElement('radio', 'filter', null, get_lang('Yes'), 1);
  1596. $group[] = $form->createElement('radio', 'filter', null, get_lang('No'), 0);
  1597. $form->addGroup($group, '', get_lang('FieldFilter'), '', false);
  1598. /* Enable this when field_loggeable is introduced as a table field (2.0)
  1599. $group = array();
  1600. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('Yes'), 1);
  1601. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('No'), 0);
  1602. $form->addGroup($group, '', get_lang('FieldLoggeable'), '', false);
  1603. */
  1604. $form->addElement('text', 'field_order', get_lang('FieldOrder'), array('class' => 'span1'));
  1605. if ($action == 'edit') {
  1606. $option = new ExtraFieldOption($this->type);
  1607. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1608. $form->freeze('field_options');
  1609. }
  1610. $defaults['field_options'] = $option->get_field_options_by_field_to_string($id);
  1611. $form->addButtonUpdate(get_lang('Modify'));
  1612. } else {
  1613. $defaults['visible'] = 0;
  1614. $defaults['changeable'] = 0;
  1615. $defaults['filter'] = 0;
  1616. $form->addButtonCreate(get_lang('Add'));
  1617. }
  1618. /*if (!empty($defaults['created_at'])) {
  1619. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  1620. }
  1621. if (!empty($defaults['updated_at'])) {
  1622. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  1623. }*/
  1624. $form->setDefaults($defaults);
  1625. // Setting the rules
  1626. $form->addRule('display_text', get_lang('ThisFieldIsRequired'), 'required');
  1627. $form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required');
  1628. return $form;
  1629. }
  1630. /**
  1631. * @param $token
  1632. * @return string
  1633. */
  1634. public function getJqgridActionLinks($token)
  1635. {
  1636. //With this function we can add actions to the jgrid (edit, delete, etc)
  1637. return 'function action_formatter(cellvalue, options, rowObject) {
  1638. return \'<a href="?action=edit&type='.$this->type.'&id=\'+options.rowId+\'">'.Display::return_icon(
  1639. 'edit.png',
  1640. get_lang('Edit'),
  1641. '',
  1642. ICON_SIZE_SMALL
  1643. ).'</a>'.
  1644. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
  1645. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  1646. )."\'".')) return false;" href="?sec_token='.$token.'&type='.$this->type.'&action=delete&id=\'+options.rowId+\'">'.Display::return_icon(
  1647. 'delete.png',
  1648. get_lang('Delete'),
  1649. '',
  1650. ICON_SIZE_SMALL
  1651. ).'</a>'.
  1652. '\';
  1653. }';
  1654. }
  1655. /**
  1656. * @param array $columns
  1657. * @param array $column_model
  1658. * @param array $extraFields
  1659. * @return array
  1660. */
  1661. public function getRules(&$columns, &$column_model, $extraFields = array(), $checkExtraFieldExistence = false)
  1662. {
  1663. $fields = $this->get_all(
  1664. array(
  1665. 'visible = ? AND filter = ?' => array(1, 1)
  1666. ),
  1667. 'display_text'
  1668. );
  1669. $extraFieldOption = new ExtraFieldOption($this->type);
  1670. $rules = array();
  1671. if (!empty($fields)) {
  1672. foreach ($fields as $field) {
  1673. $search_options = array();
  1674. $type = 'text';
  1675. if (in_array($field['field_type'], array(self::FIELD_TYPE_SELECT, self::FIELD_TYPE_DOUBLE_SELECT))) {
  1676. $type = 'select';
  1677. $search_options['sopt'] = array('eq', 'ne'); //equal not equal
  1678. } else {
  1679. $search_options['sopt'] = array('cn', 'nc'); //contains not contains
  1680. }
  1681. $search_options['searchhidden'] = 'true';
  1682. $search_options['defaultValue'] = isset($search_options['field_default_value']) ? $search_options['field_default_value'] : null;
  1683. if ($field['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  1684. //Add 2 selects
  1685. $options = $extraFieldOption->get_field_options_by_field($field['id']);
  1686. $options = self::extra_field_double_select_convert_array_to_ordered_array($options);
  1687. $first_options = array();
  1688. if (!empty($options)) {
  1689. foreach ($options as $option) {
  1690. foreach ($option as $sub_option) {
  1691. if ($sub_option['option_value'] == 0) {
  1692. $first_options[] = $sub_option['field_id'].'#'.$sub_option['id'].':'.$sub_option['display_text'];
  1693. }
  1694. }
  1695. }
  1696. }
  1697. $search_options['value'] = implode(';', $first_options);
  1698. $search_options['dataInit'] = 'fill_second_select';
  1699. // First
  1700. $column_model[] = array(
  1701. 'name' => 'extra_'.$field['variable'],
  1702. 'index' => 'extra_'.$field['variable'],
  1703. 'width' => '100',
  1704. 'hidden' => 'true',
  1705. 'search' => 'true',
  1706. 'stype' => 'select',
  1707. 'searchoptions' => $search_options,
  1708. );
  1709. $columns[] = $field['display_text'].' (1)';
  1710. $rules[] = array(
  1711. 'field' => 'extra_'.$field['variable'],
  1712. 'op' => 'cn',
  1713. );
  1714. //Second
  1715. $search_options['value'] = $field['id'].':';
  1716. $search_options['dataInit'] = 'register_second_select';
  1717. $column_model[] = array(
  1718. 'name' => 'extra_'.$field['variable'].'_second',
  1719. 'index' => 'extra_'.$field['variable'].'_second',
  1720. 'width' => '100',
  1721. 'hidden' => 'true',
  1722. 'search' => 'true',
  1723. 'stype' => 'select',
  1724. 'searchoptions' => $search_options,
  1725. );
  1726. $columns[] = $field['display_text'].' (2)';
  1727. $rules[] = array('field' => 'extra_'.$field['variable'].'_second', 'op' => 'cn');
  1728. continue;
  1729. } else {
  1730. $search_options['value'] = $extraFieldOption->getFieldOptionsToString(
  1731. $field['id'],
  1732. false,
  1733. 'display_text'
  1734. );
  1735. }
  1736. $column_model[] = array(
  1737. 'name' => 'extra_'.$field['variable'],
  1738. 'index' => 'extra_'.$field['variable'],
  1739. 'width' => '100',
  1740. 'hidden' => 'true',
  1741. 'search' => 'true',
  1742. 'stype' => $type,
  1743. 'searchoptions' => $search_options
  1744. );
  1745. $columns[] = $field['display_text'];
  1746. $rules[] = array('field' => 'extra_'.$field['variable'], 'op' => 'cn');
  1747. }
  1748. }
  1749. return $rules;
  1750. }
  1751. /**
  1752. * @param array $options
  1753. * @return array
  1754. */
  1755. public function parseConditions($options)
  1756. {
  1757. $inject_extra_fields = null;
  1758. $extraFieldOption = new ExtraFieldOption($this->type);
  1759. $double_fields = array();
  1760. if (isset($options['extra'])) {
  1761. $extra_fields = $options['extra'];
  1762. if (!empty($extra_fields)) {
  1763. $counter = 1;
  1764. foreach ($extra_fields as &$extra) {
  1765. $extra_field_obj = new ExtraField($this->type);
  1766. $extra_field_info = $extra_field_obj->get($extra['id']);
  1767. $extra['extra_field_info'] = $extra_field_info;
  1768. if (isset($extra_field_info['field_type']) && in_array(
  1769. $extra_field_info['field_type'],
  1770. array(
  1771. ExtraField::FIELD_TYPE_SELECT,
  1772. ExtraField::FIELD_TYPE_SELECT,
  1773. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1774. )
  1775. )
  1776. ) {
  1777. $inject_extra_fields .= " fvo$counter.display_text as {$extra['field']}, ";
  1778. } else {
  1779. $inject_extra_fields .= " fv$counter.value as {$extra['field']}, ";
  1780. }
  1781. if (isset($extra_fields_info[$extra['id']])) {
  1782. $info = $extra_fields_info[$extra['id']];
  1783. } else {
  1784. $info = $this->get($extra['id']);
  1785. $extra_fields_info[$extra['id']] = $info;
  1786. }
  1787. if (isset($info['field_type']) && $info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1788. $double_fields[$info['id']] = $info;
  1789. }
  1790. $counter++;
  1791. }
  1792. }
  1793. }
  1794. $options_by_double = array();
  1795. foreach ($double_fields as $double) {
  1796. $my_options = $extraFieldOption->get_field_options_by_field(
  1797. $double['id'],
  1798. true
  1799. );
  1800. $options_by_double['extra_'.$double['variable']] = $my_options;
  1801. }
  1802. $field_value_to_join = array();
  1803. //filter can be all/any = and/or
  1804. $inject_joins = null;
  1805. $inject_where = null;
  1806. $where = null;
  1807. if (!empty($options['where'])) {
  1808. if (!empty($options['extra'])) {
  1809. // Removing double 1=1
  1810. $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
  1811. // Always OR
  1812. $counter = 1;
  1813. foreach ($extra_fields as $extra_info) {
  1814. $extra_field_info = $extra_info['extra_field_info'];
  1815. $inject_joins .= " INNER JOIN $this->table_field_values fv$counter
  1816. ON (s.".$this->primaryKey." = fv$counter.".$this->handler_id.") ";
  1817. // Add options
  1818. if (isset($extra_field_info['field_type']) && in_array(
  1819. $extra_field_info['field_type'],
  1820. array(
  1821. ExtraField::FIELD_TYPE_SELECT,
  1822. ExtraField::FIELD_TYPE_SELECT,
  1823. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1824. )
  1825. )
  1826. ) {
  1827. $options['where'] = str_replace(
  1828. $extra_info['field'],
  1829. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fvo'.$counter.'.option_value',
  1830. $options['where']
  1831. );
  1832. $inject_joins .= " INNER JOIN $this->table_field_options fvo$counter ".
  1833. " ON (fv$counter.field_id = fvo$counter.field_id AND fv$counter.value = fvo$counter.option_value) ";
  1834. } else {
  1835. //text, textarea, etc
  1836. $options['where'] = str_replace(
  1837. $extra_info['field'],
  1838. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fv'.$counter.'.value',
  1839. $options['where']
  1840. );
  1841. }
  1842. $field_value_to_join[] = " fv$counter.$this->handler_id ";
  1843. $counter++;
  1844. }
  1845. if (!empty($field_value_to_join)) {
  1846. //$inject_where .= " AND s.id = ".implode(' = ', $field_value_to_join);
  1847. }
  1848. }
  1849. $where .= ' AND '.$options['where'];
  1850. }
  1851. $order = null;
  1852. if (!empty($options['order'])) {
  1853. $order = " ORDER BY ".$options['order'];
  1854. }
  1855. $limit = null;
  1856. if (!empty($options['limit'])) {
  1857. $limit = " LIMIT ".$options['limit'];
  1858. }
  1859. return array(
  1860. 'order' => $order,
  1861. 'limit' => $limit,
  1862. 'where' => $where,
  1863. 'inject_where' => $inject_where,
  1864. 'inject_joins' => $inject_joins,
  1865. 'field_value_to_join' => $field_value_to_join,
  1866. 'inject_extra_fields' => $inject_extra_fields,
  1867. );
  1868. }
  1869. //@todo move this in the display_class or somewhere else
  1870. /**
  1871. * @param $col
  1872. * @param $oper
  1873. * @param $val
  1874. * @return string
  1875. */
  1876. public function get_where_clause($col, $oper, $val)
  1877. {
  1878. if (empty($col)) {
  1879. return '';
  1880. }
  1881. if ($oper == 'bw' || $oper == 'bn') {
  1882. $val .= '%';
  1883. }
  1884. if ($oper == 'ew' || $oper == 'en') {
  1885. $val = '%'.$val;
  1886. }
  1887. if ($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') {
  1888. $val = '%'.$val.'%';
  1889. }
  1890. $val = \Database::escape_string($val);
  1891. return " $col {$this->ops[$oper]} '$val' ";
  1892. }
  1893. /**
  1894. * @param $filters
  1895. * @param string $stringToSearch
  1896. * @return array
  1897. */
  1898. public function getExtraFieldRules($filters, $stringToSearch = 'extra_')
  1899. {
  1900. $extra_fields = array();
  1901. // Getting double select if exists
  1902. $double_select = array();
  1903. foreach ($filters->rules as $rule) {
  1904. if (strpos($rule->field, '_second') === false) {
  1905. } else {
  1906. $my_field = str_replace('_second', '', $rule->field);
  1907. $double_select[$my_field] = $rule->data;
  1908. }
  1909. }
  1910. $condition_array = array();
  1911. foreach ($filters->rules as $rule) {
  1912. if (strpos($rule->field, $stringToSearch) === false) {
  1913. //normal fields
  1914. $field = $rule->field;
  1915. if (isset($rule->data) && $rule->data != -1) {
  1916. $condition_array[] = $this->get_where_clause($field, $rule->op, $rule->data);
  1917. }
  1918. } else {
  1919. // Extra fields
  1920. if (strpos($rule->field, '_second') === false) {
  1921. //No _second
  1922. $original_field = str_replace($stringToSearch, '', $rule->field);
  1923. $field_option = $this->get_handler_field_info_by_variable($original_field);
  1924. if ($field_option['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1925. if (isset($double_select[$rule->field])) {
  1926. $data = explode('#', $rule->data);
  1927. $rule->data = $data[1].'::'.$double_select[$rule->field];
  1928. } else {
  1929. // only was sent 1 select
  1930. $data = explode('#', $rule->data);
  1931. $rule->data = $data[1];
  1932. }
  1933. if (!isset($rule->data)) {
  1934. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  1935. $extra_fields[] = array('field' => $rule->field, 'id' => $field_option['id']);
  1936. }
  1937. } else {
  1938. if (isset($rule->data)) {
  1939. if ($rule->data == -1) {
  1940. continue;
  1941. }
  1942. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  1943. $extra_fields[] = array(
  1944. 'field' => $rule->field,
  1945. 'id' => $field_option['id'],
  1946. 'data' => $rule->data
  1947. );
  1948. }
  1949. }
  1950. } else {
  1951. $my_field = str_replace('_second', '', $rule->field);
  1952. $original_field = str_replace($stringToSearch, '', $my_field);
  1953. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  1954. $extra_fields[] = array(
  1955. 'field' => $rule->field,
  1956. 'id' => $field_option['id']
  1957. );
  1958. }
  1959. }
  1960. }
  1961. return array(
  1962. 'extra_fields' => $extra_fields,
  1963. 'condition_array' => $condition_array
  1964. );
  1965. }
  1966. /**
  1967. * Get the extra fields and their formatted values
  1968. * @param int|string $itemId The item ID (It could be a session_id, course_id or user_id)
  1969. * @return array The extra fields data
  1970. */
  1971. public function getDataAndFormattedValues($itemId)
  1972. {
  1973. $valuesData = array();
  1974. $fields = $this->get_all();
  1975. foreach ($fields as $field) {
  1976. if ($field['visible'] != '1') {
  1977. continue;
  1978. }
  1979. $fieldValue = new ExtraFieldValue($this->type);
  1980. $valueData = $fieldValue->get_values_by_handler_and_field_id($itemId, $field['id'], true);
  1981. if (!$valueData) {
  1982. continue;
  1983. }
  1984. $displayedValue = get_lang('None');
  1985. switch ($field['field_type']) {
  1986. case ExtraField::FIELD_TYPE_CHECKBOX:
  1987. if ($valueData !== false && $valueData['value'] == '1') {
  1988. $displayedValue = get_lang('Yes');
  1989. } else {
  1990. $displayedValue = get_lang('No');
  1991. }
  1992. break;
  1993. case ExtraField::FIELD_TYPE_DATE:
  1994. if ($valueData !== false && !empty($valueData['value'])) {
  1995. $displayedValue = api_format_date($valueData['value'], DATE_FORMAT_LONG_NO_DAY);
  1996. }
  1997. break;
  1998. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  1999. if ($valueData === false || empty($valueData['value'])) {
  2000. break;
  2001. }
  2002. if (!file_exists(api_get_path(SYS_UPLOAD_PATH) . $valueData['value'])) {
  2003. break;
  2004. }
  2005. $image = Display::img(
  2006. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  2007. $field['display_text'],
  2008. array('width' => '300')
  2009. );
  2010. $displayedValue = Display::url(
  2011. $image,
  2012. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  2013. array('target' => '_blank')
  2014. );
  2015. break;
  2016. case ExtraField::FIELD_TYPE_FILE:
  2017. if ($valueData === false || empty($valueData['value'])) {
  2018. break;
  2019. }
  2020. if (!file_exists(api_get_path(SYS_UPLOAD_PATH) . $valueData['value'])) {
  2021. break;
  2022. }
  2023. $displayedValue = Display::url(
  2024. get_lang('Download'),
  2025. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  2026. array(
  2027. 'title' => $field['display_text'],
  2028. 'target' => '_blank'
  2029. )
  2030. );
  2031. break;
  2032. default:
  2033. $displayedValue = $valueData['value'];
  2034. break;
  2035. }
  2036. $valuesData[] = array(
  2037. 'text' => $field['display_text'],
  2038. 'value' => $displayedValue
  2039. );
  2040. }
  2041. return $valuesData;
  2042. }
  2043. }